effective_resources 0.9.5 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/controllers/concerns/effective/crud_controller.rb +23 -546
- data/app/controllers/concerns/effective/crud_controller/actions.rb +289 -0
- data/app/controllers/concerns/effective/crud_controller/dsl.rb +81 -0
- data/app/controllers/concerns/effective/crud_controller/paths.rb +87 -0
- data/app/controllers/concerns/effective/crud_controller/permitted_params.rb +66 -0
- data/app/controllers/concerns/effective/crud_controller/save.rb +77 -0
- data/app/controllers/concerns/effective/crud_controller/submits.rb +122 -0
- data/app/helpers/effective_resources_helper.rb +51 -52
- data/app/helpers/effective_resources_private_helper.rb +69 -0
- data/app/models/concerns/effective_resource.rb +24 -0
- data/app/models/effective/model_reader.rb +29 -0
- data/app/models/effective/resource.rb +6 -2
- data/app/models/effective/resources/actions.rb +10 -10
- data/app/models/effective/resources/associations.rb +5 -0
- data/app/models/effective/resources/attributes.rb +40 -27
- data/app/models/effective/resources/controller.rb +81 -0
- data/app/models/effective/resources/forms.rb +0 -51
- data/app/models/effective/resources/init.rb +19 -17
- data/app/models/effective/resources/klass.rb +6 -8
- data/app/models/effective/resources/model.rb +23 -0
- data/app/models/effective/resources/naming.rb +7 -3
- data/app/models/effective/resources/paths.rb +4 -63
- data/app/models/effective/resources/relation.rb +4 -1
- data/app/models/effective/resources/sql.rb +1 -1
- data/app/views/application/create.js.erb +1 -1
- data/app/views/application/edit.html.haml +2 -2
- data/app/views/application/index.html.haml +1 -1
- data/app/views/application/member_action.js.erb +1 -1
- data/app/views/application/new.html.haml +3 -1
- data/app/views/application/show.html.haml +1 -1
- data/app/views/application/update.js.erb +1 -1
- data/app/views/effective/resource/_actions.html.haml +3 -32
- data/app/views/effective/resource/_actions_dropleft.html.haml +4 -26
- data/app/views/effective/resource/_actions_glyphicons.html.haml +4 -10
- data/lib/effective_resources/engine.rb +1 -0
- data/lib/effective_resources/version.rb +1 -1
- metadata +14 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6b64a2a72a925cc6d0932a5a5f60d490fbf6ae54
|
4
|
+
data.tar.gz: 65206626420879daa72dce237a217bc0193c8500
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e7283195596e3b271a42c37708d6ffc916ca38e565e9e716fcef1bb5d67bb0311ff90b40a73c7c53c6e54d587b8344be7f224435d5d6166992e81d148fa4e9c5
|
7
|
+
data.tar.gz: b28fdcca5a4eca2aeb83bff6d0100e31867a71a18eb7cd128081fc8b22a143a16a1f53c1d2a7020da4db9ceb835fc34fc970cf2d1ff39fab23fb91b0cd1f42cc
|
@@ -2,541 +2,42 @@ module Effective
|
|
2
2
|
module CrudController
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
def submits
|
12
|
-
effective_resource.submits
|
13
|
-
end
|
14
|
-
end
|
5
|
+
include Effective::CrudController::Actions
|
6
|
+
include Effective::CrudController::Paths
|
7
|
+
include Effective::CrudController::PermittedParams
|
8
|
+
include Effective::CrudController::Save
|
9
|
+
include Effective::CrudController::Submits
|
15
10
|
|
11
|
+
included do
|
16
12
|
define_actions_from_routes
|
13
|
+
define_permitted_params_from_model
|
17
14
|
define_callbacks :resource_render, :resource_save, :resource_error
|
18
15
|
end
|
19
16
|
|
20
17
|
module ClassMethods
|
18
|
+
include Effective::CrudController::Dsl
|
21
19
|
|
22
|
-
|
23
|
-
|
24
|
-
resource = Effective::Resource.new(controller_path)
|
25
|
-
(resource.member_actions - resource.crud_actions).each { |action| member_action(action) }
|
26
|
-
(resource.collection_actions - resource.crud_actions).each { |action| collection_action(action) }
|
27
|
-
end
|
28
|
-
|
29
|
-
# https://github.com/rails/rails/blob/v5.1.4/actionpack/lib/abstract_controller/callbacks.rb
|
30
|
-
def before_render(*names, &blk)
|
31
|
-
_insert_callbacks(names, blk) { |name, options| set_callback(:resource_render, :before, name, options) }
|
32
|
-
end
|
33
|
-
|
34
|
-
def after_save(*names, &blk)
|
35
|
-
_insert_callbacks(names, blk) { |name, options| set_callback(:resource_save, :after, name, options) }
|
36
|
-
end
|
37
|
-
|
38
|
-
def after_error(*names, &blk)
|
39
|
-
_insert_callbacks(names, blk) { |name, options| set_callback(:resource_error, :after, name, options) }
|
40
|
-
end
|
41
|
-
|
42
|
-
# This controls the form submit options of effective_submit
|
43
|
-
# It also controls the redirect path for any actions
|
44
|
-
#
|
45
|
-
# Effective::Resource will populate this with all member_post_actions
|
46
|
-
# And you can control the details with this DSL:
|
47
|
-
#
|
48
|
-
# submit :approve, 'Save and Approve', unless: -> { approved? }, redirect: :show
|
49
|
-
#
|
50
|
-
# submit :toggle, 'Blacklist', if: -> { sync? }, class: 'btn btn-primary'
|
51
|
-
# submit :toggle, 'Whitelist', if: -> { !sync? }, class: 'btn btn-primary'
|
52
|
-
# submit :save, 'Save', success: -> { "#{self} was saved okay!" }
|
53
|
-
|
54
|
-
def submit(action, commit = nil, args = {})
|
55
|
-
raise 'expected args to be a Hash or false' unless args.kind_of?(Hash) || args == false
|
56
|
-
|
57
|
-
if commit == false
|
58
|
-
submits.delete_if { |commit, args| args[:action] == action }; return
|
59
|
-
end
|
60
|
-
|
61
|
-
if args == false
|
62
|
-
submits.delete(commit); return
|
63
|
-
end
|
64
|
-
|
65
|
-
if commit # Overwrite the default member action when given a custom commit
|
66
|
-
submits.delete_if { |commit, args| args[:default] && args[:action] == action }
|
67
|
-
end
|
68
|
-
|
69
|
-
if args.key?(:if) && args[:if].respond_to?(:call) == false
|
70
|
-
raise "expected if: to be callable. Try submit :approve, 'Save and Approve', if: -> { finished? }"
|
71
|
-
end
|
72
|
-
|
73
|
-
if args.key?(:unless) && args[:unless].respond_to?(:call) == false
|
74
|
-
raise "expected unless: to be callable. Try submit :approve, 'Save and Approve', unless: -> { declined? }"
|
75
|
-
end
|
76
|
-
|
77
|
-
redirect = args.delete(:redirect_to) || args.delete(:redirect) # Remove redirect_to keyword. use redirect.
|
78
|
-
args.merge!(action: action, redirect: redirect)
|
79
|
-
|
80
|
-
(submits[commit] ||= {}).merge!(args)
|
81
|
-
end
|
82
|
-
|
83
|
-
# page_title 'My Title', only: [:new]
|
84
|
-
def page_title(label = nil, opts = {}, &block)
|
85
|
-
opts = label if label.kind_of?(Hash)
|
86
|
-
raise 'expected a label or block' unless (label || block_given?)
|
87
|
-
|
88
|
-
instance_exec do
|
89
|
-
before_action(opts) do
|
90
|
-
@page_title ||= (block_given? ? instance_exec(&block) : label).to_s
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
# resource_scope -> { current_user.things }
|
96
|
-
# resource_scope -> { Thing.active.where(user: current_user) }
|
97
|
-
# resource_scope do
|
98
|
-
# { user_id: current_user.id }
|
99
|
-
# end
|
100
|
-
# Nested controllers? sure
|
101
|
-
# resource_scope -> { User.find(params[:user_id]).things }
|
102
|
-
|
103
|
-
# Return value should be:
|
104
|
-
# a Relation: Thing.where(user: current_user)
|
105
|
-
# a Hash: { user_id: current_user.id }
|
106
|
-
def resource_scope(obj = nil, opts = {}, &block)
|
107
|
-
raise 'expected a proc or block' unless (obj.respond_to?(:call) || block_given?)
|
108
|
-
|
109
|
-
instance_exec do
|
110
|
-
before_action(opts) do
|
111
|
-
@_effective_resource_scope ||= instance_exec(&(block_given? ? block : obj))
|
112
|
-
end
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
|
-
# Defines a function to handle a GET and POST request on this URL
|
117
|
-
# Just add a member action to your routes, you shouldn't need to call this directly
|
118
|
-
def member_action(action)
|
119
|
-
define_method(action) do
|
120
|
-
Rails.logger.info 'Processed by Effective::CrudController#member_action'
|
121
|
-
|
122
|
-
self.resource ||= resource_scope.find(params[:id])
|
123
|
-
|
124
|
-
EffectiveResources.authorize!(self, action, resource)
|
125
|
-
|
126
|
-
@page_title ||= "#{action.to_s.titleize} #{resource}"
|
127
|
-
|
128
|
-
request.get? ? run_callbacks(:resource_render) : member_post_action(action)
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
# Defines a function to handle a GET and POST request on this URL
|
133
|
-
# Handles bulk_ actions
|
134
|
-
# Just add a member action to your routes, you shouldn't need to call this directly
|
135
|
-
# You shouldn't need to call this directly
|
136
|
-
def collection_action(action)
|
137
|
-
define_method(action) do
|
138
|
-
Rails.logger.info 'Processed by Effective::CrudController#collection_action'
|
139
|
-
|
140
|
-
if params[:ids].present?
|
141
|
-
self.resources ||= resource_scope.where(id: params[:ids])
|
142
|
-
end
|
143
|
-
|
144
|
-
if effective_resource.scope?(action)
|
145
|
-
self.resources ||= resource_scope.public_send(action)
|
146
|
-
end
|
147
|
-
|
148
|
-
self.resources ||= resource_scope.all
|
149
|
-
|
150
|
-
EffectiveResources.authorize!(self, action, resource_klass)
|
151
|
-
|
152
|
-
@page_title ||= "#{action.to_s.titleize} #{resource_plural_name.titleize}"
|
153
|
-
|
154
|
-
request.get? ? run_callbacks(:resource_render) : collection_post_action(action)
|
155
|
-
end
|
156
|
-
end
|
157
|
-
end
|
158
|
-
|
159
|
-
def index
|
160
|
-
Rails.logger.info 'Processed by Effective::CrudController#index'
|
161
|
-
|
162
|
-
@page_title ||= resource_plural_name.titleize
|
163
|
-
EffectiveResources.authorize!(self, :index, resource_klass)
|
164
|
-
|
165
|
-
self.resources ||= resource_scope.all
|
166
|
-
|
167
|
-
if resource_datatable_class
|
168
|
-
@datatable ||= resource_datatable_class.new(resource_datatable_attributes)
|
169
|
-
@datatable.view = view_context
|
170
|
-
end
|
171
|
-
|
172
|
-
run_callbacks(:resource_render)
|
173
|
-
end
|
174
|
-
|
175
|
-
def new
|
176
|
-
Rails.logger.info 'Processed by Effective::CrudController#new'
|
177
|
-
|
178
|
-
self.resource ||= resource_scope.new
|
179
|
-
|
180
|
-
self.resource.assign_attributes(
|
181
|
-
params.to_unsafe_h.except(:controller, :action, :id).select { |k, v| resource.respond_to?("#{k}=") }
|
182
|
-
)
|
183
|
-
|
184
|
-
if params[:duplicate_id]
|
185
|
-
duplicate = resource_scope.find(params[:duplicate_id])
|
186
|
-
EffectiveResources.authorize!(self, :show, duplicate)
|
187
|
-
|
188
|
-
self.resource = duplicate_resource(duplicate)
|
189
|
-
raise "expected duplicate_resource to return an unsaved new #{resource_klass} resource" unless resource.kind_of?(resource_klass) && resource.new_record?
|
190
|
-
|
191
|
-
if (message = flash[:success].to_s).present?
|
192
|
-
flash.delete(:success)
|
193
|
-
flash.now[:success] = "#{message.chomp('.')}. Adding another #{resource_name.titleize} based on previous."
|
194
|
-
end
|
195
|
-
end
|
196
|
-
|
197
|
-
@page_title ||= "New #{resource_name.titleize}"
|
198
|
-
EffectiveResources.authorize!(self, :new, resource)
|
199
|
-
|
200
|
-
run_callbacks(:resource_render)
|
201
|
-
end
|
202
|
-
|
203
|
-
def create
|
204
|
-
Rails.logger.info 'Processed by Effective::CrudController#create'
|
205
|
-
|
206
|
-
self.resource ||= resource_scope.new
|
207
|
-
|
208
|
-
@page_title ||= "New #{resource_name.titleize}"
|
209
|
-
action = commit_action[:action]
|
210
|
-
|
211
|
-
resource.assign_attributes(send(resource_params_method_name))
|
212
|
-
resource.created_by ||= current_user if resource.respond_to?(:created_by=)
|
213
|
-
|
214
|
-
EffectiveResources.authorize!(self, (action == :save ? :create : action), resource)
|
215
|
-
|
216
|
-
respond_to do |format|
|
217
|
-
if save_resource(resource, action)
|
218
|
-
request.format = :html if specific_redirect_path?
|
219
|
-
|
220
|
-
format.html do
|
221
|
-
flash[:success] ||= resource_flash(:success, resource, action)
|
222
|
-
redirect_to(resource_redirect_path)
|
223
|
-
end
|
224
|
-
|
225
|
-
format.js do
|
226
|
-
flash.now[:success] ||= resource_flash(:success, resource, action)
|
227
|
-
reload_resource # create.js.erb
|
228
|
-
end
|
229
|
-
else
|
230
|
-
flash.delete(:success)
|
231
|
-
flash.now[:danger] ||= resource_flash(:danger, resource, action)
|
232
|
-
|
233
|
-
run_callbacks(:resource_render)
|
234
|
-
|
235
|
-
format.html { render :new }
|
236
|
-
format.js {} # create.js.erb
|
237
|
-
end
|
20
|
+
def effective_resource
|
21
|
+
@_effective_resource ||= Effective::Resource.new(controller_path)
|
238
22
|
end
|
239
|
-
end
|
240
|
-
|
241
|
-
def show
|
242
|
-
Rails.logger.info 'Processed by Effective::CrudController#show'
|
243
|
-
|
244
|
-
self.resource ||= resource_scope.find(params[:id])
|
245
|
-
|
246
|
-
@page_title ||= resource.to_s
|
247
|
-
EffectiveResources.authorize!(self, :show, resource)
|
248
|
-
|
249
|
-
run_callbacks(:resource_render)
|
250
|
-
end
|
251
|
-
|
252
|
-
def edit
|
253
|
-
Rails.logger.info 'Processed by Effective::CrudController#edit'
|
254
|
-
|
255
|
-
self.resource ||= resource_scope.find(params[:id])
|
256
|
-
|
257
|
-
@page_title ||= "Edit #{resource}"
|
258
|
-
EffectiveResources.authorize!(self, :edit, resource)
|
259
23
|
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
Rails.logger.info 'Processed by Effective::CrudController#update'
|
265
|
-
|
266
|
-
self.resource ||= resource_scope.find(params[:id])
|
267
|
-
|
268
|
-
@page_title = "Edit #{resource}"
|
269
|
-
|
270
|
-
action = commit_action[:action]
|
271
|
-
EffectiveResources.authorize!(self, action, resource) unless action == :save
|
272
|
-
EffectiveResources.authorize!(self, :update, resource) if action == :save
|
273
|
-
|
274
|
-
respond_to do |format|
|
275
|
-
if save_resource(resource, action, send(resource_params_method_name))
|
276
|
-
request.format = :html if specific_redirect_path?
|
277
|
-
|
278
|
-
format.html do
|
279
|
-
flash[:success] ||= resource_flash(:success, resource, action)
|
280
|
-
redirect_to(resource_redirect_path)
|
281
|
-
end
|
282
|
-
|
283
|
-
format.js do
|
284
|
-
flash.now[:success] ||= resource_flash(:success, resource, action)
|
285
|
-
reload_resource # update.js.erb
|
286
|
-
end
|
287
|
-
else
|
288
|
-
flash.delete(:success)
|
289
|
-
flash.now[:danger] ||= resource_flash(:danger, resource, action)
|
290
|
-
|
291
|
-
run_callbacks(:resource_render)
|
292
|
-
|
293
|
-
format.html { render :edit }
|
294
|
-
format.js { } # update.js.erb
|
24
|
+
# Automatically respond to any action defined via the routes file
|
25
|
+
def define_actions_from_routes
|
26
|
+
(effective_resource.member_actions - effective_resource.crud_actions).each do |action|
|
27
|
+
define_method(action) { member_action(action) }
|
295
28
|
end
|
296
|
-
end
|
297
|
-
end
|
298
|
-
|
299
|
-
def destroy
|
300
|
-
Rails.logger.info 'Processed by Effective::CrudController#destroy'
|
301
|
-
|
302
|
-
self.resource = resource_scope.find(params[:id])
|
303
|
-
|
304
|
-
action = :destroy
|
305
|
-
@page_title ||= "Destroy #{resource}"
|
306
|
-
EffectiveResources.authorize!(self, action, resource)
|
307
|
-
|
308
|
-
respond_to do |format|
|
309
|
-
if save_resource(resource, action)
|
310
|
-
request.format = :html if specific_redirect_path?(action)
|
311
|
-
|
312
|
-
format.html do
|
313
|
-
flash[:success] ||= resource_flash(:success, resource, action)
|
314
|
-
redirect_to(resource_redirect_path(action))
|
315
|
-
end
|
316
|
-
|
317
|
-
format.js do
|
318
|
-
flash.now[:success] ||= resource_flash(:success, resource, action)
|
319
|
-
# destroy.js.erb
|
320
|
-
end
|
321
|
-
else
|
322
|
-
flash.delete(:success)
|
323
|
-
request.format = :html # Don't run destroy.js.erb
|
324
|
-
|
325
|
-
format.html do
|
326
|
-
flash[:danger] = (flash.now[:danger].presence || resource_flash(:danger, resource, action))
|
327
|
-
redirect_to(resource_redirect_path(action))
|
328
|
-
end
|
329
29
|
|
30
|
+
(effective_resource.collection_actions - effective_resource.crud_actions).each do |action|
|
31
|
+
define_method(action) { collection_action(action) }
|
330
32
|
end
|
331
33
|
end
|
332
|
-
end
|
333
34
|
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
respond_to do |format|
|
338
|
-
|
339
|
-
if save_resource(resource, action, (send(resource_params_method_name) rescue {}))
|
340
|
-
request.format = :html if specific_redirect_path?(action)
|
341
|
-
|
342
|
-
format.html do
|
343
|
-
flash[:success] ||= resource_flash(:success, resource, action)
|
344
|
-
redirect_to(resource_redirect_path(action))
|
345
|
-
end
|
346
|
-
|
347
|
-
format.js do
|
348
|
-
flash.now[:success] ||= resource_flash(:success, resource, action)
|
349
|
-
reload_resource
|
350
|
-
render_member_action(action)
|
351
|
-
end
|
352
|
-
else
|
353
|
-
flash.delete(:success)
|
354
|
-
flash.now[:danger] ||= resource_flash(:danger, resource, action)
|
355
|
-
|
356
|
-
run_callbacks(:resource_render)
|
357
|
-
|
358
|
-
format.html do
|
359
|
-
if resource_edit_path && (referer_redirect_path || '').end_with?(resource_edit_path)
|
360
|
-
@page_title ||= "Edit #{resource}"
|
361
|
-
render :edit
|
362
|
-
elsif resource_new_path && (referer_redirect_path || '').end_with?(resource_new_path)
|
363
|
-
@page_title ||= "New #{resource_name.titleize}"
|
364
|
-
render :new
|
365
|
-
elsif resource_show_path && (referer_redirect_path || '').end_with?(resource_show_path)
|
366
|
-
@page_title ||= resource_name.titleize
|
367
|
-
render :show
|
368
|
-
else
|
369
|
-
@page_title ||= resource.to_s
|
370
|
-
flash[:danger] = flash.now[:danger]
|
371
|
-
redirect_to(referer_redirect_path || resource_redirect_path(action))
|
372
|
-
end
|
373
|
-
end
|
374
|
-
|
375
|
-
format.js { render_member_action(action) }
|
35
|
+
def define_permitted_params_from_model
|
36
|
+
if effective_resource.model.present?
|
37
|
+
define_method(:effective_resource_permitted_params) { resource_permitted_params } # save.rb
|
376
38
|
end
|
377
39
|
end
|
378
|
-
end
|
379
|
-
|
380
|
-
# Which member javascript view to render: #{action}.js or effective_resources member_action.js
|
381
|
-
def render_member_action(action)
|
382
|
-
view = lookup_context.template_exists?(action, _prefixes) ? action : :member_action
|
383
|
-
render(view, locals: { action: action })
|
384
|
-
end
|
385
|
-
|
386
|
-
# No attributes are assigned or saved. We purely call action! on the resource
|
387
|
-
def collection_post_action(action)
|
388
|
-
action = action.to_s.gsub('bulk_', '').to_sym
|
389
|
-
|
390
|
-
raise 'expected post, patch or put http action' unless (request.post? || request.patch? || request.put?)
|
391
|
-
raise "expected #{resource_name} to respond to #{action}!" if resources.to_a.present? && !resources.first.respond_to?("#{action}!")
|
392
|
-
|
393
|
-
successes = 0
|
394
|
-
|
395
|
-
ActiveRecord::Base.transaction do
|
396
|
-
successes = resources.select do |resource|
|
397
|
-
begin
|
398
|
-
resource.public_send("#{action}!") if EffectiveResources.authorized?(self, action, resource)
|
399
|
-
rescue => e
|
400
|
-
false
|
401
|
-
end
|
402
|
-
end.length
|
403
|
-
end
|
404
40
|
|
405
|
-
render json: { status: 200, message: "Successfully #{action_verb(action)} #{successes} / #{resources.length} selected #{resource_plural_name}" }
|
406
|
-
end
|
407
|
-
|
408
|
-
protected
|
409
|
-
|
410
|
-
# This calls the appropriate member action, probably save!, on the resource.
|
411
|
-
def save_resource(resource, action = :save, to_assign = {}, &block)
|
412
|
-
raise "expected @#{resource_name} to respond to #{action}!" unless resource.respond_to?("#{action}!")
|
413
|
-
|
414
|
-
resource.current_user ||= current_user if resource.respond_to?(:current_user=)
|
415
|
-
|
416
|
-
ActiveRecord::Base.transaction do
|
417
|
-
begin
|
418
|
-
resource.assign_attributes(to_assign) if to_assign.respond_to?(:permitted?) && to_assign.permitted?
|
419
|
-
|
420
|
-
if resource.public_send("#{action}!") == false
|
421
|
-
raise("failed to #{action} #{resource}")
|
422
|
-
end
|
423
|
-
|
424
|
-
yield if block_given?
|
425
|
-
|
426
|
-
run_callbacks(:resource_save)
|
427
|
-
return true
|
428
|
-
rescue => e
|
429
|
-
Rails.logger.info "Failed to #{action}: #{e.message}" if Rails.env.development?
|
430
|
-
|
431
|
-
if resource.respond_to?(:restore_attributes) && resource.persisted?
|
432
|
-
resource.restore_attributes(['status', 'state'])
|
433
|
-
end
|
434
|
-
|
435
|
-
flash.delete(:success)
|
436
|
-
flash.now[:danger] = flash_danger(resource, action, e: e)
|
437
|
-
raise ActiveRecord::Rollback
|
438
|
-
end
|
439
|
-
end
|
440
|
-
|
441
|
-
run_callbacks(:resource_error)
|
442
|
-
false
|
443
|
-
end
|
444
|
-
|
445
|
-
def reload_resource
|
446
|
-
self.resource.reload if resource.respond_to?(:reload)
|
447
|
-
end
|
448
|
-
|
449
|
-
# Should return a new resource based on the passed one
|
450
|
-
def duplicate_resource(resource)
|
451
|
-
resource.dup
|
452
|
-
end
|
453
|
-
|
454
|
-
def resource_flash(status, resource, action)
|
455
|
-
submit = commit_action(action)
|
456
|
-
message = submit[status].respond_to?(:call) ? instance_exec(&submit[status]) : submit[status]
|
457
|
-
return message if message.present?
|
458
|
-
|
459
|
-
case status
|
460
|
-
when :success then flash_success(resource, action)
|
461
|
-
when :danger then flash_danger(resource, action)
|
462
|
-
else
|
463
|
-
raise "unknown resource flash status: #{status}"
|
464
|
-
end
|
465
|
-
end
|
466
|
-
|
467
|
-
def resource_redirect_path(action = nil)
|
468
|
-
submit = commit_action(action)
|
469
|
-
redirect = submit[:redirect].respond_to?(:call) ? instance_exec(&submit[:redirect]) : submit[:redirect]
|
470
|
-
|
471
|
-
commit_action_redirect = case redirect
|
472
|
-
when :index ; resource_index_path
|
473
|
-
when :edit ; resource_edit_path
|
474
|
-
when :show ; resource_show_path
|
475
|
-
when :new ; resource_new_path
|
476
|
-
when :duplicate ; resource_duplicate_path
|
477
|
-
when :back ; referer_redirect_path
|
478
|
-
when :save ; [resource_edit_path, resource_show_path].compact.first
|
479
|
-
when Symbol ; resource_action_path(submit[:action])
|
480
|
-
when String ; redirect
|
481
|
-
else ; nil
|
482
|
-
end
|
483
|
-
|
484
|
-
return commit_action_redirect if commit_action_redirect.present?
|
485
|
-
|
486
|
-
if action == :destroy
|
487
|
-
return [referer_redirect_path, resource_index_path, root_path].compact.first
|
488
|
-
end
|
489
|
-
|
490
|
-
case params[:commit].to_s
|
491
|
-
when 'Save'
|
492
|
-
[resource_edit_path, resource_show_path, resource_index_path]
|
493
|
-
when 'Save and Add New', 'Add New'
|
494
|
-
[resource_new_path, resource_index_path]
|
495
|
-
when 'Duplicate'
|
496
|
-
[resource_duplicate_path, resource_index_path]
|
497
|
-
when 'Continue', 'Save and Continue'
|
498
|
-
[resource_index_path]
|
499
|
-
else
|
500
|
-
[referer_redirect_path, resource_edit_path, resource_show_path, resource_index_path]
|
501
|
-
end.compact.first.presence || root_path
|
502
|
-
end
|
503
|
-
|
504
|
-
def referer_redirect_path
|
505
|
-
url = request.referer.to_s
|
506
|
-
|
507
|
-
return if (resource && resource.respond_to?(:destroyed?) && resource.destroyed? && url.include?("/#{resource.to_param}"))
|
508
|
-
return if url.include?('duplicate_id=')
|
509
|
-
return unless (Rails.application.routes.recognize_path(URI(url).path) rescue false)
|
510
|
-
|
511
|
-
url
|
512
|
-
end
|
513
|
-
|
514
|
-
def resource_index_path
|
515
|
-
effective_resource.action_path(:index)
|
516
|
-
end
|
517
|
-
|
518
|
-
def resource_new_path
|
519
|
-
effective_resource.action_path(:new)
|
520
|
-
end
|
521
|
-
|
522
|
-
def resource_duplicate_path
|
523
|
-
effective_resource.action_path(:new, duplicate_id: resource.id)
|
524
|
-
end
|
525
|
-
|
526
|
-
def resource_edit_path
|
527
|
-
effective_resource.action_path(:edit, resource)
|
528
|
-
end
|
529
|
-
|
530
|
-
def resource_show_path
|
531
|
-
effective_resource.action_path(:show, resource)
|
532
|
-
end
|
533
|
-
|
534
|
-
def resource_destroy_path
|
535
|
-
effective_resource.action_path(:destroy, resource)
|
536
|
-
end
|
537
|
-
|
538
|
-
def resource_action_path(action)
|
539
|
-
effective_resource.action_path(action.to_sym, resource)
|
540
41
|
end
|
541
42
|
|
542
43
|
def resource # @thing
|
@@ -558,7 +59,7 @@ module Effective
|
|
558
59
|
private
|
559
60
|
|
560
61
|
def effective_resource
|
561
|
-
@_effective_resource ||=
|
62
|
+
@_effective_resource ||= self.class.effective_resource
|
562
63
|
end
|
563
64
|
|
564
65
|
def resource_name # 'thing'
|
@@ -569,32 +70,10 @@ module Effective
|
|
569
70
|
effective_resource.klass
|
570
71
|
end
|
571
72
|
|
572
|
-
def resource_human_name
|
573
|
-
effective_resource.human_name
|
574
|
-
end
|
575
|
-
|
576
73
|
def resource_plural_name # 'things'
|
577
74
|
effective_resource.plural_name
|
578
75
|
end
|
579
76
|
|
580
|
-
# Based on the incoming params[:commit] or passed action
|
581
|
-
def commit_action(action = nil)
|
582
|
-
if action.present?
|
583
|
-
self.class.submits[action.to_s] ||
|
584
|
-
self.class.submits.find { |_, v| v[:action] == action }.try(:last) ||
|
585
|
-
{ action: action }
|
586
|
-
else # Get the current commit
|
587
|
-
self.class.submits[params[:commit].to_s] ||
|
588
|
-
self.class.submits.find { |_, v| v[:action] == :save }.try(:last) ||
|
589
|
-
{ action: :save }
|
590
|
-
end
|
591
|
-
end
|
592
|
-
|
593
|
-
def specific_redirect_path?(action = nil)
|
594
|
-
submit = commit_action(action)
|
595
|
-
(submit[:redirect].respond_to?(:call) ? instance_exec(&submit[:redirect]) : submit[:redirect]).present?
|
596
|
-
end
|
597
|
-
|
598
77
|
# Returns an ActiveRecord relation based on the computed value of `resource_scope` dsl method
|
599
78
|
def resource_scope # Thing
|
600
79
|
@_effective_resource_relation ||= (
|
@@ -611,9 +90,7 @@ module Effective
|
|
611
90
|
raise "expected resource_scope method to return an ActiveRecord::Relation or Hash"
|
612
91
|
end
|
613
92
|
|
614
|
-
unless relation.kind_of?(ActiveRecord::Relation)
|
615
|
-
raise("unable to build resource_scope for #{effective_resource.klass || 'unknown klass'}.")
|
616
|
-
end
|
93
|
+
raise("unable to build resource_scope for #{effective_resource.klass || 'unknown klass'}.") unless relation.kind_of?(ActiveRecord::Relation)
|
617
94
|
|
618
95
|
relation
|
619
96
|
)
|
@@ -623,12 +100,12 @@ module Effective
|
|
623
100
|
resource_scope.where_values_hash.symbolize_keys
|
624
101
|
end
|
625
102
|
|
626
|
-
def resource_datatable_class
|
103
|
+
def resource_datatable_class
|
627
104
|
effective_resource.datatable_klass
|
628
105
|
end
|
629
106
|
|
630
107
|
def resource_params_method_name
|
631
|
-
["#{resource_name}_params", "#{resource_plural_name}_params", 'permitted_params'].find { |name| respond_to?(name, true) } || 'params'
|
108
|
+
["#{resource_name}_params", "#{resource_plural_name}_params", 'permitted_params', 'effective_resource_permitted_params'].find { |name| respond_to?(name, true) } || 'params'
|
632
109
|
end
|
633
110
|
|
634
111
|
end
|