summaryrefslogtreecommitdiff
path: root/app/controllers/access_authorizations_controller.rb
blob: 54365e7d5ec94d2628e8206f7a07711590f599ab (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 AccessAuthorizationsController < ApplicationController
  load_and_authorize_resource :callthrough
  load_and_authorize_resource :access_authorization, :through => [:callthrough]

  before_filter :set_parent_and_path_methods
  before_filter :spread_breadcrumbs

  def index
  end

  def show
  end

  def new
    @access_authorization = @parent.access_authorizations.build
    @access_authorization.name = generate_a_new_name(@parent, @access_authorization)
    @access_authorization.phone_numbers.build
    @access_authorization.login = random_pin + random_pin
    @access_authorization.pin = random_pin
  end

  def create
    @access_authorization = @parent.access_authorizations.build(params[:access_authorization])
    if @access_authorization.save
      redirect_to @show_path_method.(@parent, @access_authorization), :notice => t('access_authorizations.controller.successfuly_created')
    else
      render :new
    end
  end

  def edit
  end

  def update
    if @access_authorization.update_attributes(params[:access_authorization])
      redirect_to @show_path_method.(@parent, @access_authorization), :notice  => t('access_authorizations.controller.successfuly_updated')
    else
      render :edit
    end
  end

  def destroy
    @access_authorization.destroy
    redirect_to @index_path_method.(@parent), :notice => t('access_authorizations.controller.successfuly_destroyed')
  end

  private

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

  def spread_breadcrumbs
    if @callthrough
      add_breadcrumb t("#{@parent.class.name.underscore.pluralize}.index.page_title"), tenant_callthroughs_path(@callthrough.tenant)
      add_breadcrumb @callthrough, tenant_callthrough_path(@callthrough.tenant, @callthrough)
      add_breadcrumb t("access_authorizations.index.page_title"), callthrough_access_authorizations_path(@callthrough)
      if @access_authorization && !@access_authorization.new_record?
        add_breadcrumb @access_authorization, callthrough_access_authorization_path(@callthrough, @access_authorization)
      end
    end
  end

end