From b80bd744ad873f6fc43018bc4bfb90677de167bd Mon Sep 17 00:00:00 2001 From: Stefan Wintermeyer Date: Mon, 17 Dec 2012 12:01:45 +0100 Subject: Start of GS5. --- app/models/phone_model.rb | 56 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 app/models/phone_model.rb (limited to 'app/models/phone_model.rb') diff --git a/app/models/phone_model.rb b/app/models/phone_model.rb new file mode 100644 index 0000000..e00e0e3 --- /dev/null +++ b/app/models/phone_model.rb @@ -0,0 +1,56 @@ +class PhoneModel < ActiveRecord::Base + attr_accessible :name, :product_manual_homepage_url, :product_homepage_url, :uuid + + # Associations + # + belongs_to :manufacturer + + has_many :phones, :dependent => :destroy + + # Validations + # + validates_presence_of :name + validate :validate_product_manual_homepage_url + validate :validate_product_homepage_url + + validates_presence_of :uuid + validates_uniqueness_of :uuid + + def to_s + self.name + end + + # State machine: + # + default_scope where(:state => 'active') + state_machine :initial => :active do + + event :deactivate do + transition [:active] => :deactivated + end + + event :activate do + transition [:deactivated] => :active + end + end + + + private + + def validate_product_manual_homepage_url + if ! self.product_manual_homepage_url.blank? + if ! CustomValidators.validate_url( self.product_manual_homepage_url ) + errors.add( :product_manual_homepage_url, "is invalid." ) + end + end + end + + def validate_product_homepage_url + if ! self.product_homepage_url.blank? + if ! CustomValidators.validate_url( self.product_homepage_url ) + errors.add( :product_homepage_url, "is invalid." ) + end + end + end + +end -- cgit v1.2.3