effective_resources 1.6.4 → 1.7.2

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: 4bb7652d9f80993d8147a2256189201111268282d81f881172df757474113e39
4
- data.tar.gz: 101536d12175828d0ae1008e2d6dbf226e3f594a93c55f7566f89a17d3bcd1c8
3
+ metadata.gz: a3a189848d1642d5a68d1bbae816eddf3bd4d51513d28bd369a02aa1843b5b9a
4
+ data.tar.gz: ab0af0abaefa639595858f4eee2356d2ba2b96434c3d493d6a1e1cdd49877bf1
5
5
  SHA512:
6
- metadata.gz: 360ae9e38369c840302b06696686e3e3f64ed4375fdc651571f29208c437281248c02ab8aed272063e3f3e0fd6ab999e891bbe368e60f93ff05cbc27521abffa
7
- data.tar.gz: 5cfa3f62f33ec8f0d2b1c2ea11806634964af86f9b19c2088ea184c1cf528bcabf3d7c4c0c873125ab90d60eb8a794ca376c91d78a05cd57f43137d6f827567f
6
+ metadata.gz: 660470293ca5400697b468ea97a95458a46b323ba6399ce35dea43dc2f535afdf86cae6c7f341cfea69a8ca13f54cbd156bc80fcb0d9fe11ec3c97e5c1aafbc7
7
+ data.tar.gz: 7e479eef9412d2195465f1cf4194f594dba7bcb5ec47179730098042cda8d151cc855fe52d09972ef9fad0e70a104ec982af49f13ea5e9e7b6d3c9f77bc35e02
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
@@ -42,14 +42,19 @@ module Effective
42
42
  return if resource.can_visit_step?(step)
43
43
 
44
44
  next_step = wizard_steps.reverse.find { |step| resource.can_visit_step?(step) }
45
- raise('There is no wizard step to visit') unless next_step
45
+ raise('There is no wizard step to visit. Make sure can_visit_step?(step) returns true for at least one step') unless next_step
46
46
 
47
- flash[:danger] = "You have been redirected to the #{resource_wizard_step_title(next_step)} step."
47
+ if Rails.env.development?
48
+ Rails.logger.info " \e[31m\e[1mFAILED\e[0m\e[22m" # bold red
49
+ Rails.logger.info " Unable to visit step :#{step}. Last can_visit_step? is :#{next_step}. Change the acts_as_wizard model's can_visit_step?(step) function to change this."
50
+ end
51
+
52
+ flash[:success] = "You have been redirected to the #{resource_wizard_step_title(next_step)} step."
48
53
  redirect_to wizard_path(next_step)
49
54
  end
50
55
 
51
56
  # before_action :assign_current_step, only: [:show, :update]
52
- # Assign the urrent step to resource
57
+ # Assign the current step to resource
53
58
  def assign_current_step
54
59
  if respond_to?(:current_user) && resource.respond_to?(:current_user=)
55
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
@@ -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)
@@ -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
@@ -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.4'.freeze
2
+ VERSION = '1.7.2'.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.4
4
+ version: 1.7.2
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-08 00:00:00.000000000 Z
11
+ date: 2021-01-21 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