effective_resources 0.8.6 → 0.8.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cf2388cda759dd479d527951e8d9ee29e95ccfd2
4
- data.tar.gz: 4281eedf715d8d5ef2cfe6ea91cf5dbe686dfd1b
3
+ metadata.gz: 9ecad33788aad8f500973e4bce6486b1cd844c79
4
+ data.tar.gz: dda9302fd764fb11f1ec14946fa8b75fca388659
5
5
  SHA512:
6
- metadata.gz: 89dd285d4e1179cad76c591719e8f48ca0faf6a2998cead46cfc59234e122f09767129d773da66ff8ce39d98272217eab0b892fe100a2fdb4f06426ad5e6b4f2
7
- data.tar.gz: 0c0a5fd8aa744d0630af9686793ce190da98906d37455e5bc1bf8b6ea375e8b04ac0020061b21431d0f986c48cdc6aaa9275e9d18e78df286e55610cdf998b8d
6
+ metadata.gz: 62b2d628abef9e7c481211cc449d795f0e480af7187b1741f9c8446bea6f3fbbcca169e9f43b2eedef0f634574ddb6bae6fa67a0c7cb27a14d137e24e7809b60
7
+ data.tar.gz: 3aed9366a8292394a3cdecc420d6e607dcb62bbad965fa5948ec6c39df98a29b79d99026c88198c70799014d1fb602b93e08fb47bb2e61877fe184672509de72
data/README.md CHANGED
@@ -61,10 +61,9 @@ class PostsController < ApplicationController
61
61
  params.require(:post).permit(:id, :author_id, :category, :title, :body)
62
62
  end
63
63
 
64
- # On #create or #update, if you click 'Add New', the new resource form will be rendered with these attributes preopulated
65
- # Makes it super quick to Add New items if only one or two of the fields are changing.
66
- def add_new_post_params
67
- permitted_params.slice(:author_id, :category)
64
+ # Pass /things/new?duplicate_id=3
65
+ def duplicate_resource(resource)
66
+ resource_klass.new(resource.attributes.slice('job_site', 'address'))
68
67
  end
69
68
 
70
69
  end
@@ -49,6 +49,7 @@ module Effective
49
49
  #
50
50
  # submit :toggle, 'Blacklist', if: -> { sync? }, class: 'btn btn-primary'
51
51
  # submit :toggle, 'Whitelist', if: -> { !sync? }, class: 'btn btn-primary'
52
+ # submit :save, 'Save', success: -> { "#{self} was saved okay!" }
52
53
 
53
54
  def submit(action, commit = nil, args = {})
54
55
  raise 'expected args to be a Hash or false' unless args.kind_of?(Hash) || args == false
@@ -95,6 +96,8 @@ module Effective
95
96
  # resource_scope do
96
97
  # { user_id: current_user.id }
97
98
  # end
99
+ # Nested controllers? sure
100
+ # resource_scope -> { User.find(params[:user_id]).things }
98
101
 
99
102
  # Return value should be:
100
103
  # a Relation: Thing.where(user: current_user)
@@ -201,12 +204,24 @@ module Effective
201
204
 
202
205
  respond_to do |format|
203
206
  if save_resource(resource, action)
204
- flash[:success] ||= flash_success(resource, action)
207
+ format.html do
208
+ flash[:success] ||= resource_flash(:success, resource, action)
209
+ redirect_to(resource_redirect_path)
210
+ end
205
211
 
206
- format.html { redirect_to(resource_redirect_path) }
207
- format.js {} # create.js.erb
212
+ format.js do
213
+ if commit_action[:redirect].present?
214
+ flash[:success] ||= resource_flash(:success, resource, action)
215
+ redirect_to(resource_redirect_path)
216
+ else
217
+ flash.now[:success] ||= resource_flash(:success, resource, action)
218
+ reload_resource
219
+ # create.js.erb
220
+ end
221
+ end
208
222
  else
209
- flash.now[:danger] ||= flash_danger(resource, action)
223
+ flash.delete(:success)
224
+ flash.now[:danger] ||= resource_flash(:danger, resource, action)
210
225
 
211
226
  format.html { render :new }
212
227
  format.js {} # create.js.erb
@@ -245,15 +260,27 @@ module Effective
245
260
 
246
261
  respond_to do |format|
247
262
  if save_resource(resource, action)
248
- flash[:success] ||= flash_success(resource, action)
263
+ format.html do
264
+ flash[:success] ||= resource_flash(:success, resource, action)
265
+ redirect_to(resource_redirect_path)
266
+ end
249
267
 
250
- format.html { redirect_to(resource_redirect_path) }
251
- format.js { reload_resource } # update.js.erb
268
+ format.js do
269
+ if commit_action[:redirect].present?
270
+ flash[:success] ||= resource_flash(:success, resource, action)
271
+ redirect_to(resource_redirect_path)
272
+ else
273
+ flash.now[:success] ||= resource_flash(:success, resource, action)
274
+ reload_resource
275
+ # update.js.erb
276
+ end
277
+ end
252
278
  else
