summaryrefslogtreecommitdiff
path: root/app/models/switchboard_entry.rb
blob: 44de7fdd9fb5e408e2d02b3d85332d0f1891ecb5 (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
61
62
63
64
65
66
class SwitchboardEntry < ActiveRecord::Base
  # https://github.com/rails/strong_parameters
  include ActiveModel::ForbiddenAttributesProtection

  belongs_to :switchboard, :touch => true
  belongs_to :sip_account, :touch => true

  has_many :phone_numbers, :through => :sip_account

  validates :switchboard,
            :presence => true

  validates :sip_account,
            :presence => true

  validates :name,
            :length => { :maximum => 10 },
            :uniqueness => {:scope => :switchboard_id},
            :allow_blank => true,
            :allow_nil => true

  acts_as_list :scope => [ :switchboard_id ]

  default_scope order(:position)

  def to_s
    if self.name.blank? && !self.sip_account.to_s.blank?
      self.sip_account.to_s
    else
      self.name.to_s
    end
  end

  def avatar_src
    if self.sip_account.sip_accountable.class == User
      if self.sip_account.sip_accountable.image?
        self.sip_account.sip_accountable.image_url(:profile)
      else
        if self.sip_account.sip_accountable.male?
          '/assets/icons/user-male-16x.png'
        else
          '/assets/icons/user-female-16x.png'
        end
      end
    else
      nil
    end
  end

  def callstate
    if self.sip_account.call_legs.where(callstate: 'ACTIVE').any? || self.sip_account.b_call_legs.where(b_callstate: 'ACTIVE').any?
      'ACTIVE'
    else
      if self.sip_account.call_legs.where(callstate: 'EARLY').any?
        'EARLY'
      else
        if self.sip_account.call_legs.where(callstate: 'RINGING').any?
          'RINGING'
        else
          nil
        end
      end
    end
  end
  
end