effective_resources 1.6.5 → 1.7.3

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: 638c0fd7b0f6e8ad8cbcdfe19a1d10e3824a2767734371e7ee744687181eb773
4
- data.tar.gz: 25bf07d2126c62e65092cbeb4113a8df0017188fd5f17108b425cf8aa4ff09b0
3
+ metadata.gz: a63a4abae27e2b70ac922d2f3eca07c701842b9ef2482677d200236f05a05dc8
4
+ data.tar.gz: 136206e80f42d5c019f9113d46c3260d19115f7f40ef58f0064f74b57283167f
5
5
  SHA512:
6
- metadata.gz: 86e11947ca25724dfb6255bd77129b92b87780f8199257634ebdd6841406165ba952d4ddad329608d15685102c5e216b9afeae6cffc51052c05b9564881ec749
7
- data.tar.gz: 70608699672fe60a576f96987224accdddeffeef6248d775c2f6f4481ef1b69c42edddc14e20cb953025098e041d9e354fdb354dfa0a6b99a25a18218f9a5d81
6
+ metadata.gz: 2b08dd74cfa1605c8dcf8e8082612b078ff20b93f9bfbb931f41b8d58ec2795f4a6ca0ed13f837860df4b3166c2069136ab26e0b19a46fa812af2293674bb56a
7
+ data.tar.gz: a64adaf287722c737951827174f1074ad0580688540944551f406d66fb28dadf74ca7c845c57f008aea6036c355655674328c37c89cc90be36f40738753db436
data/README.md CHANGED
@@ -256,6 +256,102 @@ end
256
256
 
257
257
  and include Effective::CrudController in your resource controller.
258
258
 
259
+ ### acts_as_wizard
260
+
261
+ Build up an object through a wizard.
262
+
263
+ Works with the [wicked](https://github.com/zombocom/wicked) gem to create wizard quickly.
264
+
265
+ Create a model and define `acts_as_wizard`:
266
+
267
+ ```ruby
268
+ class Thing < ApplicationRecord
269
+ acts_as_wizard(
270
+ start: 'Start',
271
+ select: 'Select',
272
+ finish: 'Finish'
273
+ )
274
+
275
+ effective_resource do
276
+ title :string
277
+ wizard_steps :text, permitted: false
278
+ end
279
+
280
+ validates :title, presence: true
281
+
282
+ def to_s
283
+ title.presence || 'New Thing'
284
+ end
285
+
286
+ # If you define a bang method matching the name of a step
287
+ # it will be called when that step is submitted.
288
+ # Otherwise save! is called.
289
+ def select!
290
+ ApplicationMailer.selected(self).deliver_later
291
+ save!
292
+ end
293
+
294
+ # An array of steps that the controller will use
295
+ # Default value is just all of them. But you can customize here
296
+ # def required_steps
297
+ # steps = WIZARD_STEPS.keys
298
+ # selectable? ? steps : steps - [:select]
299
+ # end
300
+
301
+ # Control whether the user has permission to visit this step
302
+ #
303
+ # This is the default, can go forward or back:
304
+ #
305
+ # def can_visit_step?(step)
306
+ # can_revisit_completed_steps(step)
307
+ # end
308
+ #
309
+ # Easy change if you only want to go forward:
310
+ #
311
+ # def can_visit_step?(step)
312
+ # cannot_revisit_completed_steps(step)
313
+ # end
314
+ #
315
+ # or custom algorithm:
316
+ #
317
+ # def can_visit_step?(step)
318
+ # return false unless has_completed_previous_step?(step)
319
+ # return false if has_completed_step?(:finish) && step != :finish
320
+ # end
321
+ end
322
+ ```
323
+
324
+ In your routes:
325
+
326
+ ```ruby
327
+ resources :things, only: [:index, :show, :new, :destroy] do
328
+ resources :build, controller: :things, only: [:show, :update]
329
+ end
330
+ ```
331
+
332
+ Make a controller:
333
+
334
+ ```ruby
335
+ class ThingsController < ApplicationController
336
+ include Effective::WizardController
337
+ end
338
+ ```
339
+
340
+ And then create one view per step.
341
+
342
+ Here's `views/things/start.html.haml`:
343
+
344
+ ```haml
345
+ = render_wizard_sidebar(resource) do
346
+ %h1= @page_title
347
+
348
+ = effective_form_with(model: resource, url: wizard_path(step), method: :put) do |f|
349
+ = f.text_field :title
350
+ = f.submit 'Save and Continue'
351
+ ```
352
+
353
+ You can also call `render_wizard_sidebar(resource)` without the block syntax.
354
+
259
355
  ## Testing
260
356
 
261
357
  Run tests by:
@@ -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
@@ -2,18 +2,17 @@ module Effective
2
2
  module WizardController
3
3
  extend ActiveSupport::Concern
4
4
 
5
- unless defined?(Wicked)
6
- raise("please install gem 'wicked' to use Effective::WizardController")
7
- end
8
-
9
- include Wicked::Wizard
5
+ include Wicked::Wizard if defined?(Wicked)
10
6
  include Effective::CrudController
11
7
 
12
8
  include Effective::WizardController::Actions
13
9
  include Effective::WizardController::BeforeActions
14
10
  include Effective::WizardController::Save
11
+ include Effective::WizardController::WickedOverrides
15
12
 
16
13
  included do
14
+ raise("please install gem 'wicked' to use Effective::WizardController") unless defined?(Wicked)
15
+
17
16
  with_options(only: [:show, :update]) do
18
17
  before_action :redirect_if_blank_step
19
18
 
@@ -56,9 +55,11 @@ module Effective
56
55
  effective_resource.klass.const_get(:WIZARD_STEPS).keys
57
56
  end
58
57
 
58
+ # It could be :new, :start
59
+ # Or resource, step
59
60
  def resource_wizard_path(resource, step)
60
- path_helper = effective_resource.action_path_helper(:show).to_s.sub('_path', '_build_path')
61
- 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)
62
63
  end
63
64
 
64
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
@@ -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.5'.freeze
2
+ VERSION = '1.7.3'.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.5
4
+ version: 1.7.3
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-17 00:00:00.000000000 Z
11
+ date: 2021-02-02 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