From 3833ac1506fb336cd617ec41d25e35c34e74064e Mon Sep 17 00:00:00 2001 From: Peter Kozak Date: Tue, 26 Mar 2013 15:51:35 +0100 Subject: voicemail refactored --- app/controllers/sip_accounts_controller.rb | 7 ++ app/controllers/voicemail_accounts_controller.rb | 68 +++++++++++++++++++ app/controllers/voicemail_settings_controller.rb | 85 +++++++----------------- 3 files changed, 98 insertions(+), 62 deletions(-) create mode 100644 app/controllers/voicemail_accounts_controller.rb (limited to 'app/controllers') diff --git a/app/controllers/sip_accounts_controller.rb b/app/controllers/sip_accounts_controller.rb index 0d34109..8208a29 100644 --- a/app/controllers/sip_accounts_controller.rb +++ b/app/controllers/sip_accounts_controller.rb @@ -36,6 +36,7 @@ class SipAccountsController < ApplicationController break unless SipAccount.exists?(:auth_name => @sip_account.auth_name) end @sip_account.password = SecureRandom.hex(GsParameter.get('DEFAULT_LENGTH_SIP_PASSWORD')) + possible_voicemail_accounts end def create @@ -61,6 +62,7 @@ class SipAccountsController < ApplicationController end def edit + possible_voicemail_accounts end def update @@ -101,4 +103,9 @@ class SipAccountsController < ApplicationController end end + def possible_voicemail_accounts + @possible_voicemail_accounts = @sip_account.voicemail_accounts + @possible_voicemail_accounts = @possible_voicemail_accounts + @sip_account.sip_accountable.voicemail_accounts + end + end diff --git a/app/controllers/voicemail_accounts_controller.rb b/app/controllers/voicemail_accounts_controller.rb new file mode 100644 index 0000000..7e00131 --- /dev/null +++ b/app/controllers/voicemail_accounts_controller.rb @@ -0,0 +1,68 @@ +class VoicemailAccountsController < ApplicationController + load_resource :sip_account + load_resource :conference + load_resource :hunt_group + load_resource :automatic_call_distributor + load_resource :user + load_resource :tenant + load_resource :voicemail_account + + load_and_authorize_resource :phone_number, :through => [:sip_account, :conference, :hunt_group, :automatic_call_distributor, :user, :tenant] + + before_filter :set_and_authorize_parent + + def index + @voicemail_accounts = @parent.voicemail_accounts + end + + def show + + end + + def new + @voicemail_account = @parent.voicemail_accounts.build(:active => true) + if @parent.class == SipAccount && VoicemailAccount.where(:name => @parent.auth_name).count == 0 + @voicemail_account.name = @parent.auth_name + else + @voicemail_account.name = SecureRandom.hex(GsParameter.get('DEFAULT_LENGTH_SIP_AUTH_NAME')) + end + end + + def create + @voicemail_account = @parent.voicemail_accounts.new(params[:voicemail_account]) + if @voicemail_account.save + m = method( :"#{@parent.class.name.underscore}_voicemail_accounts_url" ) + redirect_to m.( @parent ), :notice => t('voicemail_accounts.controller.successfuly_created') + else + render :new + end + end + + def edit + @voicemail_account = VoicemailAccount.find(params[:id]) + end + + def update + @voicemail_account = VoicemailAccount.find(params[:id]) + if @voicemail_account.update_attributes(params[:voicemail_account]) + m = method( :"#{@parent.class.name.underscore}_voicemail_accounts_url" ) + redirect_to m.( @parent ), :notice => t('voicemail_accounts.controller.successfuly_updated') + else + render :edit + end + end + + def destroy + @voicemail_account = VoicemailAccount.find(params[:id]) + @voicemail_account.destroy + m = method( :"#{@parent.class.name.underscore}_voicemail_accounts_url" ) + redirect_to m.( @parent ), :notice => t('voicemail_accounts.controller.successfuly_destroyed') + end + + private + def set_and_authorize_parent + @parent = @sip_account || @conference || @hunt_group || @automatic_call_distributor || @user || @tenant + + authorize! :read, @parent + end +end diff --git a/app/controllers/voicemail_settings_controller.rb b/app/controllers/voicemail_settings_controller.rb index 5de0c35..ee8e36b 100644 --- a/app/controllers/voicemail_settings_controller.rb +++ b/app/controllers/voicemail_settings_controller.rb @@ -1,92 +1,53 @@ class VoicemailSettingsController < ApplicationController - load_resource :sip_account - load_and_authorize_resource :voicemail_setting, :through => :sip_account, :singleton => true - - before_filter :set_and_authorize_parent - before_filter :spread_breadcrumbs - before_filter :voicemail_defaults, :only => [:index, :show, :new, :create, :edit] + load_and_authorize_resource :voicemail_account + load_and_authorize_resource :voicemail_setting, :through => [:voicemail_account] def index - render :edit + @voicemail_settings = @voicemail_account.voicemail_settings end def show - render :edit end def new - render :edit end def create - @sip_account = SipAccount.where(:id => params[:sip_account_id]).first - params[:voicemail_setting][:username] = @sip_account.auth_name - params[:voicemail_setting][:domain] = @sip_account.sip_domain.try(:host) - @voicemail_setting = VoicemailSetting.new(params[:voicemail_setting]) + @voicemail_setting = @voicemail_account.voicemail_settings.build(params[:voicemail_setting]) + @voicemail_setting.class_type = VoicemailSetting::VOICEMAIL_SETTINGS[@voicemail_setting.name] if @voicemail_setting.save - redirect_to sip_account_voicemail_settings_path(@sip_account), :notice => t('voicemail_settings.controller.successfuly_created') + m = method( :"#{@voicemail_account.voicemail_accountable.class.name.underscore}_voicemail_account_path" ) + redirect_to m.( @voicemail_account.voicemail_accountable, @voicemail_account ), :notice => t('voicemail_settings.controller.successfuly_created') else - render :action => 'edit' + render :new end end def edit - + @voicemail_setting = @voicemail_account.voicemail_settings.find(params[:id]) + @no_edit = { + :name => { + :input => VoicemailSetting::VOICEMAIL_SETTINGS[@voicemail_setting.name][:input], + :name => @voicemail_setting.name + }, + :description => true + } end def update + @voicemail_setting = @voicemail_account.voicemail_settings.find(params[:id]) if @voicemail_setting.update_attributes(params[:voicemail_setting]) - redirect_to sip_account_voicemail_settings_path(@sip_account), :notice => t('voicemail_settings.controller.successfuly_updated') + m = method( :"#{@voicemail_account.voicemail_accountable.class.name.underscore}_voicemail_account_path" ) + redirect_to m.( @voicemail_account.voicemail_accountable, @voicemail_account ), :notice => t('voicemail_settings.controller.successfuly_updated') else render :edit end end def destroy - - end - - private - def set_and_authorize_parent - @parent = @sip_account - - authorize! :read, @parent - - @show_path_method = method( :"#{@parent.class.name.underscore}_voicemail_setting_path" ) - @index_path_method = method( :"#{@parent.class.name.underscore}_voicemail_settings_path" ) - @new_path_method = method( :"new_#{@parent.class.name.underscore}_voicemail_setting_path" ) - @edit_path_method = method( :"edit_#{@parent.class.name.underscore}_voicemail_setting_path" ) - end - - def spread_breadcrumbs - if @parent.class == SipAccount - if @sip_account.sip_accountable.class == User - add_breadcrumb t("#{@sip_account.sip_accountable.class.name.underscore.pluralize}.index.page_title"), method( :"tenant_#{@sip_account.sip_accountable.class.name.underscore.pluralize}_path" ).(@sip_account.tenant) - add_breadcrumb @sip_account.sip_accountable, method( :"tenant_#{@sip_account.sip_accountable.class.name.underscore}_path" ).(@sip_account.tenant, @sip_account.sip_accountable) - end - add_breadcrumb t("sip_accounts.index.page_title"), method( :"#{@sip_account.sip_accountable.class.name.underscore}_sip_accounts_path" ).(@sip_account.sip_accountable) - add_breadcrumb @sip_account, method( :"#{@sip_account.sip_accountable.class.name.underscore}_sip_account_path" ).(@sip_account.sip_accountable, @sip_account) - add_breadcrumb t("voicemail_settings.index.page_title"), sip_account_voicemail_settings_path(@sip_account) - end - end - - def voicemail_defaults - storage_dir = GsParameter.where(:entity => 'voicemail', :section => 'parameters', :name => 'storage-dir').first.try(:value) - path = "#{storage_dir}/#{@sip_account.sip_domain.host}/#{@sip_account.auth_name}/" - @greeting_files = Dir.glob("#{path}*greeting*.wav").collect {|r| [ File.basename(r), File.expand_path(r) ] } - @name_files = Dir.glob("#{path}*name*.wav").collect {|r| [ File.basename(r), File.expand_path(r) ] } - - if @voicemail_setting.blank? then - @voicemail_setting = @sip_account.voicemail_setting - end - - if @voicemail_setting.blank? - @voicemail_setting = VoicemailSetting.new - @voicemail_setting.notify = true - @voicemail_setting.attachment = true - @voicemail_setting.mark_read = true - @voicemail_setting.purge = false - end + @voicemail_setting = @voicemail_account.voicemail_settings.find(params[:id]) + @voicemail_setting.destroy + m = method( :"#{@voicemail_account.voicemail_accountable.class.name.underscore}_voicemail_account_path" ) + redirect_to m.( @voicemail_account.voicemail_accountable, @voicemail_account ), :notice => t('voicemail_settings.controller.successfuly_destroyed') end - end -- cgit v1.2.3