effective_resources 1.6.6 → 1.7.4

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: 323b70f4a0eb470c2e96edf3651c83493763ad203659f03bb25832cf487812a1
4
- data.tar.gz: f997c5dcd62f4de1d49065c5fb3d5d882ea3eca30c47fe518bb6642dbd43e65a
3
+ metadata.gz: 321d1bd446ce934af73f92967e26577e09f16de314d0c0c780630901ccc23823
4
+ data.tar.gz: f65a37ad4e83d73e6d3da2ac2bf3a0a1588b7ca86e425ddb2d901ba9e9dfd571
5
5
  SHA512:
6
- metadata.gz: 4d78587039acac7fa236913c588946277481a5df90933141db13a7d447491233452f8dd6c3c96ebf7bff8dfd95ba217e56c65e826128d9674532331d6971422c
7
- data.tar.gz: daff8c0e7009dd67297f2e62a90744b7dcc67008f107543b53c53b24476ca8534284202aae98b0579e394fddbdbb498925b0294f47c95e70f9bbf8b551cab4a3
6
+ metadata.gz: e828b71738b3f48d9819fbffcabf1e8a94a44ae0e410069a0a7f1b64751cdb02fb23ce75dd0ad7ffc687ce6c0955112c4da1f9c864cc62256832fd308938b936
7
+ data.tar.gz: d2be424f57532b120b7a1e465e158592a2f5cc20ba2c8b6ac21dba864d9f8c844143d9e229507b1ad9e7792285065665ec57c87eedcfaab760e40fedf02ec095
@@ -11,13 +11,14 @@ module Effective
11
11
 
12
12
  included do
13
13
  define_actions_from_routes
14
- define_permitted_params_from_model
15
14
  define_callbacks :resource_render, :resource_before_save, :resource_after_save, :resource_after_commit, :resource_error
16
15
  end
17
16
 
18
17
  module ClassMethods
19
18
  include Effective::CrudController::Dsl
20
19
 
20
+ # This is used to define_actions_from_routes and for the buttons/submits/ons
21
+ # It doesn't really work with the resource_scope correctly but the routes are important here
21
22
  def effective_resource
22
23
  @_effective_resource ||= Effective::Resource.new(controller_path)
23
24
  end
@@ -32,17 +33,6 @@ module Effective
32
33
  define_method(action) { collection_action(action) }
33
34
  end
34
35
  end
35
-
36
- def define_permitted_params_from_model
37
- if effective_resource.model.present?
38
- define_method(:effective_resource_permitted_params) { resource_permitted_params } # save.rb
39
- end
40
-
41
- if effective_resource.active_model?
42
- define_method(:effective_resource_permitted_params) { resource_active_model_permitted_params } # save.rb
43
- end
44
- end
45
-
46
36
  end
47
37
 
48
38
  def resource # @thing
@@ -62,11 +52,29 @@ module Effective
62
52
  end
63
53
 
64
54
  def effective_resource
65
- self.class.effective_resource
55
+ @_effective_resource ||= begin
56
+ relation = instance_exec(&resource_scope_relation) if respond_to?(:resource_scope_relation)
57
+
58
+ if respond_to?(:resource_scope_relation) && !relation.kind_of?(ActiveRecord::Relation)
59
+ raise('resource_scope must return an ActiveRecord::Relation')
60
+ end
61
+
62
+ resource = Effective::Resource.new(controller_path, relation: relation)
63
+
64
+ unless resource.relation.kind_of?(ActiveRecord::Relation) || resource.active_model?
65
+ 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.")
66
+ end
67
+
68
+ resource
69
+ end
66
70
  end
67
71
 
68
72
  private
69
73
 
74
+ def resource_scope
75
+ effective_resource.relation
76
+ end
77
+
70
78
  def resource_name # 'thing'
71
79
  effective_resource.name
72
80
  end
@@ -83,30 +91,6 @@ module Effective
83
91
  effective_resource.plural_name
84
92
  end
85
93
 
86
- # Returns an ActiveRecord relation based on the computed value of `resource_scope` dsl method
87
- def resource_scope # Thing
88
- @_effective_resource_relation ||= (
89
- relation = case @_effective_resource_scope # If this was initialized by the resource_scope before_action
90
- when ActiveRecord::Relation
91
- @_effective_resource_scope
92
- when Hash
93
- effective_resource.klass.where(@_effective_resource_scope)
94
- when Symbol
95
- effective_resource.klass.send(@_effective_resource_scope)
96
- when nil
97
- effective_resource.klass.respond_to?(:all) ? effective_resource.klass.all : effective_resource.klass
98
- else
99
- raise "expected resource_scope method to return an ActiveRecord::Relation or Hash"
100
- end
101
-
102
- unless relation.kind_of?(ActiveRecord::Relation) || effective_resource.active_model?
103
- raise("unable to build resource_scope for #{effective_resource.klass || 'unknown klass'}. Please name your controller to match an existing model, or manually define a resource_scope.")
104
- end
105
-
106
- relation
107
- )
108
- end
109
-
110
94
  def resource_datatable_attributes
111
95
  resource_scope.where_values_hash.symbolize_keys
112
96
  end
@@ -128,7 +112,16 @@ module Effective
128
112
  end
129
113
 
130
114
  def resource_params_method_name
131
- ["#{resource_name}_params", "#{resource_plural_name}_params", 'permitted_params', 'effective_resource_permitted_params', ('resource_permitted_params' if effective_resource.model.present?)].compact.find { |name| respond_to?(name, true) } || 'params'
115
+ ['permitted_params', "#{resource_name}_params", "#{resource_plural_name}_params"].each do |name|
116
+ return name if respond_to?(name, true)
117
+ end
118
+
119
+ # Built in ones
120
+ return 'resource_permitted_params' if effective_resource.model.present?
121
+ return 'resource_active_model_permitted_params' if effective_resource.active_model?
122
+
123
+ # Fallback
124
+ 'params'
132
125
  end
133
126
 
134
127
  end
@@ -79,9 +79,12 @@ module Effective
79
79
  def resource_scope(obj = nil, opts = {}, &block)
80
80
  raise 'expected a proc or block' unless (obj.respond_to?(:call) || block_given?)
81
81
 
82
- instance_exec do
83
- before_action(opts) { @_effective_resource_scope ||= instance_exec(&(block_given? ? block : obj)) }
82
+ if block_given?
83
+ define_method(:resource_scope_relation) { return block }
84
+ else
85
+ define_method(:resource_scope_relation) { return obj }
84
86
  end
87
+
85
88
  end
86
89
 
87
90
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Effective
2
4
  module CrudController
3
5
  module Respond
@@ -94,7 +96,10 @@ module Effective
94
96
  end
95
97
 
96
98
  def template_present?(action)
97
- lookup_context.template_exists?("#{action}.#{request.format.symbol.to_s.sub('json', 'js').presence || 'html'}", _prefixes)
99
+ #lookup_context.template_exists?("#{action}.#{request.format.symbol.to_s.sub('json', 'js').presence || 'html'}", _prefixes)
100
+
101
+ formats = [request.format.symbol.to_s.sub('json', 'js').presence || 'html']
102
+ lookup_context.template_exists?(action, _prefixes, formats: formats)
98
103
  end
99
104
 
100
105
  end
@@ -8,6 +8,7 @@ module Effective
8
8
  include Effective::WizardController::Actions
9
9
  include Effective::WizardController::BeforeActions
10
10
  include Effective::WizardController::Save
11
+ include Effective::WizardController::WickedOverrides
11
12
 
12
13
  included do
13
14
  raise("please install gem 'wicked' to use Effective::WizardController") unless defined?(Wicked)
@@ -54,9 +55,11 @@ module Effective
54
55
  effective_resource.klass.const_get(:WIZARD_STEPS).keys
55
56
  end
56
57
 
58
+ # It could be :new, :start
59
+ # Or resource, step
57
60
  def resource_wizard_path(resource, step)
58
- path_helper = effective_resource.action_path_helper(:show).to_s.sub('_path', '_build_path')
59
- public_send(path_helper, resource, step)
61
+ param = (resource.respond_to?(:to_param) ? resource.to_param : resource)
62
+ wizard_path(step, resource_name_id => param)
60
63
  end
61
64
 
62
65
  end
@@ -21,6 +21,7 @@ module Effective
21
21
  Rails.logger.info 'Processed by Effective::WizardController#update'
22
22
 
23
23
  resource.assign_attributes(send(resource_params_method_name))
24
+ assign_current_step
24
25
 
25
26
  save_wizard_resource(resource)
26
27
  end
@@ -54,7 +54,7 @@ module Effective
54
54
  end
55
55
 
56
56
  # before_action :assign_current_step, only: [:show, :update]
57
- # Assign the urrent step to resource
57
+ # Assign the current step to resource
58
58
  def assign_current_step
59
59
  if respond_to?(:current_user) && resource.respond_to?(:current_user=)
60
60
  resource.current_user = current_user
