-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathresource_controller.rb
318 lines (270 loc) · 8.36 KB
/
resource_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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# frozen_string_literal: true
class Spree::Admin::ResourceController < Spree::Admin::BaseController
include Spree::Backend::Callbacks
helper_method :new_object_url, :edit_object_url, :object_url, :collection_url
before_action :load_resource, except: :update_positions
rescue_from ActiveRecord::RecordNotFound do |exception|
resource_not_found(flash_class: exception.model.constantize)
end
rescue_from ActiveRecord::RecordInvalid, with: :resource_invalid
respond_to :html
def new
invoke_callbacks(:new_action, :before)
respond_with(@object) do |format|
format.html { render layout: !request.xhr? }
if request.xhr?
format.js { render layout: false }
end
end
end
def edit
respond_with(@object) do |format|
format.html { render layout: !request.xhr? }
if request.xhr?
format.js { render layout: false }
end
end
end
def update
invoke_callbacks(:update, :before)
if @object.update(permitted_resource_params)
invoke_callbacks(:update, :after)
respond_with(@object) do |format|
format.html do
flash[:success] = flash_message_for(@object, :successfully_updated)
redirect_to location_after_save
end
format.js { render layout: false }
end
else
invoke_callbacks(:update, :fails)
respond_with(@object) do |format|
format.html do
flash.now[:error] = @object.errors.full_messages.join(", ")
render_after_update_error
end
format.js { render layout: false }
end
end
end
def create
invoke_callbacks(:create, :before)
@object.attributes = permitted_resource_params
if @object.save
invoke_callbacks(:create, :after)
flash[:success] = flash_message_for(@object, :successfully_created)
respond_with(@object) do |format|
format.html { redirect_to location_after_save }
format.js { render layout: false }
end
else
invoke_callbacks(:create, :fails)
respond_with(@object) do |format|
format.html do
flash.now[:error] = @object.errors.full_messages.join(", ")
render_after_create_error
end
format.js { render layout: false }
end
end
end
def update_positions
ActiveRecord::Base.transaction do
positions = params[:positions]
positions.each do |id, index|
# Yes here there is a N+1 but acts_as_list use after update callback
# so we can't keep not reloaded data without create a bug
# (spec : backend/spec/controllers/spree/admin/resource_controller_spec.rb)
# "with take care of acts_as_list's after update callback" test
#
# TODO : create a global set_list_position on all concerned objects
# maybe in the acts_as_list gem
model_class.where(id:).first&.set_list_position(index)
end
end
respond_to do |format|
format.js { head :no_content }
end
end
def destroy
invoke_callbacks(:destroy, :before)
destroy_result =
if @object.respond_to?(:discard)
@object.discard
else
@object.destroy
end
if destroy_result
invoke_callbacks(:destroy, :after)
flash[:success] = flash_message_for(@object, :successfully_removed)
respond_with(@object) do |format|
format.html { redirect_to location_after_destroy }
format.js { render partial: "spree/admin/shared/destroy" }
end
else
invoke_callbacks(:destroy, :fails)
respond_with(@object) do |format|
message = @object.errors.full_messages.to_sentence
format.html do
flash[:error] = message
redirect_to location_after_destroy
end
format.js do
render status: :unprocessable_entity, plain: message
end
end
end
end
private
class << self
attr_accessor :parent_data
def belongs_to(model_name, options = {})
@parent_data ||= {}
@parent_data[:model_name] = model_name
@parent_data[:model_class] = (options[:model_class] || model_name.to_s.classify.constantize)
@parent_data[:find_by] = options[:find_by] || :id
@parent_data[:includes] = options[:includes]
end
end
def resource_not_found(flash_class: model_class, redirect_url: collection_url)
super
end
def model_class
"Spree::#{controller_name.classify}".constantize
end
def parent_model_name
self.class.parent_data[:model_name].gsub('spree/', '')
end
def object_name
controller_name.singularize
end
def load_resource
if member_action?
@object ||= load_resource_instance
# call authorize! a third time (called twice already in Admin::BaseController)
# this time we pass the actual instance so fine-grained abilities can control
# access to individual records, not just entire models.
authorize! action, @object
instance_variable_set("@#{object_name}", @object)
else
@collection ||= collection
# note: we don't call authorize here as the collection method should use
# CanCan's accessible_by method to restrict the actual records returned
instance_variable_set("@#{controller_name}", @collection)
end
end
def load_resource_instance
if new_actions.include?(action)
build_resource
elsif params[:id]
find_resource
end
end
def parent
@parent ||= self.class.parent_data[:model_class]
.includes(self.class.parent_data[:includes])
.find_by!(self.class.parent_data[:find_by] => params["#{parent_model_name}_id"])
instance_variable_set("@#{parent_model_name}", @parent)
rescue ActiveRecord::RecordNotFound => e
resource_not_found(flash_class: e.model.constantize, redirect_url: routes_proxy.polymorphic_url([:admin, parent_model_name.pluralize.to_sym]))
end
def parent?
self.class.parent_data.present?
end
def find_resource
if parent?
parent.send(controller_name).find(params[:id])
else
model_class.find(params[:id])
end
end
def build_resource
if parent?
parent.send(controller_name).build
else
model_class.new
end
end
def collection
return parent.send(controller_name) if parent? && parent
if model_class.respond_to?(:accessible_by) && !current_ability.has_block?(params[:action], model_class)
model_class.accessible_by(current_ability, action)
else
model_class.all
end
end
def location_after_destroy
collection_url
end
def location_after_save
collection_url
end
# URL helpers
def new_object_url(options = {})
if parent?
routes_proxy.new_polymorphic_url([:admin, parent, model_class], options)
else
routes_proxy.new_polymorphic_url([:admin, model_class], options)
end
end
def edit_object_url(object, options = {})
if parent?
routes_proxy.polymorphic_url([:edit, :admin, parent, object], options)
else
routes_proxy.polymorphic_url([:edit, :admin, object], options)
end
end
def object_url(object = nil, options = {})
target = object ? object : @object
if parent?
routes_proxy.polymorphic_url([:admin, parent, target], options)
else
routes_proxy.polymorphic_url([:admin, target], options)
end
end
def collection_url(options = {})
if parent?
routes_proxy.polymorphic_url([:admin, parent, model_class], options)
else
routes_proxy.polymorphic_url([:admin, model_class], options)
end
end
def routes_proxy
spree
end
# Allow all attributes to be updatable.
#
# Other controllers can, should, override it to set custom logic
def permitted_resource_params
params[object_name].present? ? params.require(object_name).permit! : ActionController::Parameters.new.permit!
end
def collection_actions
[:index]
end
def member_action?
!collection_actions.include? action
end
def new_actions
[:new, :create]
end
def render_after_create_error
render action: 'new'
end
def render_after_update_error
render action: 'edit'
end
def resource_invalid(exception)
invoke_callbacks(action, :fails)
respond_with(@object) do |format|
format.html do
flash.now[:error] = exception.message
if @object.new_record?
render_after_create_error
else
render_after_update_error
end
end
format.js { render layout: false }
end
end
end