summaryrefslogtreecommitdiff
path: root/app/models/callthrough.rb
blob: c057fa6fcbe6b08ef9a766b96e48ae267c99045b (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
57
58
59
60
class Callthrough < ActiveRecord::Base
  attr_accessible :name, :clip_no_screening,
                  :phone_numbers_attributes, :access_authorizations_attributes,
                  :whitelists_attributes

  # Validations and Associations
  #
  belongs_to :tenant

  validates_presence_of :tenant_id
  validates_presence_of :tenant

  # These are the phone_numbers for this callthrough.
  # One has to dial this number to access the callthrough.
  #
  has_many :phone_numbers, :as => :phone_numberable, :dependent => :destroy

  accepts_nested_attributes_for :phone_numbers, 
                                :reject_if => lambda { |phone_number| phone_number[:number].blank? }, 
                                :allow_destroy => true

  validate :requires_at_least_one_phone_number

  # These are the access authorizations for this callthrough.
  # One has to be known by his phone number or by a login/pin or even both.
  #
  has_many :access_authorizations, :as => :access_authorizationable, :dependent => :destroy

  accepts_nested_attributes_for :access_authorizations, 
                                :reject_if => lambda { |access_authorization| access_authorization[:login].blank? && access_authorization[:pin].blank? && access_authorization[:phone_numbers_attributes]['0'][:number].blank? },
                                :allow_destroy => true

  has_many :access_authorization_phone_numbers, :source => :phone_numbers, 
           :through => :access_authorizations, :readonly => true

  # These are the whitelists of the phone numbers which can be called through this callthrough.
  #
  has_many :whitelists, :as => :whitelistable, :dependent => :destroy

  accepts_nested_attributes_for :whitelists, 
                                :reject_if => lambda { |whitelist| whitelist[:phone_numbers_attributes]['0']['number'].blank? }, 
                                :allow_destroy => true

  has_many :whitelisted_phone_numbers, :source => :phone_numbers, 
           :through => :whitelists, :readonly => true

  # Delegations:
  #
  delegate :sip_domain, :to => :tenant, :allow_nil => true

  def to_s
    self.name || I18n.t('callthroughs.name') + ' ID ' + self.id
  end


  private
  def requires_at_least_one_phone_number
    errors.add(:base, "You must provide at least one phone number") if !self.phone_numbers.map{|phone_number| phone_number.valid?}.include?(true)
  end
end