summaryrefslogtreecommitdiff
path: root/app/controllers/callthroughs_controller.rb
blob: f4896220bf1d73e46498cb2308ea0573ce649d81 (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
69
70
71
72
73
74
75
class CallthroughsController < ApplicationController
  load_and_authorize_resource :tenant
  load_and_authorize_resource :callthrough, :through => [:tenant]

  before_filter :set_parent_and_path_methods
  before_filter :spread_breadcrumbs

  def index
  end

  def show
  end

  def new
    @callthrough = @tenant.callthroughs.build
    @callthrough.name = generate_a_new_name(@tenant, @callthrough)
    @callthrough.phone_numbers.build
    @callthrough.access_authorizations.build(:name => "#{t('access_authorizations.name')} #{@callthrough.access_authorizations.count + 1}", :pin => random_pin).phone_numbers.build
    @callthrough.whitelists.build.phone_numbers.build
  end

  def create
    @callthrough = @tenant.callthroughs.build(params[:callthrough])
    if @callthrough.save
      redirect_to tenant_callthrough_path(@tenant, @callthrough), :notice => t('callthroughs.controller.successfuly_created')
    else
      @callthrough.phone_numbers.build if @callthrough.phone_numbers.size == 0
      render :new
    end
  end

  def edit
    @callthrough.phone_numbers.build
    @callthrough.access_authorizations.build.phone_numbers.build
    if @callthrough.whitelisted_phone_numbers.count == 0
     if @callthrough.whitelists.count == 0
       @callthrough.whitelists.build.phone_numbers.build 
     else
       @callthrough.whitelists.first.phone_numbers.build
     end      
    end
  end

  def update
    if @callthrough.update_attributes(params[:callthrough])
      redirect_to tenant_callthrough_path(@tenant, @callthrough), :notice  => t('callthroughs.controller.successfuly_updated')
    else
      render :edit
    end
  end

  def destroy
    @callthrough.destroy
    redirect_to tenant_callthroughs_path(@tenant), :notice => t('callthroughs.controller.successfuly_destroyed')
  end

  private

  def set_parent_and_path_methods
    @parent = @tenant
    @show_path_method = method( :"#{@parent.class.name.underscore}_callthrough_path" )
    @index_path_method = method( :"#{@parent.class.name.underscore}_callthroughs_path" )
    @new_path_method = method( :"new_#{@parent.class.name.underscore}_callthrough_path" )
    @edit_path_method = method( :"edit_#{@parent.class.name.underscore}_callthrough_path" )
  end

  def spread_breadcrumbs
    if @parent && @parent.class == Tenant
      add_breadcrumb t("callthroughs.name").pluralize, tenant_callthroughs_path(@parent)
      if @callthrough && !@callthrough.new_record?
        add_breadcrumb @callthrough, tenant_callthrough_path(@parent, @callthrough)
      end
    end
  end
end