-
-
Notifications
You must be signed in to change notification settings - Fork 324
Expand file tree
/
Copy pathpictures_controller.rb
More file actions
251 lines (214 loc) · 6.74 KB
/
Copy pathpictures_controller.rb
File metadata and controls
251 lines (214 loc) · 6.74 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# frozen_string_literal: true
module Alchemy
module Admin
class PicturesController < Alchemy::Admin::ResourcesController
include UploaderResponses
include ArchiveOverlay
include CurrentLanguage
include PictureDescriptionsFormHelper
before_action :load_resource,
only: [:edit, :update, :url, :destroy]
before_action :set_size, only: [:index, :show, :edit_multiple, :update]
authorize_resource class: Alchemy::Picture
before_action(only: :assign) do
@picture = Picture.find(params[:id])
end
add_alchemy_filter :by_file_format, type: :select, options: ->(_query, params) do
case params&.to_h
in {except:}
Alchemy::Picture.file_formats - Alchemy::Picture.file_formats(from_extensions: except)
in {only:}
Alchemy::Picture.file_formats(from_extensions: only)
else
Alchemy::Picture.file_formats
end
end
add_alchemy_filter :recent, type: :checkbox
add_alchemy_filter :last_upload, type: :checkbox
add_alchemy_filter :without_tag, type: :checkbox
add_alchemy_filter :deletable, type: :checkbox
helper_method :picture_offset
def index
@pictures = filtered_pictures(page: params[:page])
if in_overlay?
archive_overlay
end
end
def show
@pictures = filtered_pictures(page: params[:picture_index], per_page: 1)
@picture = @pictures.first
@previous = @pictures.prev_page
@next = @pictures.next_page
@assignments = @picture.related_ingredients.joins(element: :page).merge(PageVersion.draft)
@picture_description = @picture.descriptions.find_or_initialize_by(
language_id: Alchemy::Current.language.id
)
render action: "show"
end
def url
options = picture_url_params.to_h.symbolize_keys.transform_values! do |value|
value.in?(%w[true false]) ? value == "true" : value
end
render json: {
url: @picture.url(options),
alt: @picture.name,
title: Alchemy.t(:image_name, name: @picture.name)
}
end
def create
@picture = Picture.new(picture_params)
if @picture.save
render successful_uploader_response(file: @picture)
else
render failed_uploader_response(file: @picture)
end
end
def edit_multiple
@pictures = Picture.where(id: params[:picture_ids])
@tags = @pictures.collect(&:tag_list).flatten.uniq.join(", ")
end
def update
@message = if @picture.update(picture_params)
{
body: Alchemy.t(:picture_updated_successfully, name: @picture.name),
type: "notice"
}
else
{
body: Alchemy.t(:picture_update_failed),
type: "error"
}
end
render :update, status: (@message[:type] == "notice") ? 200 : 422
end
def update_multiple
@pictures = Picture.find(params[:picture_ids])
@pictures.each do |picture|
picture.update_name_and_tag_list!(params)
end
flash[:notice] = Alchemy.t("Pictures updated successfully")
redirect_to_index
end
def delete_multiple
if params[:picture_ids].present?
params[:picture_ids].each { DeletePictureJob.perform_later(_1) }
flash[:notice] = Alchemy.t(:pictures_will_be_deleted_now)
else
flash[:warn] = Alchemy.t("Could not delete Pictures")
end
redirect_to_index
end
def destroy
name = @picture.name
@picture.destroy
flash[:notice] = Alchemy.t("Picture deleted successfully", name: name)
rescue => e
flash[:error] = e.message
ensure
redirect_to_index
end
def filtered_pictures(page: 1, per_page: items_per_page)
@query = Picture.ransack(search_filter_params[:q])
@query.sorts = default_sort_order if @query.sorts.empty?
pictures = @query.result
if params[:tagged_with].present?
pictures = pictures.tagged_with(params[:tagged_with])
end
pictures = pictures.page(page).per(per_page)
Alchemy.storage_adapter.preloaded_pictures(pictures)
end
def default_sort_order
"created_at desc"
end
def items_per_page
if in_overlay?
case @size
when "small" then 25
when "large" then 4
else
9
end
else
cookies[:alchemy_pictures_per_page] = params[:per_page] ||
cookies[:alchemy_pictures_per_page] ||
pictures_per_page_for_size
end.to_i
end
def items_per_page_options
per_page = pictures_per_page_for_size
[per_page, per_page * 2, per_page * 4]
end
private
def picture_offset
((params[:page] || 1).to_i - 1) * items_per_page
end
def set_size
@size = params[:size] || session[:alchemy_pictures_size] || "medium"
session[:alchemy_pictures_size] = @size
end
def pictures_per_page_for_size
case @size
when "small" then 60
when "large" then 12
else
20
end
end
def redirect_to_index
do_redirect_to admin_pictures_path(search_filter_params)
end
def search_filter_params
@_search_filter_params ||= begin
params[:q] ||= ActionController::Parameters.new
if params[:only].present?
params[:q][:by_file_format] ||= Picture.file_formats(from_extensions: params[:only])
end
if params[:except].present?
params[:q][:by_file_format] ||= Picture.file_formats - Picture.file_formats(from_extensions: params[:except])
end
params.except(*COMMON_SEARCH_FILTER_EXCLUDES + [:picture_ids]).permit(
*common_search_filter_includes + [
:size,
:form_field_id,
{only: []},
{except: []}
]
)
end
end
def permitted_ransack_search_fields
super + [
{by_file_format: []}
]
end
def picture_params
params.require(:picture).permit(
:image_file,
:upload_hash,
:name,
{
descriptions_attributes: [
:id,
:text,
:language_id,
:picture_id
]
},
:tag_list
)
end
def picture_url_params
params.permit(
:crop_from,
:crop_size,
:crop,
:flatten,
:format,
:quality,
:size,
:upsample
)
end
end
end
end