From 846c6ca704e587621eab0c373693c4f52e4c8433 Mon Sep 17 00:00:00 2001 From: Stefan Wintermeyer Date: Thu, 7 Mar 2013 11:25:33 +0100 Subject: Added a different VIEW definition for sqlite3 and refactored some has_many code for the sip_account. --- app/models/sip_account.rb | 7 +- db/migrate/20130307065200_create_calls_active.rb | 99 ++++++++++++++++-------- 2 files changed, 74 insertions(+), 32 deletions(-) diff --git a/app/models/sip_account.rb b/app/models/sip_account.rb index 81b9c1c..cdb609d 100644 --- a/app/models/sip_account.rb +++ b/app/models/sip_account.rb @@ -38,7 +38,8 @@ class SipAccount < ActiveRecord::Base has_many :ringtones, :as => :ringtoneable, :dependent => :destroy - has_many :calls, :finder_sql => lambda { |s| "SELECT DISTINCT calls_active.* FROM calls_active WHERE sip_account_id = #{self.id} OR b_sip_account_id = #{self.id}" } + has_many :call_legs, :class_name => 'Call' + has_many :b_call_legs, :class_name => 'Call', :foreign_key => 'b_sip_account_id' # Delegations: # @@ -83,6 +84,10 @@ class SipAccount < ActiveRecord::Base def to_s truncate((self.caller_name || "SipAccount ID #{self.id}"), :length => GsParameter.get('TO_S_MAX_CALLER_NAME_LENGTH')) + " (#{truncate(self.auth_name, :length => GsParameter.get('TO_S_MAX_LENGTH_OF_AUTH_NAME'))}@...#{self.host.split(/\./)[2,3].to_a.join('.') if self.host })" end + + def calls + self.call_legs + self.b_call_legs + end def call_forwarding_toggle( call_forwarding_service, to_voicemail = nil ) if ! self.phone_numbers.first diff --git a/db/migrate/20130307065200_create_calls_active.rb b/db/migrate/20130307065200_create_calls_active.rb index 930bbc7..0ea877d 100644 --- a/db/migrate/20130307065200_create_calls_active.rb +++ b/db/migrate/20130307065200_create_calls_active.rb @@ -1,38 +1,75 @@ class CreateCallsActive < ActiveRecord::Migration def self.up - execute %q{CREATE VIEW calls_active AS SELECT - a.uuid AS uuid, - a.direction AS direction, - a.created_epoch AS start_stamp, - a.cid_name AS caller_id_name, - a.cid_num AS caller_id_number, - a.dest AS destination, - d.id AS sip_account_id, - d.caller_name AS sip_caller_name, - a.callee_name as callee_name, - a.callee_num as callee_number, - a.callstate AS callstate, - a.read_codec AS read_codec, - a.read_rate AS read_rate, - a.read_bit_rate AS read_bit_rate, - a.write_codec AS write_codec, - a.write_rate AS write_rate, - a.write_bit_rate AS write_bit_rate, - a.secure AS secure, - b.uuid AS b_uuid, - b.cid_name AS b_caller_id_name, - b.cid_num AS b_caller_id_number, - e.id AS b_sip_account_id, - e.caller_name AS b_sip_caller_name - FROM channels a - LEFT JOIN calls c ON a.uuid = c.caller_uuid AND a.hostname = c.hostname - LEFT JOIN channels b ON b.uuid = c.callee_uuid AND b.hostname = c.hostname - LEFT JOIN sip_accounts d ON a.presence_id LIKE CONCAT(d.auth_name, "@%") - LEFT JOIN sip_accounts e ON b.presence_id LIKE CONCAT(e.auth_name, "@%") - WHERE a.uuid = c.caller_uuid OR a.uuid NOT IN (select callee_uuid from calls)} + if ActiveRecord::Base.connection_config[:adapter] != 'sqlite3' + execute <<-SQL + CREATE VIEW calls_active AS SELECT + a.uuid AS uuid, + a.direction AS direction, + a.created_epoch AS start_stamp, + a.cid_name AS caller_id_name, + a.cid_num AS caller_id_number, + a.dest AS destination, + d.id AS sip_account_id, + d.caller_name AS sip_caller_name, + a.callee_name as callee_name, + a.callee_num as callee_number, + a.callstate AS callstate, + a.read_codec AS read_codec, + a.read_rate AS read_rate, + a.read_bit_rate AS read_bit_rate, + a.write_codec AS write_codec, + a.write_rate AS write_rate, + a.write_bit_rate AS write_bit_rate, + a.secure AS secure, + b.uuid AS b_uuid, + b.cid_name AS b_caller_id_name, + b.cid_num AS b_caller_id_number, + e.id AS b_sip_account_id, + e.caller_name AS b_sip_caller_name + FROM channels a + LEFT JOIN calls c ON a.uuid = c.caller_uuid AND a.hostname = c.hostname + LEFT JOIN channels b ON b.uuid = c.callee_uuid AND b.hostname = c.hostname + LEFT JOIN sip_accounts d ON a.presence_id LIKE CONCAT(d.auth_name, "@%") + LEFT JOIN sip_accounts e ON b.presence_id LIKE CONCAT(e.auth_name, "@%") + WHERE a.uuid = c.caller_uuid OR a.uuid NOT IN (select callee_uuid from calls) + SQL + else + execute <<-SQL + CREATE VIEW calls_active AS SELECT + a.uuid AS uuid, + a.direction AS direction, + a.created_epoch AS start_stamp, + a.cid_name AS caller_id_name, + a.cid_num AS caller_id_number, + a.dest AS destination, + d.id AS sip_account_id, + d.caller_name AS sip_caller_name, + a.callee_name as callee_name, + a.callee_num as callee_number, + a.callstate AS callstate, + a.read_codec AS read_codec, + a.read_rate AS read_rate, + a.read_bit_rate AS read_bit_rate, + a.write_codec AS write_codec, + a.write_rate AS write_rate, + a.write_bit_rate AS write_bit_rate, + a.secure AS secure, + b.uuid AS b_uuid, + b.cid_name AS b_caller_id_name, + b.cid_num AS b_caller_id_number, + e.id AS b_sip_account_id, + e.caller_name AS b_sip_caller_name + FROM channels a + LEFT JOIN calls c ON a.uuid = c.caller_uuid AND a.hostname = c.hostname + LEFT JOIN channels b ON b.uuid = c.callee_uuid AND b.hostname = c.hostname + LEFT JOIN sip_accounts d ON a.presence_id LIKE (d.auth_name || "@%") + LEFT JOIN sip_accounts e ON b.presence_id LIKE (e.auth_name || "@%") + WHERE a.uuid = c.caller_uuid OR a.uuid NOT IN (select callee_uuid from calls) + SQL + end end def self.down execute "DROP VIEW calls_active" end -end +end \ No newline at end of file -- cgit v1.2.3