effective_resources 0.8.26 → 0.9.0

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: a06e3e88a6bf810728238c3e946eb7cca8d91360
4
- data.tar.gz: 2dc0d2c0e88675f7f355ec8a0c172faa5c5cd98d
3
+ metadata.gz: b065e313e4557d61719a9164d5ac32226a8070e4
4
+ data.tar.gz: 2e451c636d5644ff487a5df44c546053675eaed6
5
5
  SHA512:
6
- metadata.gz: 6df911c7b05cf356999d037a0fe8873fc06a100e0bf3dffbe4b82343a9d876893a3ceac152c60c64e52391b660e09b3d3e59440403063a02cf2ee7e5552e0e2a
7
- data.tar.gz: 0b3c9d1df71cecc5e0299587423e381e931b9a3bb730c0b636c02240dfbbe5fa2a87a8e4b9aa41ce0b6b5a76916d0dc2b93cf649cee0ba8098399128d3de10d6
6
+ metadata.gz: d428a2f4135014eb14d5bd92734e327cb1befff5acbc0e2c7b5c9d9a58c07e34aeca29531f8228e1614ac046bccd30d9c43665e3ec594bdead3018cb7b5febc5
7
+ data.tar.gz: c072a4f0c130c92e5b7c2ac57a299b6e61c092c8b3d8d17a59a51d8af49ffb1292b2006aae0a38a2720eee2a04679b088141d108d3fda37ba0bff45f7f60652f
@@ -38,46 +38,49 @@ module EffectiveResourcesHelper
38
38
  end
39
39
  end
40
40
 
41
- def render_resource_crud_actions(resource, instance = nil, atts = {}, &block)
42
- (atts = instance; instance = nil) if instance.kind_of?(Hash) && atts.blank?
43
- render_resource_actions(resource, instance, atts.merge(crud: true), &block)
44
- end
45
-
41
+ # Renders the effective/resource view partial for this resource
46
42
  # resource is an Effective::Resource
47
- # instance is an ActiveRecord thing
48
- # atts are crud: true|false, locals: {}, namespace:, partial:
49
- # anything else are actions, edit: true, show: false, destroy: false and member GET actions
43
+ # instance is an ActiveRecord thing, an Array of ActiveRecord things, or nil
44
+ # Atts are everything else. Interesting ones include:
45
+
46
+ # partial: :dropleft|:glyphicons|string
47
+ # locals: {} render locals
48
+ # you can also pass all action names and true/false such as edit: true, show: false
50
49
  def render_resource_actions(resource, instance = nil, atts = {}, &block)
51
50
  (atts = instance; instance = nil) if instance.kind_of?(Hash) && atts.blank?
52
51
  raise 'expected first argument to be an Effective::Resource' unless resource.kind_of?(Effective::Resource)
53
52
  raise 'expected attributes to be a Hash' unless atts.kind_of?(Hash)
54
53
 
55
- instance = instance || instance_variable_get('@' + resource.name) || resource.instance
56
- raise "unable to find resource instance. Either pass the instance as the second argument, or assign @#{resource.name}" unless instance
57
-
58
- crud = atts.delete(:crud) || false
59
54
  locals = atts.delete(:locals) || {}
60
55
  namespace = atts.delete(:namespace) || (resource.namespace.to_sym if resource.namespace)
61
56
  partial = atts.delete(:partial)
57
+ spacer_template = locals.delete(:spacer_template)
62
58
 
63
59
  partial = case partial
64
60
  when String
65
61
  partial
66
62
  when Symbol
67
- ['effective/resource/actions', partial.to_s].join('_')
63
+ ['effective/resource/actions'.freeze, partial.to_s].join('_')
68
64
  else
69
- 'effective/resource/actions'
65
+ 'effective/resource/actions'.freeze
70
66
  end + '.html'.freeze
71
67
 
72
- actions = (crud ? resource.resource_crud_actions : resource.resource_actions)
73
- raise "unknown action for #{resource.name}: #{unknown}." if (unknown = (atts.keys - actions)).present?
68
+ actions = (instance ? resource.resource_member_actions : resource.resource_collection_actions)
69
+ actions = (actions & resource.crud_actions) if atts.delete(:crud)
70
+ raise "unknown action for #{resource.name}: #{(atts.keys - actions).join(' ')}." if (atts.keys - actions).present?
74
71
 
75
- actions = actions - atts.reject { |_, v| v }.keys + atts.select { |_, v| v }.keys
76
- actions = actions.uniq.select { |action| EffectiveResources.authorized?(controller, action, resource) }
72
+ actions = (actions - atts.reject { |_, v| v }.keys + atts.select { |_, v| v }.keys).uniq
73
+ actions.select! { |action| EffectiveResources.authorized?(controller, action, (Array(instance).first || resource.klass)) }
77
74
 
78
75
  locals = { resource: instance, effective_resource: resource, namespace: namespace, actions: actions }.compact.merge(locals)
79
76
 
80
- block_given? ? render((partial), locals) { yield } : render((partial), locals)
77
+ if instance.kind_of?(Array) # Render as collection mode
78
+ render(partial: partial, collection: instance, as: :resource, locals: locals.except(:resource), spacer_template: spacer_template)
79
+ elsif block_given?
80
+ render(partial, locals) { yield }
81
+ else
82
+ render(partial, locals)
83
+ end
81
84
  end
82
85
 
83
86
  # When called from /admin/things/new.html.haml this will render 'admin/things/form', or 'things/form', or 'thing/form'
@@ -70,13 +70,14 @@ module Effective
70
70
  end
71
71
 
72
72
  # Used by render_resource_actions helper
73
- # All the actions we can actually make a link to
74
- def resource_actions
73
+ # All the actions we can actually make a link to but not submits
74
+ def resource_member_actions
75
75
  (routes.keys & [:show, :edit, :destroy]) + member_get_actions
76
76
  end
77
77
 
78
- def resource_crud_actions
79
- (routes.keys & [:show, :edit, :destroy])
78
+ # Used by render_resource_actions helper for the index screens or new record
79
+ def resource_collection_actions
80
+ (routes.keys & [:new]) + collection_get_actions
80
81
  end
81
82
 
82
83
  # GET actions
@@ -1,15 +1,22 @@
1
+ - if actions.index(:new)
2
+ - name = effective_resource.human_name.titleize
3
+ = link_to "New #{name}", effective_resource.action_path(:new), title: "New #{name}", class: 'btn btn-primary'
4
+
1
5
  - if actions.index(:edit)
2
6
  = link_to 'Edit', effective_resource.action_path(:edit, resource), title: "Edit #{resource}", class: 'btn btn-secondary'
3
7
 
4
8
  - if actions.index(:show)
5
9
  = link_to 'View', effective_resource.action_path(:show, resource), title: resource.to_s, class: 'btn btn-secondary'
6
10
 
7
- - (actions - [:edit, :show, :destroy]).each do |action|
8
- = link_to action.to_s.titleize, effective_resource.action_path(action, resource), class: 'btn btn-secondary',
9
- title: "#{action} #{resource}", data: { method: :post, confirm: "Really #{action} #{resource}?"}
11
+ - (actions - [:new, :edit, :show, :destroy]).each do |action|
12
+ - if effective_resource.member_post_actions.include?(action)
13
+ = link_to action.to_s.titleize, effective_resource.action_path(action, resource), class: 'btn btn-secondary',
14
+ title: "#{action} #{resource}", data: { method: :post, confirm: "#{action.to_s.titleize} #{resource}?"}
15
+ - else
16
+ = link_to action.to_s.titleize, effective_resource.action_path(action), class: 'btn btn-secondary', title: action.to_s.titleize
10
17
 
11
- = yield
18
+ = yield if block_given?
12
19
 
13
20
  - if actions.index(:destroy)
14
- = link_to "Delete #{resource}", effective_resource.action_path(:destroy, resource), class: 'btn btn-danger',
15
- title: "Delete #{resource}", data: { method: :delete, confirm: "Really delete #{resource}?" }
21
+ = link_to 'Delete', effective_resource.action_path(:destroy, resource), class: 'btn btn-danger',
22
+ title: "Delete #{resource}", data: { method: :delete, confirm: "Delete #{resource}?" }
@@ -7,10 +7,10 @@
7
7
 
8
8
  - (actions - [:edit, :show, :destroy]).each do |action|
9
9
  = dropdown_link_to action.to_s.titleize, effective_resource.action_path(action, resource),
10
- title: "#{action} #{resource}", data: { method: :post, confirm: "Really #{action} #{resource}?"}
10
+ title: "#{action} #{resource}", data: { method: :post, confirm: "#{action.to_s.titleize} #{resource}?"}
11
11
 
12
- = yield
12
+ = yield if block_given?
13
13
 
14
14
  - if actions.index(:destroy)
15
- = dropdown_link_to "Delete #{resource}", effective_resource.action_path(:destroy, resource),
16
- title: "Delete #{resource}", data: { method: :delete, confirm: "Really delete #{resource}?" }
15
+ = dropdown_link_to 'Delete', effective_resource.action_path(:destroy, resource),
16
+ title: "Delete #{resource}", data: { method: :delete, confirm: "Delete #{resource}?" }
@@ -1,3 +1,3 @@
1
1
  module EffectiveResources
2
- VERSION = '0.8.26'.freeze
2
+ VERSION = '0.9.0'.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.26
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect