effective_resources 1.5.2 → 1.5.3

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
  SHA256:
3
- metadata.gz: cbff7f74429b0acf21d35b75cb64e6fedcdb4b35c2e62080c530d7416dd1d05c
4
- data.tar.gz: 392c8953a3cd59673d093c9fb8c8160951d54866c293e4f7f200d358503e0aab
3
+ metadata.gz: e01b50384fc2d41b95913769c539276b592257b4f36109917a02afe5a74a8584
4
+ data.tar.gz: 005a5676dca6e1d0e14a19ad96f31d834a85fdec527514bc75b86d040b3ed2f0
5
5
  SHA512:
6
- metadata.gz: 24f6a3cc4b8ab73cfd180e79f45ab609fd6cb3af7ef76f24c07b272c82d6ee1087203e76e09f2113e217975f1d29cd550930a99cd82197ccd18985a5f1faa1b0
7
- data.tar.gz: 1b3c4ef600b6aae43357764d6d908141f13453853fb7645ed11bf58f136dcb2265e16fadbaed9cfd627dd1a6d557fe4f14f1a24021e66491e072ad676e174a3e
6
+ metadata.gz: 0d135e3a5f848c0ee8f40d89e5412a4576fdd4e25068cd5df6ee3d01d3901db36f10c034376dc9bc253cc0f7aebbdce242cebca9c988e8a2bb315621aee80f80
7
+ data.tar.gz: c7808cfa20c0b8c7b256a0dc5d46ba0b39bf6c638d1200820cd93c50228b8c456b77f9adbe34039825c6e0a00dc5312b6da4ab51a464769ac8b11f91393dcd78
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module EffectiveResourcesHelper
2
4
 
3
5
  # effective_bootstrap
@@ -74,18 +76,22 @@ module EffectiveResourcesHelper
74
76
  namespace ||= (effective_resource.namespace.to_sym if effective_resource.namespace)
75
77
 
76
78
  # Assign actions
77
- actions = if atts.key?(:actions) # We filter out any actions passed to us that aren't supported
78
- available = effective_resource.actions + atts[:actions].map { |k, v| v[:action] if v[:path] }.compact
79
- atts[:actions].inject({}) { |h, (commit, opts)| h[commit] = opts if available.include?(opts[:action]); h }
79
+ # We filter out any actions passed to us that aren't supported
80
+ actions = if atts.key?(:actions)
81
+ {}.tap do |actions|
82
+ atts[:actions].each do |commit, opts|
83
+ actions[commit] = opts if (effective_resource.actions.include?(opts[:action]) || opts[:path]).present?
84
+ end
85
+ end
80
86
  else
81
87
  (resource.kind_of?(Class) ? effective_resource.resource_klass_actions : effective_resource.resource_actions)
82
88
  end
83
89
 
84
90
  # Consider only, except, false and proc false
85
- only = Array(atts[:only]).compact
86
- except = Array(atts[:except]).compact
91
+ only = Array(atts[:only]) if atts[:only].present?
92
+ except = Array(atts[:except]) if atts[:except].present?
87
93
 
88
- actions = actions.select do |_, opts|
94
+ actions.select! do |_, opts|
89
95
  action = opts[:action]
90
96
 
91
97
  if only.present? && !only.include?(action)
@@ -100,8 +106,11 @@ module EffectiveResourcesHelper
100
106
  end
101
107
 
102
108
  # Select Partial
103
- partial = ['effective/resource/actions', partial.to_s].join('_') if partial.kind_of?(Symbol)
104
- partial = (partial.presence || 'effective/resource/actions') + '.html'
109
+ partial = if partial.kind_of?(Symbol)
110
+ "effective/resource/actions_#{partial}.html"
111
+ else
112
+ "#{partial.presence || 'effective/resource/actions'}.html"
113
+ end
105
114
 
106
115
  # Assign Locals
107
116
  locals = {
@@ -2,21 +2,27 @@
2
2
 
3
3
  module EffectiveResourcesPrivateHelper
4
4
  REPLACE_PAGE_ACTIONS = {'update' => :edit, 'create' => :new}
5
+ BLACKLIST = [:default, :only, :except, :if, :unless, :redirect, :success, :danger, :klass]
6
+
7
+ DATA_CONFIRM = 'data-confirm'
5
8
 
6
9
  def permitted_resource_actions(resource, actions)
7
10
  page_action = REPLACE_PAGE_ACTIONS[params[:action]] || params[:action].try(:to_sym) || :save
8
11
  executor = Effective::ResourceExec.new(self, resource)
9
12
 
10
- actions.select do |commit, args|
13
+ actions.each_with_object({}) do |(commit, args), h|
11
14
  action = (args[:action] == :save ? (resource.new_record? ? :create : :update) : args[:action])
12
15
 
13
- (args.key?(:only) ? args[:only].include?(page_action) : true) &&
14
- (args.key?(:except) ? !args[:except].include?(page_action) : true) &&
15
- (args.key?(:if) ? executor.instance_exec(&args[:if]) : true) &&
16
- (args.key?(:unless) ? !executor.instance_exec(&args[:unless]) : true) &&
17
- EffectiveResources.authorized?(controller, action, resource)
18
- end.inject({}) do |h, (commit, args)|
16
+ permitted = (args.key?(:only) ? args[:only].include?(page_action) : true) &&
17
+ (args.key?(:except) ? !args[:except].include?(page_action) : true) &&
18
+ (args.key?(:if) ? executor.instance_exec(&args[:if]) : true) &&
19
+ (args.key?(:unless) ? !executor.instance_exec(&args[:unless]) : true) &&
20
+ EffectiveResources.authorized?(controller, action, resource)
21
+
22
+ next unless permitted
23
+
19
24
  opts = args.except(:default, :only, :except, :if, :unless, :redirect, :success, :danger, :klass)
25
+ resource_to_s = resource.to_s.presence || resource.class.name.underscore
20
26
 
21
27
  # Transform data: { ... } hash into 'data-' keys
22
28
  if opts.key?(:data)
@@ -32,34 +38,34 @@ module EffectiveResourcesPrivateHelper
32
38
  end
33
39
 
34
40
  # Replace resource name in any token strings
35
- if opts.key?('data-confirm')
36
- opts['data-confirm'] = opts['data-confirm'].gsub('@resource', (resource.to_s.presence || resource.class.name.gsub('::', ' ').underscore.gsub('_', ' ')).to_s)
41
+ if opts[DATA_CONFIRM].present? && opts[DATA_CONFIRM].include?('@resource'.freeze)
42
+ opts[DATA_CONFIRM] = opts[DATA_CONFIRM].gsub('@resource'.freeze, resource_to_s)
37
43
  end
38
44
 
39
45
  # Assign class
40
46
  opts[:class] ||= (
41
- if opts['data-method'].to_s == 'delete'
42
- 'btn btn-danger'
47
+ if opts['data-method'.freeze] == 'delete'.freeze
48
+ 'btn btn-danger'.freeze
43
49
  elsif h.length == 0
44
- 'btn btn-primary'
50
+ 'btn btn-primary'.freeze
45
51
  elsif defined?(EffectiveBootstrap)
46
- 'btn btn-secondary'
52
+ 'btn btn-secondary'.freeze
47
53
  else
48
- 'btn btn-default'
54
+ 'btn btn-default'.freeze
49
55
  end
50
56
  )
51
57
 
52
58
  # Assign title
53
59
  opts[:title] ||= case opts[:action]
54
60
  when :save then commit
55
- when :edit then "Edit #{resource}"
56
- when :show then "#{resource}"
57
- when :destroy then "Delete #{resource}"
61
+ when :edit then "Edit #{resource_to_s}"
62
+ when :show then "#{resource_to_s}"
63
+ when :destroy then "Delete #{resource_to_s}"
58
64
  when :index then "All #{resource.class.name.gsub('::', ' ').underscore.gsub('_', ' ').titleize.pluralize}"
59
- else "#{opts[:action].to_s.titleize} #{resource}"
65
+ else "#{opts[:action].to_s.titleize} #{resource_to_s}"
60
66
  end
61
67
 
62
- h[commit] = opts; h
68
+ h[commit] = opts
63
69
  end
64
70
  end
65
71
 
@@ -1,12 +1,17 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Effective
2
4
  module Resources
3
5
  module Actions
6
+ EMPTY_HASH = {}
7
+ POST_VERBS = ['POST', 'PUT', 'PATCH']
8
+ CRUD_ACTIONS = %i(index new create show edit update destroy)
4
9
 
5
10
  # This was written for the Edit actions fallback templates and Datatables
6
11
  # Effective::Resource.new('admin/posts').routes[:index]
7
12
  def routes
8
13
  @routes ||= (
9
- matches = [[namespace, plural_name].compact.join('/'.freeze), [namespace, name].compact.join('/'.freeze)]
14
+ matches = [[namespace, plural_name].compact.join('/'), [namespace, name].compact.join('/')]
10
15
 
11
16
  routes_engine.routes.routes.select do |route|
12
17
  matches.any? { |match| match == route.defaults[:controller] } && !route.name.to_s.end_with?('root')
@@ -19,7 +24,7 @@ module Effective
19
24
  # Effective::Resource.new('effective/order', namespace: :admin)
20
25
  def routes_engine
21
26
  case class_name
22
- when 'Effective::Order'.freeze
27
+ when 'Effective::Order'
23
28
  EffectiveOrders::Engine
24
29
  else
25
30
  Rails.application
@@ -30,12 +35,14 @@ module Effective
30
35
  # This will return empty for create, update and destroy
31
36
  def action_path_helper(action)
32
37
  return unless routes[action]
33
- return (routes[action].name + '_path'.freeze) if routes[action].name.present?
38
+ return (routes[action].name + '_path') if routes[action].name.present?
34
39
  end
35
40
 
36
41
  # Effective::Resource.new('admin/posts').action_path(:edit, Post.last) => '/admin/posts/3/edit'
37
42
  # Will work for any action. Returns the real path
38
- def action_path(action, resource = nil, opts = {})
43
+ def action_path(action, resource = nil, opts = nil)
44
+ opts ||= EMPTY_HASH
45
+
39
46
  if klass.nil? && resource.present? && initialized_name.kind_of?(ActiveRecord::Reflection::BelongsToReflection)
40
47
  return Effective::Resource.new(resource, namespace: namespace).action_path(action, resource, opts)
41
48
  end
@@ -53,12 +60,26 @@ module Effective
53
60
  end
54
61
  end
55
62
 
56
- # This generates the correct route when an object is overriding to_param
57
- if (resource || instance).respond_to?(:attributes)
58
- formattable = (resource || instance).attributes.symbolize_keys.merge(id: (resource || instance).to_param)
63
+ target = (resource || instance)
64
+
65
+ formattable = if routes[action].parts.include?(:id)
66
+ if target.respond_to?(:to_param) && target.respond_to?(:id) && (target.to_param != target.id.to_s)
67
+ routes[action].parts.each_with_object({}) do |part, h|
68
+ if part == :id
69
+ h[part] = target.to_param
70
+ elsif part == :format
71
+ # Nothing
72
+ elsif target.respond_to?(part)
73
+ h[part] = target.public_send(part)
74
+ end
75
+ end
76
+ else
77
+ target
78
+ end
59
79
  end
60
80
 
61
- path = routes[action].format(formattable || {}).presence
81
+ # Generate the path
82
+ path = routes[action].format(formattable || EMPTY_HASH)
62
83
 
63
84
  if path.present? && opts.present?
64
85
  uri = URI.parse(path)
@@ -70,74 +91,85 @@ module Effective
70
91
  end
71
92
 
72
93
  def actions
73
- routes.keys
94
+ @route_actions ||= routes.keys
74
95
  end
75
96
 
76
97
  def crud_actions
77
- actions & %i(index new create show edit update destroy)
98
+ @crud_actions ||= (actions & CRUD_ACTIONS)
78
99
  end
79
100
 
80
101
  # GET actions
81
102
  def collection_actions
82
- routes.values.map { |route| route.defaults[:action].to_sym if is_collection_route?(route) }.compact
103
+ @collection_actions ||= (
104
+ routes.map { |_, route| route.defaults[:action].to_sym if is_collection_route?(route) }.tap(&:compact!)
105
+ )
83
106
  end
84
107
 
85
108
  def collection_get_actions
86
- routes.values.map { |route| route.defaults[:action].to_sym if is_collection_route?(route) && is_get_route?(route) }.compact
109
+ @collection_get_actions ||= (
110
+ routes.map { |_, route| route.defaults[:action].to_sym if is_collection_route?(route) && is_get_route?(route) }.tap(&:compact!)
111
+ )
87
112
  end
88
113
 
89
114
  def collection_post_actions
90
- routes.values.map { |route| route.defaults[:action].to_sym if is_collection_route?(route) && is_post_route?(route) }.compact
115
+ @collection_post_actions ||= (
116
+ routes.map { |_, route| route.defaults[:action].to_sym if is_collection_route?(route) && is_post_route?(route) }.tap(&:compact!)
117
+ )
91
118
  end
92
119
 
93
120
  # All actions
94
121
  def member_actions
95
- routes.values.map { |route| route.defaults[:action].to_sym if is_member_route?(route) }.compact
122
+ @member_actions ||= (
123
+ routes.map { |_, route| route.defaults[:action].to_sym if is_member_route?(route) }.tap(&:compact!)
124
+ )
96
125
  end
97
126
 
98
127
  # GET actions
99
128
  def member_get_actions
100
- routes.values.map { |route| route.defaults[:action].to_sym if is_member_route?(route) && is_get_route?(route) }.compact
129
+ @member_get_actions ||= (
130
+ routes.map { |_, route| route.defaults[:action].to_sym if is_member_route?(route) && is_get_route?(route) }.tap(&:compact!)
131
+ )
101
132
  end
102
133
 
103
134
  def member_delete_actions
104
- routes.values.map { |route| route.defaults[:action].to_sym if is_member_route?(route) && is_delete_route?(route) }.compact
135
+ @member_delete_actions ||= (
136
+ routes.map { |_, route| route.defaults[:action].to_sym if is_member_route?(route) && is_delete_route?(route) }.tap(&:compact!)
137
+ )
105
138
  end
106
139
 
107
140
  # POST/PUT/PATCH actions
108
141
  def member_post_actions
109
- routes.values.map { |route| route.defaults[:action].to_sym if is_member_route?(route) && is_post_route?(route) }.compact
142
+ @member_post_actions ||= (
143
+ routes.map { |_, route| route.defaults[:action].to_sym if is_member_route?(route) && is_post_route?(route) }.tap(&:compact!)
144
+ )
110
145
  end
111
146
 
112
147
  # Same as controller_path in the view
113
148
  def controller_path
114
- [namespace, plural_name].compact * '/'.freeze
149
+ [namespace, plural_name].compact * '/'
115
150
  end
116
151
 
117
152
  private
118
153
 
119
154
  def is_member_route?(route)
120
- (route.path.required_names || []).include?('id'.freeze)
155
+ (route.path.required_names || []).include?('id')
121
156
  end
122
157
 
123
158
  def is_collection_route?(route)
124
- (route.path.required_names || []).include?('id'.freeze) == false
159
+ (route.path.required_names || []).include?('id') == false
125
160
  end
126
161
 
127
162
  def is_get_route?(route)
128
- route.verb.to_s.include?('GET'.freeze)
163
+ route.verb == 'GET'
129
164
  end
130
165
 
131
166
  def is_delete_route?(route)
132
- route.verb.to_s.include?('DELETE'.freeze)
167
+ route.verb == 'DELETE'
133
168
  end
134
169
 
135
170
  def is_post_route?(route)
136
- ['POST', 'PUT', 'PATCH'].freeze.any? { |verb| route.verb == verb }
171
+ POST_VERBS.include?(route.verb)
137
172
  end
138
173
  end
139
174
  end
140
175
  end
141
-
142
-
143
-
@@ -1,3 +1,5 @@
1
+ # frozen_sting_literals: true
2
+
1
3
  module Effective
2
4
  module Resources
3
5
  module Controller
@@ -66,15 +68,19 @@ module Effective
66
68
  # It is used by datatables
67
69
  def resource_actions
68
70
  {}.tap do |actions|
69
- (member_get_actions & crud_actions).reverse_each do |action|
71
+ member_get_actions.reverse_each do |action|
72
+ next unless crud_actions.include?(action)
70
73
  actions[action.to_s.titleize] = { action: action, default: true }
71
74
  end
72
75
 
73
- (member_get_actions - crud_actions).each do |action|
76
+ member_get_actions.each do |action|
77
+ next if crud_actions.include?(action)
74
78
  actions[action.to_s.titleize] = { action: action, default: true }
75
79
  end
76
80
 
77
- (member_post_actions - crud_actions).each do |action|
81
+ member_post_actions.each do |action|
82
+ next if crud_actions.include?(action)
83
+
78
84
  actions[action.to_s.titleize] = { action: action, default: true, 'data-method' => :post, 'data-confirm' => "Really #{action} @resource?" }
79
85
 
80
86
  actions[action.to_s.titleize] = case action
@@ -89,7 +95,7 @@ module Effective
89
95
 
90
96
  member_delete_actions.each do |action|
91
97
  if action == :destroy
92
- next if actions.values.find { |v| v[:action] == :archive }.present?
98
+ next if actions.find { |_, v| v[:action] == :archive }.present?
93
99
  actions['Delete'] = { action: action, default: true, 'data-method' => :delete, 'data-confirm' => "Really delete @resource?" }
94
100
  else
95
101
  actions[action.to_s.titleize] = { action: action, default: true, 'data-method' => :delete, 'data-confirm' => "Really #{action} @resource?" }
@@ -1,5 +1,6 @@
1
1
  - permitted_resource_actions(resource, actions).each do |label, opts|
2
- = link_to(label, (effective_resource.action_path(opts[:action], resource) || '#'), opts.except(:action))
2
+ - action = opts.delete(:action)
3
+ = link_to(label, (effective_resource.action_path(action, resource) || '#'), opts)
3
4
 
4
5
  = instance_exec(resource, &format_block) if local_assigns[:format_block]
5
6
  = yield if block_given?
@@ -1,6 +1,9 @@
1
1
  = dropdown(variation: :dropleft, btn_class: btn_class) do
2
2
  - permitted_resource_actions(resource, actions).each do |label, opts|
3
- = dropdown_link_to(label, (effective_resource.action_path(opts[:action], resource) || '#'), opts.except(:action, :class).merge(btn_class: btn_class))
3
+ - action = opts.delete(:action)
4
+ - opts.delete(:class)
5
+
6
+ = dropdown_link_to(label, (effective_resource.action_path(action, resource) || '#'), opts)
4
7
 
5
8
  = instance_exec(resource, &format_block) if local_assigns[:format_block]
6
9
  = yield(resource) if block_given?
@@ -1,5 +1,8 @@
1
1
  - permitted_resource_actions(resource, actions).each do |label, opts|
2
- = glyphicon_to(opts[:action], (effective_resource.action_path(opts[:action], resource) || '#'), opts.except(:action, :class))
2
+ - action = opts.delete(:action)
3
+ - opts.delete(:class)
4
+
5
+ = glyphicon_to(action, (effective_resource.action_path(action, resource) || '#'), opts)
3
6
 
4
7
  = instance_exec(resource, &format_block) if local_assigns[:format_block]
5
8
  = yield(resource) if block_given?
@@ -1,3 +1,3 @@
1
1
  module EffectiveResources
2
- VERSION = '1.5.2'.freeze
2
+ VERSION = '1.5.3'.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: 1.5.2
4
+ version: 1.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect