effective_resources 1.8.4 → 1.8.9

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: 33448d396358208a6585b8beeff84f22b659bfc3676acbf9ed087d647acc56d3
4
- data.tar.gz: 56b33689f40c4988cb600175623f5b3a3a4dde6192630d86188080244f5333ee
3
+ metadata.gz: 9509a414f11385f7ea245dd9981a689630cf9266678ff038f96c46fddd79091a
4
+ data.tar.gz: abb42db276f35a17a8a1cae9ed5d226d34488f6a06c0ca6096a95a55974a24f8
5
5
  SHA512:
6
- metadata.gz: d8e3d0a86b310e31d991deb85b090c62167ba713e986d8a6c8ba30b3c3fa5583014d406af6e7d21cdbcc1f34fe5c8e8ec541d29e25835b724ebaa87e13b4cae1
7
- data.tar.gz: 5908aa0138e84275ad07dcb500ccce0601a9ba7864ee98711eab0712a0075ea4fd0ad9adc33842fd423d3f6bc71d7f4d53319c3fbced1ec2ff90f28d8e27b527
6
+ metadata.gz: e8364de4a198c0e481a8819571b12d8e2092ad1785a1403f8a5c7c4993497f8c4b4e6309582b8661651f0ab95933e36418d67a2dccdb811c74cb9914af49cdd4
7
+ data.tar.gz: 68917727e998f65ad44282caa4ceb4810bce58a81c0a6ed66d56760992e840a5a4cdd662d889022e4fae3ff99fad488a4d44603604d1c986344bf42cf45c91c4
@@ -11,7 +11,6 @@ module Effective
11
11
  include Effective::CrudController::Submits
12
12
 
13
13
  included do
14
- define_actions_from_routes
15
14
  define_callbacks :resource_render, :resource_before_save, :resource_after_save, :resource_after_commit, :resource_error
16
15
  layout -> { resource_layout }
17
16
  end
@@ -19,22 +18,11 @@ module Effective
19
18
  module ClassMethods
20
19
  include Effective::CrudController::Dsl
21
20
 
22
- # This is used to define_actions_from_routes and for the buttons/submits/ons
21
+ # This is used for the buttons/submits/ons
23
22
  # It doesn't really work with the resource_scope correctly but the routes are important here
24
23
  def effective_resource
25
24
  @_effective_resource ||= Effective::Resource.new(controller_path)
26
25
  end
27
-
28
- # Automatically respond to any action defined via the routes file
29
- def define_actions_from_routes
30
- (effective_resource.member_actions - effective_resource.crud_actions).each do |action|
31
- define_method(action) { member_action(action) }
32
- end
33
-
34
- (effective_resource.collection_actions - effective_resource.crud_actions).each do |action|
35
- define_method(action) { collection_action(action) }
36
- end
37
- end
38
26
  end
39
27
 
40
28
  def resource # @thing
@@ -53,22 +41,41 @@ module Effective
53
41
  send(:instance_variable_set, "@#{resource_plural_name}", instance)
54
42
  end
55
43
 
56
- def effective_resource
44
+ def effective_resource(safe: false)
57
45
  @_effective_resource ||= begin
58
46
  relation = instance_exec(&resource_scope_relation) if respond_to?(:resource_scope_relation)
59
47
 
60
- if respond_to?(:resource_scope_relation) && !relation.kind_of?(ActiveRecord::Relation)
61
- raise('resource_scope must return an ActiveRecord::Relation')
48
+ if respond_to?(:resource_scope_relation)
49
+ unless relation.kind_of?(ActiveRecord::Relation) || (relation.kind_of?(Class) && relation.ancestors.include?(ActiveModel::Model))
50
+ raise('resource_scope must return an ActiveRecord::Relation or class including ActiveModel::Model')
51
+ end
62
52
  end
63
53
 
64
54
  resource = Effective::Resource.new(controller_path, relation: relation)
65
55
 
66
56
  unless resource.relation.kind_of?(ActiveRecord::Relation) || resource.active_model?
67
- raise("unable to build resource_scope for #{resource.klass || 'unknown klass'}. Please name your controller to match an existing model, or manually define a resource_scope.")
57
+ raise("unable to build resource_scope for #{resource.klass || 'unknown klass'}. Please name your controller to match an existing model, or manually define a resource_scope.") unless safe
58
+ else
59
+ resource
68
60
  end
61
+ end
62
+ end
63
+
64
+ def action_missing(action, *args, &block)
65
+ effective_resource = self.effective_resource(safe: true)
66
+ return super if effective_resource.blank?
67
+
68
+ action = action.to_sym
69
69
 
70
- resource
70
+ if effective_resource.member_actions.include?(action)
71
+ return member_action(action)
71
72
  end
73
+
74
+ if effective_resource.collection_actions.include?(action)
75
+ return collection_action(action)
76
+ end
77
+
78
+ super
72
79
  end
73
80
 
74
81
  private
@@ -2,7 +2,7 @@ module Effective
2
2
  module CrudController
3
3
  module Paths
4
4
 
5
- def resource_redirect_path(action = nil)
5
+ def resource_redirect_path(resource, action)
6
6
  submit = commit_action(action)
7
7
  redirect = submit[:redirect].respond_to?(:call) ? instance_exec(&submit[:redirect]) : submit[:redirect]
8
8
 
@@ -39,13 +39,29 @@ module Effective
39
39
  # Otherwise consider the action
40
40
  commit_default_redirect = case action
41
41
  when :create
42
- [resource_show_path, resource_edit_path, resource_index_path]
42
+ [
43
+ (resource_show_path if EffectiveResources.authorized?(self, :show, resource)),
44
+ (resource_edit_path if EffectiveResources.authorized?(self, :edit, resource)),
45
+ (resource_index_path if EffectiveResources.authorized?(self, :index, resource.class))
46
+ ]
43
47
  when :update
44
- [resource_edit_path, resource_show_path, resource_index_path]
48
+ [
49
+ (resource_edit_path if EffectiveResources.authorized?(self, :edit, resource)),
50
+ (resource_show_path if EffectiveResources.authorized?(self, :show, resource)),
51
+ (resource_index_path if EffectiveResources.authorized?(self, :index, resource.class))
52
+ ]
45
53
  when :destroy
46
- [referer_redirect_path, resource_index_path]
54
+ [
55
+ referer_redirect_path,
56
+ (resource_index_path if EffectiveResources.authorized?(self, :index, resource.class))
57
+ ]
47
58
  else
48
- [referer_redirect_path, resource_edit_path, resource_show_path, resource_index_path]
59
+ [
60
+ referer_redirect_path,
61
+ (resource_edit_path if EffectiveResources.authorized?(self, :edit, resource)),
62
+ (resource_show_path if EffectiveResources.authorized?(self, :show, resource)),
63
+ (resource_index_path if EffectiveResources.authorized?(self, :index, resource.class))
64
+ ]
49
65
  end.compact.first
50
66
 
51
67
  return commit_default_redirect if commit_default_redirect.present?
@@ -10,12 +10,12 @@ module Effective
10
10
  respond_to do |format|
11
11
  format.html do
12
12
  flash[:success] ||= resource_flash(:success, resource, action)
13
- redirect_to(resource_redirect_path(action))
13
+ redirect_to(resource_redirect_path(resource, action))
14
14
  end
15
15
 
16
16
  format.js do
17
17
  flash[:success] ||= resource_flash(:success, resource, action)
18
- redirect_to(resource_redirect_path(action))
18
+ redirect_to(resource_redirect_path(resource, action))
19
19
  end
20
20
  end
21
21
  elsif template_present?(action)
@@ -35,7 +35,7 @@ module Effective
35
35
  respond_to do |format|
36
36
  format.html do
37
37
  flash[:success] ||= resource_flash(:success, resource, action)
38
- redirect_to(resource_redirect_path(action))
38
+ redirect_to(resource_redirect_path(resource, action))
39
39
  end
40
40
 
41
41
  format.js do
@@ -61,7 +61,7 @@ module Effective
61
61
  when :destroy
62
62
  format.html do
63
63
  redirect_flash
64
- redirect_to(resource_redirect_path(action))
64
+ redirect_to(resource_redirect_path(resource, action))
65
65
  end
66
66
  else
67
67
  if template_present?(action)
@@ -73,7 +73,7 @@ module Effective
73
73
  else
74
74
  format.html do
75
75
  redirect_flash
76
- redirect_to(resource_redirect_path(action))
76
+ redirect_to(resource_redirect_path(resource, action))
77
77
  end
78
78
  end
79
79
  end
@@ -8,8 +8,10 @@ module Effective
8
8
  def flash_success(resource, action = nil, name: nil)
9
9
  raise 'expected an ActiveRecord resource' unless (name || resource.class.respond_to?(:model_name))
10
10
 
11
- name ||= begin
12
- resource.destroyed? ? resource.class.model_name.to_s.downcase.split('::').last : resource.to_s.presence
11
+ name ||= if resource.respond_to?(:destroyed?) && resource.destroyed?
12
+ resource.class.model_name.to_s.downcase.split('::').last
13
+ else
14
+ resource.to_s.presence
13
15
  end
14
16
 
15
17
  "Successfully #{action_verb(action)} #{name || 'resource'}".html_safe
@@ -24,8 +26,10 @@ module Effective
24
26
 
