-
-
Notifications
You must be signed in to change notification settings - Fork 317
/
Copy pathpictures_controller.rb
180 lines (157 loc) · 4.88 KB
/
pictures_controller.rb
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
# frozen_string_literal: true
module Alchemy
module Admin
class PicturesController < Alchemy::Admin::ResourcesController
include UploaderResponses
helper 'alchemy/admin/tags'
before_action :load_resource,
only: [:show, :edit, :update, :destroy, :info]
authorize_resource class: Alchemy::Picture
def index
@size = params[:size].present? ? params[:size] : 'medium'
@query = Picture.ransack(search_filter_params[:q])
@pictures = Picture.search_by(
search_filter_params,
@query,
items_per_page
)
if in_overlay?
archive_overlay
end
end
def show
@previous = @picture.previous(params)
@next = @picture.next(params)
@assignments = @picture.essence_pictures.joins(content: {element: :page})
render action: 'show'
end
def create
@picture = Picture.new(picture_params)
@picture.name = @picture.humanized_name
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
if @picture.update(picture_params)
@message = {
body: Alchemy.t(:picture_updated_successfully, name: @picture.name),
type: 'notice'
}
else
@message = {
body: Alchemy.t(:picture_update_failed),
type: 'error'
}
end
render :update
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 request.delete? && params[:picture_ids].present?
pictures = Picture.find(params[:picture_ids])
names = []
not_deletable = []
pictures.each do |picture|
if picture.deletable?
names << picture.name
picture.destroy
else
not_deletable << picture.name
end
end
if not_deletable.any?
flash[:warn] = Alchemy.t(
"These pictures could not be deleted, because they were in use",
names: not_deletable.to_sentence
)
else
flash[:notice] = Alchemy.t("Pictures deleted successfully", names: names.to_sentence)
end
else
flash[:warn] = Alchemy.t("Could not delete Pictures")
end
rescue StandardError => e
flash[:error] = e.message
ensure
redirect_to_index
end
def destroy
name = @picture.name
@picture.destroy
flash[:notice] = Alchemy.t("Picture deleted successfully", name: name)
rescue StandardError => e
flash[:error] = e.message
ensure
redirect_to_index
end
def items_per_page
if in_overlay?
case params[: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(params[:size])
end
end
def items_per_page_options
per_page = pictures_per_page_for_size(@size)
[per_page, per_page * 2, per_page * 4]
end
private
def pictures_per_page_for_size(size)
case size
when 'small' then 60
when 'large' then 12
else
20
end
end
def in_overlay?
params[:element_id].present?
end
def archive_overlay
@content = Content.select('id').find_by(id: params[:content_id])
@element = Element.select('id').find_by(id: params[:element_id])
respond_to do |format|
format.html { render partial: 'archive_overlay' }
format.js { render action: 'archive_overlay' }
end
end
def redirect_to_index
do_redirect_to admin_pictures_path(search_filter_params)
end
def search_filter_params
@_search_filter_params ||= params.except(*COMMON_SEARCH_FILTER_EXCLUDES + [:picture_ids]).permit(
*common_search_filter_includes + [
:size,
:element_id,
:swap,
:content_id
]
)
end
def picture_params
params.require(:picture).permit(:image_file, :upload_hash, :name, :tag_list)
end
end
end
end