effective_resources 1.8.2 → 1.8.7

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: fc4db438688ecdd123360281ec6cfbe5b7aede01451615bcee92c566f186081c
4
- data.tar.gz: fa9154d7b1942bd0e8023f8795b670313260bc0cb7c22b84f2cd9c8d993e8ee1
3
+ metadata.gz: 79cab109b01cb0133d18f3530e8ed872c183c8e637c8279da87bbd068ddbee21
4
+ data.tar.gz: 20e8cb596ab2181fde595dc430c2e57fa1396b7d91490827d496e20438f59575
5
5
  SHA512:
6
- metadata.gz: 80b67a72b7a4f700b788d906a4db3c7e6e58f758f1d8f64d31178875c11c8021df2c706b86763efeba151a44b62cd07d19b46b0c2d354a20dd0831ee04aef89f
7
- data.tar.gz: 1f18e82c58fdae3ff884659c7166825727a19e90a28a55add1d952dea32e7ee3cdabe1bf251b7bbb83d8c72017c99df22defffedf8d9720092a24876c212175b
6
+ metadata.gz: b4ed96cdaf9a6fb68f51b3328fde6b38151f595f09e94fbc8943f034d0de2a9ae48a959f07c050fc77a8bfcaa8d1b6be0f671e7d23e762de51771cc58a6c8779
7
+ data.tar.gz: feaa6d1027ba1b6315a922b400feab502bc1f96e545092e4d9f9284f9cd935e80e918dd2b2e93fb5b0f2391b59978aaa908c1663ec112d10c9bd3cde819c830b
@@ -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
@@ -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,6 +1,9 @@
1
1
  # HasManyRichTexts
2
2
  #
3
- # Mark your model with 'has_many_rich_texts' and then any method missing is a rich text region
3
+ # Mark your model with 'has_many_rich_texts'
4
+ # Then it will automatically create a region when using a method named rich_text_*
5
+ # object.rich_text_body = "<p>Stuff</p>"
6
+ # object.rich_text_body => ActionText::RichText<name="body" body="<p>Stuff</p>">
4
7
 
5
8
  module HasManyRichTexts
6
9
  extend ActiveSupport::Concern
@@ -25,31 +28,26 @@ module HasManyRichTexts
25
28
  rich_texts.find { |rt| rt.name == name } || rich_texts.build(name: name)
26
29
  end
27
30
 
28
- def rich_text_body=(name, body)
31
+ def assign_rich_text_body(name, body)
29
32
  rich_text(name).assign_attributes(body: body)
30
33
  end
31
34
 
32
35
  # Prevents an ActiveModel::UnknownAttributeError
33
36
  # https://github.com/rails/rails/blob/main/activemodel/lib/active_model/attribute_assignment.rb#L48
34
37
  def respond_to?(*args)
35
- method = args.first.to_s
36
- return false if ['to_a', 'to_ary'].any? { |str| method == str }
37
- return false if ['_by', '_at', '_id', '_by=', '_at=', 'id='].any? { |str| method.end_with?(str) }
38
- true
38
+ args.first.to_s.start_with?('rich_text_') ? true : super
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
+
45
+ name = method.chomp('=').sub('rich_text_', '')
46
46
 
47
- if method.end_with?('=') && args.length == 1 && (args.first.kind_of?(String) || args.first.kind_of?(NilClass))
48
- send(:rich_text_body=, method.chomp('='), *args)
49
- elsif args.length == 0
50
- send(:rich_text, method, *args)
47
+ if method.end_with?('=')
48
+ send(:assign_rich_text_body, name, *args)
51
49
  else
52
- super
50
+ send(:rich_text, name, *args)
53
51
  end
54
52
  end
55
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.2'.freeze
2
+ VERSION = '1.8.7'.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.2
4
+ version: 1.8.7
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