summaryrefslogtreecommitdiff
path: root/app/controllers/whitelists_controller.rb
blob: 0526844811c92ae7ace5bf98eea7bfd8af84d117 (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
class WhitelistsController < ApplicationController
  load_and_authorize_resource :callthrough
  load_and_authorize_resource :whitelist, :through => [:callthrough]

  before_filter :set_parent_and_path_methods
  before_filter :spread_breadcrumbs

  def index
  end

  def show
  end

  def new
    @whitelist.phone_numbers.build
  end

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

  def edit
  end

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

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

  private

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

  def spread_breadcrumbs
    if @parent && @parent.class == Callthrough
        add_breadcrumb t("#{@parent.class.name.underscore.pluralize}.name").pluralize, tenant_callthroughs_path(@parent.tenant)
        add_breadcrumb @callthrough, tenant_callthrough_path(@parent.tenant, @callthrough)
        add_breadcrumb t("whitelists.index.page_title"), callthrough_whitelists_path(@parent)
    end
  end

end