summaryrefslogtreecommitdiff
path: root/app/models/phone_book.rb
blob: 3603eae4730c56b32ff842bf67fc941765086215 (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
class PhoneBook < ActiveRecord::Base
  attr_accessible :name, :description, :uuid
  
  belongs_to :phone_bookable, :polymorphic => true
  has_many :phone_book_entries, :dependent => :destroy
  
  validates_presence_of :name
  validates_uniqueness_of :name, :scope => [ :phone_bookable_type, :phone_bookable_id ] 
  
  validates_length_of :name, :within => 1..50

  validates_presence_of :uuid
  validates_uniqueness_of :uuid

  # State Machine stuff
  default_scope where(:state => 'active')
  state_machine :initial => :active do
  end
  
  def to_s
    name
  end

  def find_entry_by_number(number)
    phone_book_entries_ids = self.phone_book_entries.map{|phone_book_entry| phone_book_entry.id}
    
    phone_number = PhoneNumber.where(:phone_numberable_id => phone_book_entries_ids, :phone_numberable_type => 'PhoneBookEntry', :number => number).first

    if phone_number
      return phone_number.phone_numberable
    end
  end
end