From 17f94f46374d60e58a00071303c9081469e7997c Mon Sep 17 00:00:00 2001 From: Stefan Wintermeyer Date: Tue, 25 Jun 2013 12:53:13 +0200 Subject: Optimized the active_calls method. Much faster now. Added a dispatchable_incoming_calls method. --- app/models/switchboard.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'app/models') diff --git a/app/models/switchboard.rb b/app/models/switchboard.rb index 095f878..73219e8 100644 --- a/app/models/switchboard.rb +++ b/app/models/switchboard.rb @@ -25,8 +25,13 @@ class Switchboard < ActiveRecord::Base } 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 @@ -36,7 +41,11 @@ class Switchboard < ActiveRecord::Base end def active_calls - self.switchboard_entries.where(:switchable => true).map{|se| se.sip_account}.uniq.map{|sip_account| sip_account.calls}.flatten + Call.where("sip_account_id = ? or b_sip_account_id = ?", self.switchable_sip_account_ids, self.switchable_sip_account_ids) + end + + def dispatchable_incoming_calls + Call.where("b_sip_account_id = ?", self.switchable_sip_account_ids) end private -- cgit v1.2.3