summaryrefslogtreecommitdiff
path: root/app/models/manufacturer.rb
blob: 03d2bb759d6ae4de988041ff898cd2fb94358441 (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
class Manufacturer < ActiveRecord::Base
  attr_accessible :name, :ieee_name, :homepage_url
  
  # Associations:
  #
  has_many :ouis, :dependent => :destroy
  has_many :phone_models, :order => :name, :dependent => :destroy
  
  
  # Validations:
  #
  validates_presence_of :name
  validates_presence_of :ieee_name
  
  validates_uniqueness_of :name, :case_sensitive => false
  
  validate :validate_homepage_url
  
  # State Machine stuff
  default_scope where(:state => 'active').order(:name)
  state_machine :initial => :active do

    event :deactivate do
      transition [:active] => :deactivated
    end
    
    event :activate do
      transition [:deactivated] => :active
    end
  end
  
  def to_s
    self.name
  end
  
  private
  
  def validate_homepage_url
    if ! self.homepage_url.blank?
      if ! CustomValidators.validate_url( self.homepage_url )
        errors.add( :homepage_url, "is invalid." )
      end
    end
  end
  
end