@@ -0,0 +1,23 @@
1
+ module Effective
2
+ module WizardController
3
+ module WickedOverrides
4
+
5
+ # Changes made here to work inside an effective rails engine
6
+ #
7
+ # https://github.com/zombocom/wicked/blob/main/lib/wicked/controller/concerns/path.rb
8
+ # https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/routing/url_for.rb#L180
9
+ def wizard_path(goto_step = nil, options = {})
10
+ options = options.respond_to?(:to_h) ? options.to_h : options
11
+ options = { :controller => wicked_controller,
12
+ :action => 'show',
13
+ :id => goto_step || params[:id],
14
+ :only_path => true
15
+ }.merge(options)
16
+
17
+ merged_url_options = options.reverse_merge!(url_options)
18
+ effective_resource.url_helpers.url_for(merged_url_options)
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -107,15 +107,16 @@ module EffectiveResourcesHelper
107
107
 
108
108
  # Select Partial
109
109
  partial = if partial.kind_of?(Symbol)
110
- "effective/resource/actions_#{partial}.html"
110
+ "effective/resource/actions_#{partial}"
111
111
  else
112
- "#{partial.presence || 'effective/resource/actions'}.html"
112
+ partial.presence || 'effective/resource/actions'
113
113
  end
114
114
 
115
115
  # Assign Locals
116
116
  locals = {
117
117
  resource: resource,
118
118
  effective_resource: effective_resource,
119
+ formats: [:html],
119
120
  format_block: (block if block_given?),
120
121
  namespace: namespace,
121
122
  actions: actions,
@@ -123,7 +124,14 @@ module EffectiveResourcesHelper
123
124
  }.compact.merge(locals)
124
125
 
125
126
  if resource.kind_of?(Array)
126
- render(partial: partial, collection: resource, as: :resource, locals: locals.except(:resource), spacer_template: spacer_template)
127
+ render(
128
+ partial: partial,
129
+ formats: [:html],
130
+ collection: resource,
131
+ as: :resource,
132
+ locals: locals.except(:resource),
133
+ spacer_template: spacer_template
134
+ )
127
135
  else
128
136
  render(partial, locals)
129
137
  end
@@ -58,8 +58,7 @@ module ActsAsSlugged
58
58
  end
59
59
 
60
60
  def to_param
61
- slug
61
+ slug_was || slug
62
62
  end
63
63
 
64
64
  end
65
-
@@ -68,6 +68,10 @@ module ActsAsWizard
68
68
  wizard_steps[step].present?
69
69
  end
70
70
 
71
+ def next_step
72
+ required_steps.reverse.find { |step| can_visit_step?(step) } || required_steps.first
73
+ end
74
+
71
75
  def previous_step(step)
72
76
  index = required_steps.index(step)
73
77
  required_steps[index-1] unless index == 0 || index.nil?
@@ -15,8 +15,8 @@ module Effective
15
15
  include Effective::Resources::Sql
16
16
 
17
17
  # post, Post, Admin::Post, admin::Post, admin/posts, admin/post, admin/effective::post
18
- def initialize(input, namespace: nil, &block)
19
- _initialize_input(input, namespace: namespace)
18
+ def initialize(input, namespace: nil, relation: nil, &block)
19
+ _initialize_input(input, namespace: namespace, relation: relation)
20
20
  _initialize_model(&block) if block_given?
21
21
  self
22
22
  end
@@ -12,8 +12,8 @@ module Effective
12
12
  def routes
