summaryrefslogtreecommitdiff
path: root/app/controllers/generic_files_controller.rb
blob: 69e00e1d8f0e8dbab680b16ef774d6d3cf46dbc2 (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
76
77
78
79
80
81
82
83
84
85
86
87
class GenericFilesController < 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 :generic_file

  load_and_authorize_resource :generic_file, :through => [:sip_account, :conference, :hunt_group, :automatic_call_distributor, :user, :tenant]

  before_filter :set_and_authorize_parent
  before_filter :spread_breadcrumbs

  def index
    @generic_files = @parent.generic_files
  end

  def show
    respond_to do |format|
      format.html
      format.xml {render :xml => @generic_file}
      format.all {
        if request.format == @generic_file.file_type
          send_file @generic_file.file.path, :type => @generic_file.file_type, :filename => "#{@generic_file.name}.#{request.parameters[:format].to_s}"
        end
      }
    end
  end

  def new
    @generic_file = @parent.generic_files.build()
  end

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

  def edit
    @generic_file = GenericFile.find(params[:id])
  end

  def update
    @generic_file = GenericFile.find(params[:id])
    if @generic_file.update_attributes(params[:generic_file])
      m = method( :"#{@parent.class.name.underscore}_generic_files_url" )
      redirect_to m.( @parent ), :notice  => t('generic_files.controller.successfuly_updated')
    else
      render :edit
    end
  end

  def destroy
    @generic_file = GenericFile.find(params[:id])
    @generic_file.destroy
    m = method( :"#{@parent.class.name.underscore}_generic_files_url" )
    redirect_to m.( @parent ), :notice => t('generic_files.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

  def spread_breadcrumbs
    if @parent.class == User
      add_breadcrumb t("users.index.page_title"), tenant_users_path(@parent.current_tenant)
      add_breadcrumb @parent, tenant_user_path(@parent.current_tenant, @parent)
    end

    add_breadcrumb t("generic_files.index.page_title"), method( :"#{@parent.class.name.underscore}_generic_files_url" ).(@parent)

    if !@generic_file.to_s.blank?
      add_breadcrumb @generic_file
    end

  end
end