253
- flash.now[:danger] ||= flash_danger(resource, action)
279
+ flash.delete(:success)
280
+ flash.now[:danger] ||= resource_flash(:danger, resource, action)
254
281
 
255
282
  format.html { render :edit }
256
- format.js {} # update.js.erb
283
+ format.js { } # update.js.erb
257
284
  end
258
285
  end
259
286
  end
@@ -261,20 +288,38 @@ module Effective
261
288
  def destroy
262
289
  self.resource = resource_scope.find(params[:id])
263
290
 
291
+ action = :destroy
264
292
  @page_title ||= "Destroy #{resource}"
265
- EffectiveResources.authorize!(self, :destroy, resource)
293
+ EffectiveResources.authorize!(self, action, resource)
266
294
 
267
295
  respond_to do |format|
268
- if save_resource(resource, :destroy)
269
- flash[:success] ||= flash_success(resource, :delete)
296
+ if save_resource(resource, action)
297
+ format.html do
298
+ flash[:success] ||= resource_flash(:success, resource, action)
299
+ redirect_to(resource_redirect_path)
300
+ end
270
301
 
271
- format.html { redirect_to(resource_redirect_path) }
272
- format.js {} # delete.js.erb
302
+ format.js do
303
+ if commit_action[:redirect].present?
304
+ flash[:success] ||= resource_flash(:success, resource, action)
305
+ redirect_to(resource_redirect_path)
306
+ else
307
+ flash.now[:success] ||= resource_flash(:success, resource, action)
308
+ # delete.js.erb
309
+ end
310
+ end
273
311
  else
274
- flash.now[:danger] ||= flash_danger(resource, :delete)
312
+ flash.delete(:success)
313
+
314
+ format.html do
315
+ flash[:danger] ||= resource_flash(:danger, resource, action)
316
+ redirect_to(resource_redirect_path)
317
+ end
275
318
 
276
- format.html { redirect_to(resource_redirect_path) }
277
- format.js {} # delete.js.erb
319
+ format.js do
320
+ flash.now[:danger] ||= resource_flash(:danger, resource, action)
321
+ # destroy.js.erb
322
+ end
278
323
  end
279
324
  end
280
325
  end
@@ -284,10 +329,10 @@ module Effective
284
329
  raise 'expected post, patch or put http action' unless (request.post? || request.patch? || request.put?)
285
330
 
286
331
  if save_resource(resource, action)
287
- flash[:success] ||= flash_success(resource, action)
332
+ flash[:success] ||= resource_flash(:success, resource, action)
288
333
  redirect_to(referer_redirect_path || resource_redirect_path)
289
334
  else
290
- flash.now[:danger] ||= flash_danger(resource, action)
335
+ flash.now[:danger] ||= resource_flash(:danger, resource, action)
291
336
 
292
337
  if resource_edit_path && (referer_redirect_path || '').end_with?(resource_edit_path)
293
338
  @page_title ||= "Edit #{resource}"
@@ -315,7 +360,7 @@ module Effective
315
360
 
316
361
  successes = 0
317
362
 
318
- resource_klass.transaction do
363
+ ActiveRecord::Base.transaction do
319
364
  successes = resources.select do |resource|
320
365
  begin
321
366
  resource.public_send("#{action}!") if EffectiveResources.authorized?(self, action, resource)
@@ -336,7 +381,7 @@ module Effective
336
381
 
337
382
  resource.current_user ||= current_user if resource.respond_to?(:current_user=)
338
383
 
339
- resource_klass.transaction do
384
+ ActiveRecord::Base.transaction do
340
385
  begin
341
386
  if resource.public_send("#{action}!") == false
342
387
  raise("failed to #{action} #{resource}")
@@ -346,7 +391,7 @@ module Effective
346
391
  run_callbacks(:resource_save)
347
392
  return true
348
393
  rescue => e
349
- resource.restore_attributes(['status', 'state'])
394
+ resource.restore_attributes(['status', 'state']) if resource.respond_to?(:restore_attributes)
350
395
 
351
396
  flash.delete(:success)
352
397
  flash.now[:danger] = flash_danger(resource, action, e: e)
@@ -359,7 +404,7 @@ module Effective
359
404
  end
360
405
 
361
406
  def reload_resource
362
- self.resource = resource_scope.find(params[:id])
407
+ self.resource.reload if resource.respond_to?(:reload)
363
408
  end
364
409
 
365
410
  # Should return a new resource based on the passed one
@@ -367,6 +412,18 @@ module Effective
367
412
  resource.dup
368
413
  end
369
414
 
415
+ def resource_flash(status, resource, action)
416
+ message = commit_action[status].respond_to?(:call) ? instance_exec(&commit_action[status]) : commit_action[status]
417
+ return message if message.present?
418
+
419
+ message || case status
420
+ when :success then flash_success(resource, action)
421
+ when :danger then flash_danger(resource, action)
422
+ else
423
+ raise "unknown resource flash status: #{status}"
424
+ end
425
+ end
426
+
370
427
  def resource_redirect_path
371
428
  redirect = commit_action[:redirect].respond_to?(:call) ? instance_exec(&commit_action[:redirect]) : commit_action[:redirect]
372
429
 
@@ -402,7 +459,7 @@ module Effective
402
459
  def referer_redirect_path
403
460
  url = request.referer.to_s
404
461
 
405
- return if (resource && resource.destroyed? && url.include?("/#{resource.to_param}"))
462
+ return if (resource && resource.respond_to?(:destroyed?) && resource.destroyed? && url.include?("/#{resource.to_param}"))
406
463
  return if url.include?('duplicate_id=')
407
464
  return unless (Rails.application.routes.recognize_path(URI(url).path) rescue false)
408
465
 
@@ -480,7 +537,9 @@ module Effective
480
537
  end
481
538
 
482
539
  def commit_action
483
- self.class.submits[params[:commit].to_s] || { action: :save }
540
+ self.class.submits[params[:commit].to_s] ||
541
+ self.class.submits.find { |_, v| v[:action] == :save }&.last ||
542
+ { action: :save }
484
543
  end
485
544
 
486
545
  # Returns an ActiveRecord relation based on the computed value of `resource_scope` dsl method
@@ -39,8 +39,8 @@ module EffectiveResourcesHelper
39
39
  end
40
40
 
41
41
  # When called from /admin/things/new.html.haml this will render 'admin/things/form', or 'things/form', or 'thing/form'
42
- def render_resource_form(resource)
43
- atts = {:namespace => (resource.namespace.to_sym if resource.namespace.present?), resource.name.to_sym => instance_variable_get('@' + resource.name)}.compact
42
+ def render_resource_form(resource, atts = {})
43
+ atts = {:namespace => (resource.namespace.to_sym if resource.namespace.present?), resource.name.to_sym => instance_variable_get('@' + resource.name)}.compact.merge(atts)
44
44
 
45
45
  if lookup_context.template_exists?('form', controller._prefixes, :partial)
46
46
  render 'form', atts
@@ -1,5 +1,5 @@
1
1
  <% resource = (@_effective_resource || Effective::Resource.new(controller_path)) %>
2
2
  <% @resource = instance_variable_get('@' + resource.name) if resource.name %>
3
3
 
4
- EffectiveBootstrap.remote_form_payload = $("<%= j render_resource_form(resource) %>");
5
- EffectiveBootstrap.remote_form_flash = <%= raw flash.to_json %>;
4
+ EffectiveForm.remote_form_payload = $("<div><%= j render_resource_form(resource) %></div>");
5
+ EffectiveForm.remote_form_flash = <%= raw flash.to_json %>;
@@ -1,5 +1,5 @@
1
1
  <% resource = (@_effective_resource || Effective::Resource.new(controller_path)) %>
2
2
  <% @resource = instance_variable_get('@' + resource.name) if resource.name %>
3
3
 
4
- EffectiveBootstrap.remote_form_payload = $("<%= j render_resource_form(resource) %>");
5
- EffectiveBootstrap.remote_form_flash = <%= raw flash.to_json %>;
4
+ EffectiveForm.remote_form_payload = $("<div><%= j render_resource_form(resource) %></div>");
5
+ EffectiveForm.remote_form_flash = <%= raw flash.to_json %>;
@@ -1,5 +1,5 @@
1
1
  <% resource = (@_effective_resource || Effective::Resource.new(controller_path)) %>
2
2
  <% @resource = instance_variable_get('@' + resource.name) if resource.name %>
3
3
 
4
- EffectiveBootstrap.remote_form_payload = $("<%= j render_resource_form(resource) %>");
5
- EffectiveBootstrap.remote_form_flash = <%= raw flash.to_json %>;
4
+ EffectiveForm.remote_form_payload = $("<div><%= j render_resource_form(resource) %></div>");
5
+ EffectiveForm.remote_form_flash = <%= raw flash.to_json %>;
@@ -1,3 +1,3 @@
1
1
  module EffectiveResources
2
- VERSION = '0.8.6'.freeze
2
+ VERSION = '0.8.7'.freeze
3
3
  end
@@ -24,7 +24,7 @@ module EffectiveResources
24
24
  end
25
25
 
26
26
  def self.authorize!(controller, action, resource)
27
- raise Effective::AccessDenied unless authorized?(controller, action, resource)
27
+ raise Effective::AccessDenied.new('Access Denied', action, resource) unless authorized?(controller, action, resource)
28
28
  end
29
29
 
30
30
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_resources
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.6
4
+ version: 0.8.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-23 00:00:00.000000000 Z
11
+ date: 2018-04-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails