summaryrefslogtreecommitdiff
path: root/app/models/switchboard.rb
blob: d62657f21dc5f2d51fab48cf43077e40e8c56776 (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
class Switchboard < ActiveRecord::Base
  # https://github.com/rails/strong_parameters
  include ActiveModel::ForbiddenAttributesProtection

  validates :name,
            :presence => true,
            :uniqueness => {:scope => :user_id}

  validates :reload_interval,
            :numericality => { :only_integer => true, 
                               :greater_than => 249,
                               :allow_nil => true 
                             }

  validates :entry_width,
            :numericality => { :only_integer => true, 
                               :greater_than => 0,
                               :less_than => 5
                             }

  validates :amount_of_displayed_phone_numbers,
            :numericality => { :only_integer => true, 
                               :greater_than_or_equal_to => 0,
                               :less_than => 20
                             }

  belongs_to :user, :touch => true

  has_many :switchboard_entries, :dependent => :destroy
  has_many :switchable_switchboard_entries, :class_name => "SwitchboardEntry", :conditions => {:switchable => true}

  has_many :sip_accounts, :through => :switchboard_entries
  has_many :switchable_sip_accounts, :source => :sip_account, :through => :switchable_switchboard_entries, :uniq => true

  has_many :phone_numbers, :through => :sip_accounts

  before_validation :convert_0_to_nil

  def to_s
    self.name.to_s
  end

  def active_calls
    Call.where("sip_account_id = ? or b_sip_account_id = ?", self.switchable_sip_account_ids, self.switchable_sip_account_ids).order(:start_stamp)
  end

  def dispatchable_incoming_calls
    Call.where("b_sip_account_id = ?", self.switchable_sip_account_ids).order(:start_stamp)
  end

  private
  def convert_0_to_nil
    if self.reload_interval == 0
      self.reload_interval = nil
    end
  end
end