Class: Comfy::Admin::Cms::FilesController

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

Instance Method Summary collapse

Methods included from ReorderAction

#reorder

Instance Method Details

#createObject



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
92
93
# File 'app/controllers/comfy/admin/cms/files_controller.rb', line 54

def create
  categories_scope = @site.categories.of_type("Comfy::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("comfy.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("comfy.admin.cms.files.creation_failure")
    render action: :new
  end
end

#destroyObject



109
110
111
112
113
114
115
116
117
118
# File 'app/controllers/comfy/admin/cms/files_controller.rb', line 109

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

#editObject



95
96
97
# File 'app/controllers/comfy/admin/cms/files_controller.rb', line 95

def edit
  render
end

#indexObject



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

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: Comfy::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("comfy_cms_files.position")
    @files = comfy_paginate(files_scope, per_page: 50)
  end
end

#newObject



50
51
52
# File 'app/controllers/comfy/admin/cms/files_controller.rb', line 50

def new
  render
end

#updateObject



99
100
101
102
103
104
105
106
107
# File 'app/controllers/comfy/admin/cms/files_controller.rb', line 99

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