diff options
author | Peter Kozak <spag@golwen.net> | 2013-03-26 15:51:35 +0100 |
---|---|---|
committer | Peter Kozak <spag@golwen.net> | 2013-03-26 15:51:35 +0100 |
commit | 3833ac1506fb336cd617ec41d25e35c34e74064e (patch) | |
tree | 7f865a6529ae42a7e878b34eca8f99b7e65575f2 /app/models | |
parent | 78948fc995786dcd1beae8e98573ec8ccba3ae9d (diff) |
voicemail refactored
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/sip_account.rb | 24 | ||||
-rw-r--r-- | app/models/user.rb | 2 | ||||
-rw-r--r-- | app/models/voicemail_account.rb | 20 | ||||
-rw-r--r-- | app/models/voicemail_setting.rb | 52 |
4 files changed, 71 insertions, 27 deletions
diff --git a/app/models/sip_account.rb b/app/models/sip_account.rb index 0c923be..77fe87a 100644 --- a/app/models/sip_account.rb +++ b/app/models/sip_account.rb @@ -6,7 +6,8 @@ class SipAccount < ActiveRecord::Base attr_accessible :auth_name, :caller_name, :password, :voicemail_pin, :tenant_id, :call_waiting, :clir, :clip_no_screening, :clip, :description, :callforward_rules_act_per_sip_account, - :hotdeskable, :gs_node_id, :language_code + :hotdeskable, :gs_node_id, :language_code, :voicemail_account_id + # Associations: # @@ -27,8 +28,6 @@ class SipAccount < ActiveRecord::Base has_many :call_histories, :as => :call_historyable, :dependent => :destroy - has_one :voicemail_setting, :class_name => "VoicemailSetting", :primary_key => 'auth_name', :foreign_key => 'username', :dependent => :destroy - belongs_to :gs_node belongs_to :language, :foreign_key => 'language_code', :primary_key => 'code' @@ -44,6 +43,9 @@ class SipAccount < ActiveRecord::Base has_many :acd_agents, :as => :destination, :dependent => :destroy has_many :switchboard_entries, :dependent => :destroy + has_many :voicemail_accounts, :as => :voicemail_accountable, :dependent => :destroy + belongs_to :voicemail_account + # Delegations: # delegate :host, :to => :sip_domain, :allow_nil => true @@ -58,15 +60,11 @@ class SipAccount < ActiveRecord::Base validate_sip_password :password - validates_format_of :voicemail_pin, :with => /[0-9]+/, - :allow_nil => true, :allow_blank => true - validates_uniqueness_of :auth_name, :scope => :sip_domain_id # Before and after hooks: # before_save :save_value_of_to_s - after_save :create_voicemail_setting, :if => :'voicemail_setting == nil' before_validation :find_and_set_tenant_id before_validation :set_sip_domain_id before_validation :convert_umlauts_in_caller_name @@ -279,18 +277,6 @@ class SipAccount < ActiveRecord::Base end end - def create_voicemail_setting - voicemail_setting = VoicemailSetting.new() - voicemail_setting.username = self.auth_name - voicemail_setting.domain = self.sip_domain.try(:host) - voicemail_setting.password = self.voicemail_pin - voicemail_setting.notify = true - voicemail_setting.attachment = true - voicemail_setting.mark_read = true - voicemail_setting.purge = false - voicemail_setting.save - end - def create_default_group_memberships default_groups = Hash.new() templates = GsParameter.get('SipAccount', 'group', 'default') diff --git a/app/models/user.rb b/app/models/user.rb index b74fc06..81d1622 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -94,6 +94,8 @@ class User < ActiveRecord::Base has_many :switchboards, :dependent => :destroy + has_many :voicemail_accounts, :as => :voicemail_accountable, :dependent => :destroy + # Avatar like photo mount_uploader :image, ImageUploader diff --git a/app/models/voicemail_account.rb b/app/models/voicemail_account.rb new file mode 100644 index 0000000..02dc283 --- /dev/null +++ b/app/models/voicemail_account.rb @@ -0,0 +1,20 @@ +class VoicemailAccount < ActiveRecord::Base + attr_accessible :uuid, :name, :active, :gs_node_id, :voicemail_accountable_type, :voicemail_accountable_id + + belongs_to :voicemail_accountable, :polymorphic => true + has_many :voicemail_settings + + validates :name, + :presence => true, + :uniqueness => true + + validates :voicemail_accountable_id, + :presence => true + + validates :voicemail_accountable_type, + :presence => true + + def to_s + "#{voicemail_accountable.to_s}: #{name}" + end +end diff --git a/app/models/voicemail_setting.rb b/app/models/voicemail_setting.rb index a8bb304..4a9fbc9 100644 --- a/app/models/voicemail_setting.rb +++ b/app/models/voicemail_setting.rb @@ -1,12 +1,48 @@ -class VoicemailSetting < ActiveRecord::Base - self.table_name = 'voicemail_prefs' - self.primary_key = 'username' +class VoicemailSetting < ActiveRecord::Base + CLASS_TYPES = ['String', 'Integer', 'Boolean'] + VOICEMAIL_SETTINGS = { + 'password' => { :type => 'String', :characters => /[^0-9]/, :input => :password }, + 'notify' => { :type => 'Boolean', :input => :boolean }, + 'attachment' => { :type => 'Boolean', :input => :boolean }, + 'mark_read' => { :type => 'Boolean', :input => :boolean }, + 'purge' => { :type => 'Boolean', :input => :boolean }, + 'record_length_max' => { :type => 'Integer', :input => :integer }, + 'record_length_min' => { :type => 'Integer', :input => :integer }, + 'records_max' => { :type => 'Integer', :input => :integer }, + } - attr_accessible :username, :domain, :name_path, :greeting_path, :password, :notify, :attachment, :mark_read, :purge, :sip_account + attr_accessible :voicemail_account_id, :name, :value, :class_type, :description - has_one :sip_account, :foreign_key => 'auth_name' + belongs_to :voicemail_account - validates_presence_of :username - validates_presence_of :domain - validates :username, :uniqueness => {:scope => :domain} + validates :name, + :presence => true, + :uniqueness => {:scope => :voicemail_account_id} + + validates :class_type, + :presence => true, + :inclusion => { :in => CLASS_TYPES } + + before_validation :set_class_type_and_value + + def to_s + name + end + + def set_class_type_and_value + seting_pref = VOICEMAIL_SETTINGS[self.name] + if seting_pref + self.class_type = seting_pref[:type] + case self.class_type + when 'String' + if seting_pref[:characters] && self.class_type == 'String' + self.value = self.value.to_s.gsub(seting_pref[:characters], '') + end + when 'Integer' + self.value = self.value.to_i + when 'Boolean' + self.value = ActiveRecord::ConnectionAdapters::Column.value_to_boolean(self.value) + end + end + end end |