Class: Occams::Admin::Cms::FilesController

Inherits:
BaseController
  • Object
show all
Includes:
ActionView::Helpers::NumberHelper, ReorderAction
Defined in:
app/controllers/occams/admin/cms/files_controller.rb

Instance Method Summary collapse

Methods included from ReorderAction

#reorder

Instance Method Details

#createObject



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
88
89
90
91
# File 'app/controllers/occams/admin/cms/files_controller.rb', line 53

def create
  categories_scope = @site.categories.of_type('Occams::Cms::File')

  if params[:categories]
    ids = categories_scope.where(label: params[:categories]).pluck(:id)
    @file.category_ids = ids
  end

  # Automatically tagging upload if it's done through redactor
  if params[:source] == 'redactor'
    category = categories_scope.find_or_create_by(label: 'wysiwyg')
    @file.category_ids = [category.id]
  end

  @file.save!

  case params[:source]
  when 'plupload'
    render partial: 'file', object: @file
  when 'redactor'
    render json: {
      filelink: url_for(@file.attachment),
      filename: @file.attachment.filename
    }
  else
    flash[:success] = I18n.t('occams.admin.cms.files.created')
    redirect_to action: :edit, id: @file
  end
rescue ActiveRecord::RecordInvalid
  case params[:source]
  when 'plupload'
    render body: @file.errors.full_messages.to_sentence, status: :unprocessable_entity
  when 'redactor'
    render body: nil, status: :unprocessable_entity
  else
    flash.now[:danger] = I18n.t('occams.admin.cms.files.creation_failure')
    render action: :new
  end
end

#destroyObject



107
108
109
110
111
112
113
114
115
116
# File 'app/controllers/occams/admin/cms/files_controller.rb', line 107

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

#editObject



93
94
95
# File 'app/controllers/occams/admin/cms/files_controller.rb', line 93

def edit
  render
end

#indexObject



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
# File 'app/controllers/occams/admin/cms/files_controller.rb', line 13

def index
  files_scope = @site.files.with_attached_attachment

  case params[:source]

  # Integration with Redactor 1.0 Wysiwyg
  when 'redactor'
    file_scope  = files_scope.limit(100).order(:position)
    file_hashes =
      case params[:type]
      when 'image'
        file_scope.with_images.collect do |file|
          { thumb: url_for(file.attachment.variant(combine_options: Occams::Cms::File::VARIANT_SIZE[:redactor])),
            image: url_for(file.attachment),
            title: file.label }
        end
      else
        file_scope.collect do |file|
          { title: file.label,
            name: file.attachment.filename,
            link: url_for(file.attachment),
            size: number_to_human_size(file.attachment.byte_size) }
        end
      end

    render json: file_hashes

  else
    files_scope = files_scope
      .includes(:categories)
      .for_category(params[:categories])
      .order('occams_cms_files.position')
    @files = occams_paginate(files_scope, per_page: 50)
  end
end

#newObject



49
50
51
# File 'app/controllers/occams/admin/cms/files_controller.rb', line 49

def new
  render
end

#updateObject



97
98
99
100
101
102
103
104
105
# File 'app/controllers/occams/admin/cms/files_controller.rb', line 97

def update
  if @file.update(file_params)
    flash[:success] = I18n.t('occams.admin.cms.files.updated')
    redirect_to action: :edit, id: @file
  else
    flash.now[:danger] = I18n.t('occams.admin.cms.files.update_failure')
    render action: :edit
  end
end