effective_resources 1.8.3 → 1.8.8

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
  SHA256:
3
- metadata.gz: 3e9efd79bdfd45a008026c4df5eb078e141ce8291c01942aea6b40565434be38
4
- data.tar.gz: 8f31ee7e20b727cab52b580a470a74c00d64e1f830e76153d080dab7f9788645
3
+ metadata.gz: 7247ed4940067b7becde627b02de2ccc38e8fa425b0a43f1080e60b40b4c57dc
4
+ data.tar.gz: 38939264d4747f0d2f83a306c9d0fc4e474e6f6303dd612d47e40390a0cf0e8e
5
5
  SHA512:
6
- metadata.gz: 0e7bd31331526a1f9bd7d68022d5cf8587c21ed142c06e8353e2b14ea1d3acc5a26af54d69dacdce1469159b0e55c4698b381fa027067dbcf1b688d47ad42f2b
7
- data.tar.gz: ac2ffd7ccd01c8f016a99bf2f7dc6472b356a4dc489d1cdf5109fbb96bba36b642859bd28b14216b60b03309c830905b828e7f422dbc924325bd74fb9d6442b6
6
+ metadata.gz: a42dba0d42144cae133a766cebe9192d0257e3a0610a4535c163311f193923c56b5b21c15a4643d842557945d66d5a68faac4c0b1934ec406ce99ef8e991d5ad
7
+ data.tar.gz: fd8659905534432f07004a55d072dab21ab2c87e9f374c4b54fef68d8bec93450f68637200d598c430575b72969e3d156a419bd349ca54eb6460f0412acda4cc
@@ -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,7 +41,7 @@ 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
 
@@ -64,11 +52,28 @@ module Effective
64
52
  resource = Effective::Resource.new(controller_path, relation: relation)
65
53
 
66
54
  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.")
55
+ 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
56
+ else
57
+ resource
68
58
  end
59
+ end
60
+ end
61
+
62
+ def action_missing(action, *args, &block)
63
+ effective_resource = self.effective_resource(safe: true)
64
+ return super if effective_resource.blank?
65
+
66
+ action = action.to_sym
69
67
 
70
- resource
68
+ if effective_resource.member_actions.include?(action)
69
+ return member_action(action)
71
70
  end
71
+
72
+ if effective_resource.collection_actions.include?(action)
73
+ return collection_action(action)
74
+ end
75
+
76
+ super
72
77
  end
73
78
 
74
79
  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
@@ -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
 
@@ -39,18 +39,15 @@ module HasManyRichTexts
39
39
  end
40
40
 
41
41
  def method_missing(method, *args, &block)
42
- super if block_given?
43
- super unless respond_to?(method)
44
-
45
42
  method = method.to_s
43
+ super unless method.start_with?('rich_text_')
44
+
46
45
  name = method.chomp('=').sub('rich_text_', '')
47
46
 
48
47
  if method.end_with?('=')
49
48
  send(:assign_rich_text_body, name, *args)
50
- elsif args.length == 0
51
- send(:rich_text, name, *args)
52
49
  else
53
- super
50
+ send(:rich_text, name, *args)
54
51
  end
55
52
  end
56
53
 
@@ -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.3'.freeze
2
+ VERSION = '1.8.8'.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.3
4
+ version: 1.8.8
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-05 00:00:00.000000000 Z
11
+ date: 2021-03-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails