effective_resources 1.8.5 → 1.8.10

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: 29481500627bdb2e83a1f66d58dde1c9d74aecc2e8902b2bb2afa82b0a70ad46
4
- data.tar.gz: e46866b50672f2e2fa928caa226d663eb630f292d3a2b10c3de026cf323b3d67
3
+ metadata.gz: dc3c685dc56b79f0af482b0cf0d67e0dacafd6b9dc1e032cceb5d688b44752d6
4
+ data.tar.gz: e5b9f21ab57e68f9fbc601d6a41cc40358f47f92850e80c756e75f7c52d13383
5
5
  SHA512:
6
- metadata.gz: fd7cfc021a37f0e1b4381b7d9a6b3ff1515c9368ff3b4fbc072573ebdc89a6e0df0b59f01fa1392fafe00d1993ff1115cdabd1081315172dc84db711786cc36f
7
- data.tar.gz: b352c4e02f16afae95201ad937ec14a746a73c116a6a9149052a3c239c9160229f9932ffbfb9a5e66a8c25782f38a29146e5b1f5b0bb4df1616c4a5201cc0da4
6
+ metadata.gz: 044ee953eb0e281c9f85c95934a2824a80d73a2779e4799e716374e7f1fe932255339bd56efc918333a9886eaef0baa0c4400a43db1060ddd40ee2c2f988d7d5
7
+ data.tar.gz: 6d337535d4e2c37da1f8d5bbeb5c6dd95621bb3789b873fcf94364fd21fd5750a7a31c0c5e6d6be28d560525c6b5a7c1de9d8a56b6abdf710e2cc771c71a35c5
@@ -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
@@ -7,7 +7,7 @@ module Effective
7
7
  action ||= resource.respond_to?("#{step}!") ? step : :save
8
8
 
9
9
  if save_resource(resource, action)
10
- flash[:success] = options.delete(:success) || resource_flash(:success, resource, action)
10
+ flash[:success] ||= options.delete(:success) || resource_flash(:success, resource, action)
11
11
 
12
12
  @skip_to ||= next_step
13
13
  @redirect_to ||= resource_wizard_path(resource, @skip_to) if was_new_record
@@ -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
 
@@ -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.5'.freeze
2
+ VERSION = '1.8.10'.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.5
4
+ version: 1.8.10
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-04-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails