effective_resources 0.8.1 → 0.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1383850485a3ad9051e04e93dd836a381b9b1f0c
4
- data.tar.gz: e745b62f182c4ea7e7610b0b752d8cc2c5fe525e
3
+ metadata.gz: 8f976ed56d3a007b2b1946b8620fc24b8c73dddc
4
+ data.tar.gz: 21034761b686916da76ccb02fda5d02f3ba5ca89
5
5
  SHA512:
6
- metadata.gz: 63d8afb4eb59ee8275ab211f58bc4a5fdbf67b2cfcc7fc7ff2edc068456ee7ce558f10d61f0009fd94565dc8022ee34b4d7d231d033144cf8025c9f2f1ba25e2
7
- data.tar.gz: 059dd7b7c19f4680c9e58dc4a60ffd412697a498cf28a5104fb1b0be04a174cdb9d2a76d6121c84ae6cd9933d7a8436ee58e3ec800d0e5da0f89d3646f03203b
6
+ metadata.gz: a1c6926084cfa8ed8b9e963f99b1b73601c26995b3ecf0f52f8887516902394c06bd362ce14f7bffa4ed3acad4d7fb899f737b289c4fb44404edf401405d4c99
7
+ data.tar.gz: 166df66e1f56b62ce979f53eeec17a81b9c736ab964019f4c2c832808806f47ddd91edba1f981bdbc795ebb33f24894acca310dc5dfa645a44110fb271fa8375
@@ -166,9 +166,22 @@ module Effective
166
166
  self.resource ||= resource_scope.new
167
167
 
168
168
  self.resource.assign_attributes(
169
- params.to_unsafe_h.except(:controller, :action).select { |k, v| resource.respond_to?("#{k}=") }
169
+ params.to_unsafe_h.except(:controller, :action, :id).select { |k, v| resource.respond_to?("#{k}=") }
170
170
  )
171
171
 
172
+ if params[:duplicate_id]
173
+ duplicate = resource_scope.find(params[:duplicate_id])
174
+ EffectiveResources.authorize!(self, :show, duplicate)
175
+
176
+ self.resource = duplicate_resource(duplicate)
177
+ raise "expected duplicate_resource to return an unsaved new #{resource_klass} resource" unless resource.kind_of?(resource_klass) && resource.new_record?
178
+
179
+ if (message = flash[:success].to_s).present?
180
+ flash.delete(:success)
181
+ flash.now[:success] = "#{message.chomp('.')}. Adding another #{resource_name.titleize} based on previous."
182
+ end
183
+ end
184
+
172
185
  @page_title ||= "New #{resource_name.titleize}"
173
186
  EffectiveResources.authorize!(self, :new, resource)
174
187
 
@@ -188,11 +201,6 @@ module Effective
188
201
  resource.created_by ||= current_user if resource.respond_to?(:created_by=)
189
202
 
190
203
  if save_resource(resource, action)
191
- if add_new_resource_action?
192
- render_add_new_resource! and return
193
- end
194
-
195
- # Normal redirect
196
204
  flash[:success] ||= flash_success(resource, action)
197
205
  redirect_to(resource_redirect_path)
198
206
  else
@@ -231,11 +239,6 @@ module Effective
231
239
  resource.assign_attributes(send(resource_params_method_name))
232
240
 
233
241
  if save_resource(resource, action)
234
- if add_new_resource_action?
235
- render_add_new_resource! and return
236
- end
237
-
238
- # Normal save
239
242
  flash[:success] ||= flash_success(resource, action)
240
243
  redirect_to(resource_redirect_path)
241
244
  else
@@ -308,7 +311,6 @@ module Effective
308
311
  render json: { status: 200, message: "Successfully #{action_verb(action)} #{successes} / #{resources.length} selected #{resource_plural_name}" }
309
312
  end
310
313
 
311
-
312
314
  protected
313
315
 
314
316
  # This calls the appropriate member action, probably save!, on the resource.
@@ -324,6 +326,8 @@ module Effective
324
326
  run_callbacks(:resource_save)
325
327
  return true
326
328
  rescue => e
329
+ resource.restore_attributes(['status', 'state'])
330
+
327
331
  flash.delete(:success)
328
332
  flash.now[:danger] = flash_danger(resource, action, e: e)
329
333
  raise ActiveRecord::Rollback
@@ -334,54 +338,51 @@ module Effective
334
338
  false
335
339
  end
336
340
 
341
+ # Should return a new resource based on the passed one
342
+ def duplicate_resource(resource)
343
+ resource.dup
344
+ end
345
+
337
346
  def resource_redirect_path
338
- return instance_exec(&commit_action[:redirect]) if commit_action[:redirect].respond_to?(:call)
339
-
340
- commit_action_redirect = case commit_action[:redirect]
341
- when :index ; resource_index_path
342
- when :edit ; resource_edit_path
343
- when :show ; resource_show_path
344
- when :new ; resource_new_path
345
- when :back ; referer_redirect_path
346
- when nil ; nil
347
- else ; resource_member_action_path(commit_action[:action])
347
+ redirect = commit_action[:redirect].respond_to?(:call) ? instance_exec(&commit_action[:redirect]) : commit_action[:redirect]
348
+
349
+ commit_action_redirect = case redirect
350
+ when :index ; resource_index_path
351
+ when :edit ; resource_edit_path
352
+ when :show ; resource_show_path
353
+ when :new ; resource_new_path
354
+ when :duplicate ; resource_duplicate_path
355
+ when :back ; referer_redirect_path
356
+ when :save ; [resource_edit_path, resource_show_path].compact.first
357
+ when Symbol ; resource_action_path(commit_action[:action])
358
+ when String ; redirect
359
+ else ; nil
348
360
  end
349
361
 
350
362
  return commit_action_redirect if commit_action_redirect.present?
351
363
 
352
364
  case params[:commit].to_s
353
365
  when 'Save'
354
- [resource_edit_path, resource_show_path, resource_index_path].compact.first
366
+ [resource_edit_path, resource_show_path, resource_index_path]
355
367
  when 'Save and Add New', 'Add New'
356
- [resource_new_path, resource_index_path].compact.first
368
+ [resource_new_path, resource_index_path]
369
+ when 'Duplicate'
370
+ [resource_duplicate_path, resource_index_path]
357
371
  when 'Continue', 'Save and Continue'
358
- resource_index_path
372
+ [resource_index_path]
359
373
  else
360
- [referer_redirect_path, resource_index_path].compact.first
361
- end.presence || root_path
362
- end
363
-
364
- def add_new_resource_action?
365
- (add_new_resource_params_method_name.present? && resource_redirect_path == resource_new_path)
366
- end
367
-
368
- def render_add_new_resource!
369
- attributes = send(add_new_resource_params_method_name).except(:id, :created_at, :updated_at)
370
- self.resource = resource_scope.new(attributes)
371
-
372
- @page_title = "New #{resource_name.titleize}"
373
- flash.now[:success] ||= flash_success(resource) + ". Adding another #{resource_name.titleize} based on previous."
374
-
375
- render(:new)
376
- true
374
+ [referer_redirect_path, resource_edit_path, resource_show_path, resource_index_path]
375
+ end.compact.first.presence || root_path
377
376
  end
378
377
 
379
378
  def referer_redirect_path
380
- return if (resource && resource.destroyed? && request.referer.to_s.include?("/#{resource.to_param}"))
379
+ url = request.referer.to_s
381
380
 
382
- if request.referer.present? && (Rails.application.routes.recognize_path(URI(request.referer.to_s).path) rescue false)
383
- request.referer.to_s
384
- end
381
+ return if (resource && resource.destroyed? && url.include?("/#{resource.to_param}"))
382
+ return if url.include?('duplicate_id=')
383
+ return unless (Rails.application.routes.recognize_path(URI(url).path) rescue false)
384
+
385
+ url
385
386
  end
386
387
 
387
388
  def resource_index_path
@@ -392,6 +393,10 @@ module Effective
392
393
  effective_resource.action_path(:new)
393
394
  end
394
395
 
396
+ def resource_duplicate_path
397
+ effective_resource.action_path(:new, duplicate_id: resource.id)
398
+ end
399
+
395
400
  def resource_edit_path
396
401
  effective_resource.action_path(:edit, resource)
397
402
  end
@@ -404,7 +409,7 @@ module Effective
404
409
  effective_resource.action_path(:destroy, resource)
405
410
  end
406
411
 
407
- def resource_member_action_path(action)
412
+ def resource_action_path(action)
408
413
  effective_resource.action_path(action.to_sym, resource)
409
414
  end
410
415
 
@@ -490,10 +495,5 @@ module Effective
490
495
  ["#{resource_name}_params", "#{resource_plural_name}_params", 'permitted_params'].find { |name| respond_to?(name, true) } || 'params'
491
496
  end
492
497
 
493
- def add_new_resource_params_method_name
494
- method_name = resource_params_method_name
495
- "add_new_#{method_name}" if respond_to?("add_new_#{method_name}", true)
496
- end
497
-
498
498
  end
499
499
  end
@@ -3,11 +3,7 @@ module EffectiveResourcesHelper
3
3
  def effective_submit(form, options = {}, &block) # effective_bootstrap
4
4
  resource = (controller.class.respond_to?(:effective_resource) ? controller.class.effective_resource : Effective::Resource.new(controller_path))
5
5
  actions = resource.submits_for(form.object, controller: controller)
6
-
7
- # Group by class and render
8
- buttons = actions.group_by { |_, opts| opts[:class] }.flat_map do |_, btns|
9
- btns.map { |name, opts| form.save(name, opts) }
10
- end.join.html_safe
6
+ buttons = actions.map { |name, opts| form.save(name, opts) }.join.html_safe
11
7
 
12
8
  form.submit('', options) do
13
9
  (block_given? ? capture(&block) : ''.html_safe) + buttons
@@ -19,23 +15,17 @@ module EffectiveResourcesHelper
19
15
  resource = (controller.class.respond_to?(:effective_resource) ? controller.class.effective_resource : Effective::Resource.new(controller_path))
20
16
  actions = resource.submits_for(form.object, controller: controller)
21
17
 
22
- wrapper_options = { class: 'form-actions' }.merge(options.delete(:wrapper_html) || {})
23
-
24
- content_tag(:div, wrapper_options) do
25
- buttons = actions.group_by { |_, args| args[:class] }.flat_map do |_, action|
26
- action.map { |action| form.button(:submit, *action) } + ['']
27
- end
18
+ buttons = actions.map { |action| form.button(:submit, *action) }
28
19
 
29
- # I think this is a bug. I can't override default button class when passing my own class: variable. it merges them.
30
- if defined?(SimpleForm) && (btn_class = SimpleForm.button_class).present?
31
- buttons = buttons.map { |button| button.sub(btn_class, '') }
32
- end
20
+ # I think this is a bug. I can't override default button class when passing my own class: variable. it merges them.
21
+ if defined?(SimpleForm) && (btn_class = SimpleForm.button_class).present?
22
+ buttons = buttons.map { |button| button.sub(btn_class, '') }
23
+ end
33
24
 
34
- if block_given?
35
- buttons = [capture(&block), ''] + buttons
36
- end
25
+ wrapper_options = { class: 'form-actions' }.merge(options.delete(:wrapper_html) || {})
37
26
 
38
- buttons.join(' ').html_safe
27
+ content_tag(:div, wrapper_options) do
28
+ (block_given? ? capture(&block) : ''.html_safe) + buttons.join(' ').html_safe
39
29
  end
40
30
  end
41
31
 
@@ -38,6 +38,10 @@ module Effective
38
38
  def action_path(action, resource = nil, opts = {})
39
39
  return unless routes[action]
40
40
 
41
+ if resource.kind_of?(Hash)
42
+ opts = resource; resource = nil
43
+ end
44
+
41
45
  # edge case: Effective::Resource.new('admin/comments').action_path(:new, @post)
42
46
  if resource.present? && !resource.kind_of?(klass)
43
47
  if (bt = belongs_to(resource)).present? && instance.respond_to?("#{bt.name}=")
@@ -45,7 +49,15 @@ module Effective
45
49
  end
46
50
  end
47
51
 
48
- routes[action].format(resource || instance).presence
52
+ path = routes[action].format(resource || instance).presence
53
+
54
+ if path.present? && opts.present?
55
+ uri = URI.parse(path)
56
+ uri.query = URI.encode_www_form(opts)
57
+ path = uri.to_s
58
+ end
59
+
60
+ path
49
61
  end
50
62
 
51
63
  def actions
@@ -9,7 +9,7 @@ module Effective
9
9
  def submits
10
10
  @submits ||= {}.tap do |submits|
11
11
  if (actions.find { |a| a == :create } || actions.find { |a| a == :update })
12
- submits['Save'] = { action: :save, class: 'btn btn-primary' }
12
+ submits['Save'] = { action: :save, default: true, class: 'btn btn-primary' }
13
13
  end
14
14
 
15
15
  member_post_actions.each do |action| # default true means it will be overwritten by dsl methods
@@ -17,11 +17,11 @@ module Effective
17
17
  end
18
18
 
19
19
  if actions.find { |a| a == :index }
20
- submits['Continue'] = { action: :save, redirect: :index }
20
+ submits['Continue'] = { action: :save, default: true, redirect: :index }
21
21
  end
22
22
 
23
23
  if actions.find { |a| a == :new }
24
- submits['Add New'] = { action: :save, redirect: :new }
24
+ submits['Add New'] = { action: :save, default: true, redirect: :new }
25
25
  end
26
26
  end
27
27
  end
@@ -38,21 +38,10 @@ module Effective
38
38
  (args.key?(:if) ? obj.instance_exec(&args[:if]) : true) &&
39
39
  (args.key?(:unless) ? !obj.instance_exec(&args[:unless]) : true) &&
40
40
  EffectiveResources.authorized?(controller, action, obj)
41
- end.sort do |(commit_x, x), (commit_y, y)|
42
- # Sort to front
43
- primary = (y[:class].include?('primary') ? 1 : 0) - (x[:class].include?('primary') ? 1 : 0)
44
- primary = nil if primary == 0
45
-
46
- # Sort to back
47
- danger = (x[:class].include?('danger') ? 1 : 0) - (y[:class].include?('danger') ? 1 : 0)
48
- danger = nil if danger == 0
49
-
50
- primary || danger || submits.keys.index(commit_x) <=> submits.keys.index(commit_y)
51
- end.inject({}) do |h, (commit, args)|
52
- h[commit] = args.except(:action, :default, :if, :unless, :redirect); h
53
41
  end.transform_values.with_index do |opts, index|
54
42
  opts[:class] = "btn #{index == 0 ? 'btn-primary' : 'btn-secondary'}" if opts[:class].blank?
55
- opts
43
+
44
+ opts.except(:action, :default, :if, :unless, :redirect)
56
45
  end
57
46
  end
58
47
 
@@ -1,3 +1,3 @@
1
1
  module EffectiveResources
2
- VERSION = '0.8.1'.freeze
2
+ VERSION = '0.8.2'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_resources
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.1
4
+ version: 0.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect