summaryrefslogtreecommitdiff
path: root/app/controllers/ringtones_controller.rb
blob: f7266503bb03cad48a0900d3cfa6b2467f712c0c (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
67
68
class RingtonesController < ApplicationController
  load_resource :phone_number
  load_resource :sip_account
  load_resource :boss_assistant_cooperation
  load_and_authorize_resource :ringtone, :through => [:phone_number, :sip_account, :boss_assistant_cooperation]
  
  before_filter :set_parent
  before_filter :spread_breadcrumbs

  def index
  end

  def show
  end

  def new
    @ringtone = @parent.ringtones.build
  end

  def create
    @ringtone = @parent.ringtones.build(params[:ringtone])
    if @ringtone.save
      redirect_to method( :"#{@parent.class.name.underscore}_ringtones_url" ).(@parent), :notice => t('ringtones.controller.successfuly_created')
    else
      render :new
    end
  end

  def edit
  end

  def update
    if @ringtone.update_attributes(params[:ringtone])
      redirect_to method( :"#{@parent.class.name.underscore}_ringtones_url" ).(@parent), :notice  => t('ringtones.controller.successfuly_updated')
    else
      render :edit
    end
  end

  def destroy
    @ringtone.destroy
    redirect_to method( :"#{@parent.class.name.underscore}_ringtones_url" ).(@parent), :notice => t('ringtones.controller.successfuly_destroyed')
  end

  private
  def set_parent
    @parent = @phone_number || @boss_assistant_cooperation || @sip_account
  end

  def spread_breadcrumbs
    if @parent.class == PhoneNumber && @parent.phone_numberable.class == SipAccount
      @sip_account = @parent.phone_numberable
      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("phone_numbers.index.page_title"), sip_account_phone_numbers_path(@sip_account)
      add_breadcrumb @phone_number, sip_account_phone_number_path(@sip_account, @phone_number)
      add_breadcrumb t("ringtones.index.page_title"), phone_number_ringtones_path(@phone_number)
      if @ringtone && !@ringtone.new_record?
        add_breadcrumb @ringtone, phone_number_ringtone_path(@phone_number, @ringtone)
      end
    end
  end

end