25
27
  messages = flash_errors(resource, e: e)
26
28
 
27
- name ||= begin
28
- resource.destroyed? ? resource.class.model_name.to_s.downcase.split('::').last : resource.to_s.presence
29
+ name ||= if resource.respond_to?(:destroyed?) && resource.destroyed?
30
+ resource.class.model_name.to_s.downcase.split('::').last
31
+ else
32
+ resource.to_s.presence
29
33
  end
30
34
 
31
35
  ["Unable to #{action}", (" #{name}" if name), (": #{messages}" if messages)].compact.join.html_safe
@@ -148,6 +148,7 @@ module EffectiveResourcesHelper
148
148
  effective_resource = (atts.delete(:effective_resource) || find_effective_resource)
149
149
 
150
150
  action = atts.delete(:action)
151
+ safe = atts.delete(:safe)
151
152
  atts = { :namespace => (effective_resource.namespace.to_sym if effective_resource.namespace), effective_resource.name.to_sym => resource }.compact.merge(atts)
152
153
 
153
154
  if lookup_context.template_exists?("form_#{action}", controller._prefixes, :partial)
@@ -168,7 +169,10 @@ module EffectiveResourcesHelper
168
169
  end
169
170
  end
170
171
 
171
- render('form', atts) # Will raise the regular error
172
+ # Will raise the regular error
173
+ return ''.html_safe if safe
174
+
175
+ render('form', atts)
172
176
  end
173
177
 
174
178
  # Similar to render_resource_form
@@ -182,6 +186,7 @@ module EffectiveResourcesHelper
182
186
  effective_resource = (atts.delete(:effective_resource) || find_effective_resource)
183
187
 
184
188
  action = atts.delete(:action)
189
+ safe = atts.delete(:safe)
185
190
  atts = { :namespace => (effective_resource.namespace.to_sym if effective_resource.namespace), effective_resource.name.to_sym => resource }.compact.merge(atts)
186
191
 
187
192
  if lookup_context.template_exists?(effective_resource.name, controller._prefixes, :partial)
@@ -194,7 +199,10 @@ module EffectiveResourcesHelper
194
199
  end
195
200
  end
196
201
 
197
- render(resource, atts) # Will raise the regular error
202
+ # Will raise the regular error
203
+ return ''.html_safe if safe
204
+
205
+ render(resource, atts)
198
206
  end
199
207
  alias_method :render_resource, :render_resource_partial
200
208
 
@@ -70,7 +70,8 @@ module Effective
70
70
  end
71
71
 
72
72
  def action_texts
73
- klass.reflect_on_all_associations(:has_one).select { |ass| ass.class_name == 'ActionText::RichText' }
73
+ klass.reflect_on_all_associations(:has_one).select { |ass| ass.class_name == 'ActionText::RichText' } +
74
+ klass.reflect_on_all_associations(:has_many).select { |ass| ass.class_name == 'ActionText::RichText' }
74
75
  end
75
76
 
76
77
  def action_texts_has_ones_ids
@@ -80,6 +80,7 @@ module Effective
80
80
  action_texts
81
81
  .map { |ass| instance.send(ass.name) }
82
82
  .compact
83
+ .flatten
83
84
  .select { |obj| obj.previous_changes['body'].present? }
84
85
  .inject({}) { |h, obj| h[obj.name.to_sym] = obj.previous_changes['body']; h }
85
86
  end
@@ -1,7 +1,7 @@
1
1
  <% resource = (@_effective_resource || Effective::Resource.new(controller_path)) %>
2
2
  <% @resource = instance_variable_get('@' + resource.name) if resource.name %>
3
3
 
4
- EffectiveForm.remote_form_payload = "<%= j render_resource_form(@resource, action: action) %>";
4
+ EffectiveForm.remote_form_payload = "<%= j render_resource_form(@resource, action: action, safe: true) %>";
5
5
  EffectiveForm.remote_form_commit = "<%= params[:commit] %>";
6
6
  EffectiveForm.remote_form_flash = <%= raw flash.to_json %>;
7
7
 
@@ -7,7 +7,7 @@ module EffectiveGem
7
7
  raise("expected self.config_keys method") unless respond_to?(:config_keys)
8
8
 
9
9
  config_keys.each do |key|
10
- self.class.define_method(key) { config()[key] }
10
+ self.singleton_class.define_method(key) { config()[key] }
11
11
  end
12
12
  end
13
13
 
@@ -1,3 +1,3 @@
1
1
  module EffectiveResources
2
- VERSION = '1.8.4'.freeze
2
+ VERSION = '1.8.9'.freeze
3
3
  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: 1.8.4
4
+ version: 1.8.9
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: 2021-03-08 00:00:00.000000000 Z
11
+ date: 2021-03-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails