summaryrefslogtreecommitdiff
path: root/app/models/phone_model.rb
blob: ac4d4a340b353defc949423ba88464df31bce770 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class PhoneModel < ActiveRecord::Base
  attr_accessible :name, :product_manual_homepage_url, :product_homepage_url, :uuid
  
  # Associations
  #
  belongs_to :manufacturer, :touch => true
  
  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