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_book.rb | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 app/models/phone_book.rb (limited to 'app/models/phone_book.rb') diff --git a/app/models/phone_book.rb b/app/models/phone_book.rb new file mode 100644 index 0000000..3603eae --- /dev/null +++ b/app/models/phone_book.rb @@ -0,0 +1,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 -- cgit v1.2.3