13
13
  @routes ||= (
14
14
  matches = [
15
- [namespace, plural_name].compact.join('/'),
16
- [namespace, name].compact.join('/')
15
+ [namespace, route_name.pluralize].compact.join('/'),
16
+ [namespace, route_name].compact.join('/'),
17
17
  ]
18
18
 
19
19
  # Check main Rails app
@@ -21,9 +21,25 @@ module Effective
21
21
  (matches & [route.defaults[:controller]]).present? && !route.name.to_s.end_with?('root')
22
22
  end
23
23
 
24
+ if routes.blank?
25
+ matches = [
26
+ [namespace, plural_name].compact.join('/'),
27
+ [namespace, name].compact.join('/')
28
+ ]
29
+
30
+ # Check main Rails app
31
+ routes = Rails.application.routes.routes.select do |route|
32
+ (matches & [route.defaults[:controller]]).present? && !route.name.to_s.end_with?('root')
33
+ end
34
+ end
35
+
24
36
  # Check engine routes
25
37
  if routes.blank?
26
38
  matches = [
39
+ [namespace, route_name.pluralize].compact.join('/'),
40
+ [namespace, route_name].compact.join('/'),
41
+ ['effective', namespace, route_name.pluralize].compact.join('/'),
42
+ ['effective', namespace, route_name].compact.join('/'),
27
43
  [namespace, plural_name].compact.join('/'),
28
44
  [namespace, name].compact.join('/'),
29
45
  ['effective', namespace, plural_name].compact.join('/'),
@@ -35,7 +51,11 @@ module Effective
35
51
  (matches & [route.defaults[:controller]]).present? && !route.name.to_s.end_with?('root')
36
52
  end
37
53
 
38
- break if routes.present?
54
+ if routes.present?
55
+ @routes_app = engine
56
+ break
57
+ end
58
+
39
59
  end
40
60
  end
41
61
 
@@ -43,6 +63,14 @@ module Effective
43
63
  )
44
64
  end
45
65
 
66
+ def routes_app
67
+ @routes_app if routes.present?
68
+ end
69
+
70
+ def url_helpers
71
+ (routes_app || Rails.application).routes.url_helpers
72
+ end
73
+
46
74
  # Effective::Resource.new('admin/posts').action_path_helper(:edit) => 'edit_admin_posts_path'
47
75
  # This will return empty for create, update and destroy
48
76
  def action_path_helper(action)
@@ -69,12 +69,12 @@ module Effective
69
69
  active_storage_has_ones.map { |ass| ass.name.to_s.gsub(/_attachment\z/, '').to_sym }
70
70
  end
71
71
 
72
- def active_texts
72
+ def action_texts
73
73
  klass.reflect_on_all_associations(:has_one).select { |ass| ass.class_name == 'ActionText::RichText' }
74
74
  end
75
75
 
76
- def active_texts_has_ones_ids
77
- active_texts.map { |ass| ass.name.to_s.gsub(/\Arich_text_/, '').to_sym }
76
+ def action_texts_has_ones_ids
77
+ action_texts.map { |ass| ass.name.to_s.gsub(/\Arich_text_/, '').to_sym }
78
78
  end
79
79
 
80
80
  def nested_resources
@@ -54,7 +54,7 @@ module Effective
54
54
 
55
55
  def active_text_attributes
56
56
  {}.tap do |retval|
57
- active_texts_has_ones_ids.each { |k, v| retval[k] = [:string] }
57
+ action_texts_has_ones_ids.each { |k, v| retval[k] = [:string] }
58
58
  end
59
59
  end
60
60
 
@@ -4,9 +4,9 @@ module Effective
4
4
 
5
5
  private
6
6
 
7
- def _initialize_input(input, namespace: nil)
7
+ def _initialize_input(input, namespace: nil, relation: nil)
8
8
  @initialized_name = input
9
- @model_klass = _klass_by_input(input)
9
+ @model_klass = (relation ? _klass_by_input(relation) : _klass_by_input(input))
10
10
 
11
11
  # Consider namespaces
12
12
  if namespace
@@ -18,20 +18,24 @@ module Effective
18
18
  end
19
19
 
20
20
  # Consider relation
21
+ if relation.kind_of?(ActiveRecord::Relation)
22
+ @relation ||= relation
23
+ end
24
+
21
25
  if input.kind_of?(ActiveRecord::Relation)
22
26
  @relation ||= input
23
27
  end
24
28
 
25
29
  if input.kind_of?(ActiveRecord::Reflection::MacroReflection) && input.scope
26
- @relation ||= klass.where(nil).merge(input.scope)
30
+ @relation ||= @model_klass.where(nil).merge(input.scope)
27
31
  end
28
32
 
29
33
  # Consider instance
30
- if klass && input.instance_of?(klass)
34
+ if @model_klass && input.instance_of?(@model_klass)
31
35
  @instance ||= input
32
36
  end
33
37
 
34
- if klass && input.kind_of?(Array) && input.last.instance_of?(klass)
38
+ if @model_klass && input.kind_of?(Array) && input.last.instance_of?(@model_klass)
35
39
  @instance ||= input.last
36
40
  end
37
41
  end
@@ -49,7 +49,7 @@ module Effective
49
49
  end
50
50
  end
51
51
 
52
- has_ones.each do |association|
52
+ (action_texts + has_ones).each do |association|
53
53
  next if except.present? && except.include?(association.name)
54
54
  next unless only.blank? || only.include?(association.name)
55
55
 
@@ -74,9 +74,22 @@ module Effective
74
74
  attributes.delete_if { |_, value| value.blank? }
75
75
  end
76
76
 
77
+ def instance_action_texts_previous_changes
78
+ return {} unless instance.present? && action_texts.present?
79
+
80
+ action_texts
81
+ .map { |ass| instance.send(ass.name) }
82
+ .compact
83
+ .select { |obj| obj.previous_changes['body'].present? }
84
+ .inject({}) { |h, obj| h[obj.name.to_sym] = obj.previous_changes['body']; h }
85
+ end
86
+
77
87
  # used by effective_logging
78
88
  def instance_changes(only: nil, except: nil)
79
- return {} unless (instance.present? && instance.previous_changes.present?)
89
+ return {} unless instance.present?
90
+
91
+ action_texts_changes = instance_action_texts_previous_changes()
92
+ return {} unless instance.previous_changes.present? || action_texts_changes.present?
80
93
 
81
94
  # Build up our only and except
82
95
  only = Array(only).map(&:to_sym)
@@ -94,6 +107,13 @@ module Effective
94
107
  changes = changes.except(*except) if except.present?
95
108
  changes = changes.slice(*only) if only.present?
96
109
 
110
+ action_texts_changes.each do |name, (before, after)|
111
+ next if except.present? && except.include?(name)
112
+ next unless only.blank? || only.include?(name)
113
+
114
+ changes[name] = [before.to_s, after.to_s]
115
+ end
116
+
97
117
  # Log to_s changes on all belongs_to associations
98
118
  belong_tos.each do |association|
99
119
  next if except.present? && except.include?(association.name)
@@ -110,7 +130,3 @@ module Effective
110
130
  end
111
131
  end
112
132
  end
113
-
114
-
115
-
116
-
@@ -19,6 +19,10 @@ module Effective
19
19
  @initialized_name
20
20
  end
21
21
 
22
+ def route_name # 'post' initialized from the controller_path/initialized_name and not the class
23
+ @route_name ||= (initialized_name.to_s.split(SPLIT).last || '').singularize.underscore
24
+ end
25
+
22
26
  def class_name # 'Effective::Post'
23
27
  @model_klass ? @model_klass.name : name.classify
24
28
  end
@@ -44,11 +48,11 @@ module Effective
44
48
  end
45
49
 
46
50
  def human_name
47
- class_name.gsub('::', ' ').underscore.gsub('_', ' ')
51
+ name.gsub('::', ' ').underscore.gsub('_', ' ')
48
52
  end
49
53
 
50
54
  def human_plural_name
51
- class_name.pluralize.gsub('::', ' ').underscore.gsub('_', ' ')
55
+ name.pluralize.gsub('::', ' ').underscore.gsub('_', ' ')
52
56
  end
53
57
 
54
58
  end
@@ -4,8 +4,10 @@ module Effective
4
4
  TARGET_LIST_LIMIT = 1500
5
5
  TARGET_KEYS_LIMIT = 30000
6
6
 
7
+ # This could be active_model? in which we just return the klass itself here
8
+ # This value ends up being crud_controller resource_scope()
7
9
  def relation
8
- @relation ||= klass.where(nil)
10
+ @relation ||= (klass.respond_to?(:where) ? klass.where(nil) : klass)
9
11
  end
10
12
 
11
13
  # When Effective::Resource is initialized with an ActiveRecord relation, the following
@@ -51,9 +51,11 @@ module EffectiveResources
51
51
  # Register the acts_as_archived routes concern
52
52
  # resources :things, concerns: :acts_as_archived
53
53
  initializer 'effective_resources.routes_concern' do |app|
54
- ActiveSupport.on_load :action_controller_base do
55
- ActionDispatch::Routing::Mapper.include(ActsAsArchived::RoutesConcern)
56
- end
54
+ ActionDispatch::Routing::Mapper.include(ActsAsArchived::RoutesConcern)
55
+
56
+ # Doesn't seem to work with the on_load in rails 6.0
57
+ #ActiveSupport.on_load :action_controller_base do
58
+ #end
57
59
  end
58
60
 
59
61
  # Register the flash_messages concern so that it can be called in ActionController
@@ -1,3 +1,3 @@
1
1
  module EffectiveResources
2
- VERSION = '1.6.6'.freeze
2
+ VERSION = '1.7.4'.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.6.6
4
+ version: 1.7.4
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: 2020-12-31 00:00:00.000000000 Z
11
+ date: 2021-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -132,6 +132,7 @@ files:
132
132
  - app/controllers/concerns/effective/wizard_controller/actions.rb
133
133
  - app/controllers/concerns/effective/wizard_controller/before_actions.rb
134
134
  - app/controllers/concerns/effective/wizard_controller/save.rb
135
+ - app/controllers/concerns/effective/wizard_controller/wicked_overrides.rb
135
136
  - app/helpers/effective_resources_helper.rb
136
137
  - app/helpers/effective_resources_private_helper.rb
137
138
  - app/helpers/effective_resources_wizard_helper.rb