Class: CmsAdmin::FilesController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/cms_admin/files_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



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
# File 'app/controllers/cms_admin/files_controller.rb', line 26

def create
  @files = []
  
  # Sometimes params[:file] comes in as a single file object
  unless params[:file].is_a?(Hash)
    uploaded_file = params[:file]
    params[:file] = { }
    params[:file][:file] = [uploaded_file]
  end
  
  file_array  = params[:file][:file] || [nil]
  label       = params[:file][:label]
      
  file_array.each_with_index do |file, i|
    file_params = params[:file].merge(:file => file)
    if file_array.size > 1 && file_params[:label].present?
      label = file_params[:label] + " #{i + 1}"
    end
    @file = @site.files.create!(file_params.merge(:label => label))
    @files << @file
  end
  
  if params[:ajax]
    view = render_to_string(:partial => 'cms_admin/files/file', :collection => @files, :layout => false)
    render :json => {:filelink => @file.file.url, :view => view.gsub("\n", '')}
  else
    flash[:success] = I18n.t('cms.files.created')
    redirect_to :action => :edit, :id => @file
  end
  
rescue ActiveRecord::RecordInvalid
  logger.detailed_error($!)
  if params[:ajax]
    render :nothing => true, :status => :unprocessable_entity
  else
    flash.now[:error] = I18n.t('cms.files.creation_failure')
    render :action => :new
  end
end

#destroyObject



76
77
78
79
80
81
82
83
84
85
# File 'app/controllers/cms_admin/files_controller.rb', line 76

def destroy
  @file.destroy
  respond_to do |format|
    format.js
    format.html do
      flash[:success] = I18n.t('cms.files.deleted')
      redirect_to :action => :index
    end
  end
end

#indexObject



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/controllers/cms_admin/files_controller.rb', line 8

def index
  @files = @site.files.includes(:categories).for_category(params[:category]).order('cms_files.position')
  
  if params[:ajax]
    files = @files.images.collect do |file|
      { :thumb  => file.file.url(:cms_thumb),
        :image  => file.file.url }
    end
    render :json => files
  else
    return redirect_to :action => :new if @site.files.count == 0
  end
end

#newObject



22
23
24
# File 'app/controllers/cms_admin/files_controller.rb', line 22

def new
  render
end

#reorderObject



87
88
89
90
91
92
93
94
# File 'app/controllers/cms_admin/files_controller.rb', line 87

def reorder
  (params[:cms_file] || []).each_with_index do |id, index|
    if (cms_file = Cms::File.find_by_id(id))
      cms_file.update_attributes(:position => index)
    end
  end
  render :nothing => true
end

#updateObject



66
67
68
69
70
71
72
73
74
# File 'app/controllers/cms_admin/files_controller.rb', line 66

def update
  @file.update_attributes!(params[:file])
  flash[:success] = I18n.t('cms.files.updated')
  redirect_to :action => :edit, :id => @file
rescue ActiveRecord::RecordInvalid
  logger.detailed_error($!)
  flash.now[:error] = I18n.t('cms.files.update_failure')
  render :action => :edit
end