effective_resources 1.6.1 → 1.6.6

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: 8623ea704325f309e92ac90f7df48786d1cb44ce3f050628f094ac90a5a74590
4
- data.tar.gz: 0c08de0bfeb8345152a89a68ab9031446ea455b3d6bcdb9cd39b5eb456be6baf
3
+ metadata.gz: 323b70f4a0eb470c2e96edf3651c83493763ad203659f03bb25832cf487812a1
4
+ data.tar.gz: f997c5dcd62f4de1d49065c5fb3d5d882ea3eca30c47fe518bb6642dbd43e65a
5
5
  SHA512:
6
- metadata.gz: 467e9ca96fca55a286f6104ebb1bb5b32821d796fe31531726114fa723344cbad3cfa3371998759dbd5a75688f6cbffe08345b9053ef756aacf972436340019c
7
- data.tar.gz: 97bb18a3ea794fe2be6696096b6636f37522a06d1e67a782e1b17794088f60d5132edb736506c50067fc3686502c9768a4e5a640e845cdd62d6c255b08f7505d
6
+ metadata.gz: 4d78587039acac7fa236913c588946277481a5df90933141db13a7d447491233452f8dd6c3c96ebf7bff8dfd95ba217e56c65e826128d9674532331d6971422c
7
+ data.tar.gz: daff8c0e7009dd67297f2e62a90744b7dcc67008f107543b53c53b24476ca8534284202aae98b0579e394fddbdbb498925b0294f47c95e70f9bbf8b551cab4a3
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:
@@ -71,6 +71,10 @@ module Effective
71
71
  effective_resource.name
72
72
  end
73
73
 
74
+ def resource_name_id
75
+ (effective_resource.name + '_id').to_sym
76
+ end
77
+
74
78
  def resource_klass # Thing
75
79
  effective_resource.klass
76
80
  end
@@ -63,8 +63,13 @@ module Effective
63
63
  self.resource ||= resource_scope.new
64
64
  action = (commit_action[:action] == :save ? :create : commit_action[:action])
65
65
 
66
- resource.current_user ||= current_user if resource.respond_to?(:current_user=)
67
- resource.created_by ||= current_user if resource.respond_to?(:created_by=)
66
+ if respond_to?(:current_user) && resource.respond_to?(:current_user=)
67
+ resource.current_user ||= current_user
68
+ end
69
+
70
+ if respond_to?(:current_user) && resource.respond_to?(:created_by=)
71
+ resource.created_by ||= current_user
72
+ end
68
73
 
69
74
  resource.assign_attributes(send(resource_params_method_name))
70
75
 
@@ -121,8 +126,13 @@ module Effective
121
126
  EffectiveResources.authorize!(self, action, resource)
122
127
  @page_title ||= "Edit #{resource}"
123
128
 
124
- resource.current_user ||= current_user if resource.respond_to?(:current_user=)
125
- resource.updated_by ||= current_user if resource.respond_to?(:updated_by=)
129
+ if respond_to?(:current_user) && resource.respond_to?(:current_user=)
130
+ resource.current_user ||= current_user
131
+ end
132
+
133
+ if respond_to?(:current_user) && resource.respond_to?(:updated_by=)
134
+ resource.updated_by ||= current_user
135
+ end
126
136
 
127
137
  resource.assign_attributes(send(resource_params_method_name))
128
138
 
@@ -96,7 +96,7 @@ module Effective
96
96
 
97
97
  if (nested_params = permitted_params_for(nested.klass, namespaces)).present?
98
98
  nested_params.insert(nested_params.rindex { |obj| !obj.kind_of?(Hash)} + 1, :_destroy)
99
- permitted_params << { "#{nested.resource_name}_attributes".to_sym => nested_params }
99
+ permitted_params << { "#{nested.name}_attributes".to_sym => nested_params }
100
100
  end
101
101
  end
102
102
 
@@ -24,7 +24,9 @@ module Effective
24
24
  save_action = ([:create, :update].include?(action) ? :save : action)
25
25
  raise "expected @#{resource_name} to respond to #{save_action}!" unless resource.respond_to?("#{save_action}!")
26
26
 
27
- resource.current_user ||= current_user if resource.respond_to?(:current_user=)
27
+ if respond_to?(:current_user) && resource.respond_to?(:current_user=)
28
+ resource.current_user ||= current_user
29
+ end
28
30
 
29
31
  success = false
30
32
 
@@ -0,0 +1,63 @@
1
+ module Effective
2
+ module WizardController
3
+ extend ActiveSupport::Concern
4
+
5
+ include Wicked::Wizard if defined?(Wicked)
6
+ include Effective::CrudController
7
+
8
+ include Effective::WizardController::Actions
9
+ include Effective::WizardController::BeforeActions
10
+ include Effective::WizardController::Save
11
+
12
+ included do
13
+ raise("please install gem 'wicked' to use Effective::WizardController") unless defined?(Wicked)
14
+
15
+ with_options(only: [:show, :update]) do
16
+ before_action :redirect_if_blank_step
17
+
18
+ before_action :assign_resource
19
+ before_action :authorize_resource
20
+ before_action :assign_required_steps
21
+ before_action :setup_wizard # Wicked
22
+
23
+ before_action :enforce_can_visit_step
24
+
25
+ before_action :assign_current_step
26
+ before_action :assign_page_title
27
+ end
28
+
29
+ helper_method :resource
30
+ helper_method :resource_wizard_step_title
31
+
32
+ helper EffectiveResourcesWizardHelper
33
+
34
+ rescue_from Wicked::Wizard::InvalidStepError do |exception|
35
+ flash[:danger] = "Unknown step. You have been moved to the #{resource_wizard_steps.first} step."
36
+ redirect_to wizard_path(resource_wizard_steps.first)
37
+ end
38
+ end
39
+
40
+ def find_wizard_resource
41
+ if params[resource_name_id] && params[resource_name_id] != 'new'
42
+ resource_scope.find(params[resource_name_id])
43
+ else
44
+ resource_scope.new
45
+ end
46
+ end
47
+
48
+ def resource_wizard_step_title(step)
49
+ return if step == 'wicked_finish'
50
+ effective_resource.klass.const_get(:WIZARD_STEPS).fetch(step)
51
+ end
52
+
53
+ def resource_wizard_steps
54
+ effective_resource.klass.const_get(:WIZARD_STEPS).keys
55
+ end
56
+
57
+ 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)
60
+ end
61
+
62
+ end
63
+ end
@@ -0,0 +1,30 @@
1
+ module Effective
2
+ module WizardController
3
+ module Actions
4
+
5
+ def new
6
+ Rails.logger.info 'Processed by Effective::WizardController#new'
7
+
8
+ self.resource ||= resource_scope.new
9
+ EffectiveResources.authorize!(self, :new, resource)
10
+
11
+ redirect_to resource_wizard_path(:new, resource_wizard_steps.first)
12
+ end
13
+
14
+ def show
15
+ Rails.logger.info 'Processed by Effective::WizardController#show'
16
+
17
+ render_wizard
18
+ end
19
+
20
+ def update
21
+ Rails.logger.info 'Processed by Effective::WizardController#update'
22
+
23
+ resource.assign_attributes(send(resource_params_method_name))
24
+
25
+ save_wizard_resource(resource)
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,74 @@
1
+ module Effective
2
+ module WizardController
3
+ module BeforeActions
4
+
5
+ # before_action :redirect_if_blank_step, only: [:show]
6
+ # When I visit /resources/1, redirect to /resources/1/build/step
7
+ def redirect_if_blank_step
8
+ if params[:id].present? && params[resource_name_id].blank?
9
+ params[resource_name_id] = params[:id]
10
+
11
+ assign_resource()
12
+
13
+ step = (resource.first_uncompleted_step || resource_wizard_steps.last)
14
+ redirect_to resource_wizard_path(resource, step)
15
+ end
16
+ end
17
+
18
+ # before_action :assign_resource, only: [:show, :update]
19
+ # Assigns the resource
20
+ def assign_resource
21
+ self.resource ||= find_wizard_resource
22
+ end
23
+
24
+ # before_action :authorize_resource, only: [:show, :update]
25
+ # Authorize the resource
26
+ def authorize_resource
27
+ EffectiveResources.authorize!(self, action_name.to_sym, resource)
28
+ end
29
+
30
+ # before_action :assign_required_steps, only: [:show, :update]
31
+ # Assign the required steps to Wickeds (dynamic steps)
32
+ def assign_required_steps
33
+ self.steps = resource.required_steps
34
+ end
35
+
36
+ # setup_wizard from Wicked called now
37
+
38
+ # before_action :enforce_can_visit_step, only: [:show, :update]
39
+ # Make sure I have permission for this step
40
+ def enforce_can_visit_step
41
+ return if step == 'wicked_finish'
42
+ return if resource.can_visit_step?(step)
43
+
44
+ next_step = wizard_steps.reverse.find { |step| resource.can_visit_step?(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
+
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."
53
+ redirect_to wizard_path(next_step)
54
+ end
55
+
56
+ # before_action :assign_current_step, only: [:show, :update]
57
+ # Assign the urrent step to resource
58
+ def assign_current_step
59
+ if respond_to?(:current_user) && resource.respond_to?(:current_user=)
60
+ resource.current_user = current_user
61
+ end
62
+
63
+ resource.current_step = step.to_sym
64
+ end
65
+
66
+ # before_action :assign_page_title, only: [:show, :update]
67
+ # Assign page title
68
+ def assign_page_title
69
+ @page_title ||= resource_wizard_step_title(step)
70
+ end
71
+
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,24 @@
1
+ module Effective
2
+ module WizardController
3
+ module Save
4
+
5
+ def save_wizard_resource(resource, action = nil, options = {})
6
+ was_new_record = resource.new_record?
7
+ action ||= resource.respond_to?("#{step}!") ? step : :save
8
+
9
+ if save_resource(resource, action)
10
+ flash[:success] = options.delete(:success) || resource_flash(:success, resource, action)
11
+
12
+ @skip_to ||= next_step
13
+ @redirect_to ||= resource_wizard_path(resource, @skip_to) if was_new_record
14
+
15
+ redirect_to(@redirect_to || wizard_path(@skip_to))
16
+ else
17
+ flash.now[:danger] = options.delete(:error) || resource_flash(:danger, resource, action)
18
+ render_step(wizard_value(step), options)
19
+ end
20
+ end
21
+
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EffectiveResourcesWizardHelper
4
+
5
+ def render_wizard_sidebar(resource, numbers: true, &block)
6
+ sidebar = content_tag(:div, class: 'nav list-group wizard-sidebar') do
7
+ resource.required_steps.map.with_index do |nav_step, index|
8
+ render_wizard_sidebar_item(resource, nav_step, (index + 1 if numbers))
9
+ end.join.html_safe
10
+ end
11
+
12
+ return sidebar unless block_given?
13
+
14
+ content_tag(:div, class: 'row') do
15
+ content_tag(:div, class: 'col-3') { sidebar } +
16
+ content_tag(:div, class: 'col-9') { yield }
17
+ end
18
+ end
19
+
20
+ def render_wizard_sidebar_item(resource, nav_step, index = nil)
21
+ # From Controller
22
+ current = (nav_step == step)
23
+ title = resource_wizard_step_title(nav_step)
24
+
25
+ # From Model
26
+ disabled = !resource.can_visit_step?(nav_step)
27
+
28
+ label = [index, title].compact.join('. ')
29
+ klass = ['list-group-item', ('active' if current), ('disabled' if disabled && !current)].compact.join(' ')
30
+
31
+ if (current || disabled)
32
+ content_tag(:li, label, class: klass)
33
+ else
34
+ link_to(label, wizard_path(nav_step), class: klass)
35
+ end
36
+ end
37
+
38
+ end
@@ -0,0 +1,103 @@
1
+ # ActsAsWizard
2
+ # Works alongside wicked gem to build a wizard
3
+ # https://github.com/zombocom/wicked
4
+
5
+ # acts_as_wizard(start: 'Start Step', select: 'Select Step', finish: 'Finished')
6
+
7
+ module ActsAsWizard
8
+ extend ActiveSupport::Concern
9
+
10
+ module Base
11
+ def acts_as_wizard(steps)
12
+ raise 'acts_as_wizard expected a Hash of steps' unless steps.kind_of?(Hash)
13
+
14
+ unless steps.all? { |k, v| k.kind_of?(Symbol) && v.kind_of?(String) }
15
+ raise 'acts_as_wizard expected a Hash of symbol => String steps'
16
+ end
17
+
18
+ @acts_as_wizard_options = {steps: steps}
19
+
20
+ include ::ActsAsWizard
21
+ end
22
+ end
23
+
24
+ included do
25
+ acts_as_wizard_options = @acts_as_wizard_options
26
+
27
+ attr_accessor :current_step
28
+ attr_accessor :current_user
29
+
30
+ if Rails.env.test? # So our tests can override the required_steps method
31
+ cattr_accessor :test_required_steps
32
+ end
33
+
34
+ const_set(:WIZARD_STEPS, acts_as_wizard_options[:steps])
35
+
36
+ effective_resource do
37
+ wizard_steps :text, permitted: false
38
+ end
39
+
40
+ serialize :wizard_steps, Hash
41
+
42
+ before_save(if: -> { current_step.present? }) do
43
+ wizard_steps[current_step.to_sym] ||= Time.zone.now
44
+ end
45
+
46
+ def can_visit_step?(step)
47
+ can_revisit_completed_steps(step)
48
+ end
49
+
50
+ def required_steps
51
+ return self.class.test_required_steps if Rails.env.test? && self.class.test_required_steps.present?
52
+ self.class.const_get(:WIZARD_STEPS).keys
53
+ end
54
+
55
+ def first_completed_step
56
+ required_steps.find { |step| has_completed_step?(step) }
57
+ end
58
+
59
+ def last_completed_step
60
+ required_steps.reverse.find { |step| has_completed_step?(step) }
61
+ end
62
+
63
+ def first_uncompleted_step
64
+ required_steps.find { |step| has_completed_step?(step) == false }
65
+ end
66
+
67
+ def has_completed_step?(step)
68
+ wizard_steps[step].present?
69
+ end
70
+
71
+ def previous_step(step)
72
+ index = required_steps.index(step)
73
+ required_steps[index-1] unless index == 0 || index.nil?
74
+ end
75
+
76
+ def has_completed_previous_step?(step)
77
+ previous = previous_step(step)
78
+ previous.blank? || has_completed_step?(previous)
79
+ end
80
+
81
+ def has_completed_last_step?
82
+ has_completed_step?(required_steps.last)
83
+ end
84
+
85
+ private
86
+
87
+ def can_revisit_completed_steps(step)
88
+ return (step == required_steps.last) if has_completed_last_step?
89
+ has_completed_previous_step?(step)
90
+ end
91
+
92
+ def cannot_revisit_completed_steps(step)
93
+ return (step == required_steps.last) if has_completed_last_step?
94
+ has_completed_previous_step?(step) && !has_completed_step?(step)
95
+ end
96
+
97
+ end
98
+
99
+ module ClassMethods
100
+ def acts_as_wizard?; true; end
101
+ end
102
+
103
+ end
@@ -85,13 +85,15 @@ module Effective
85
85
  h[part] = target.public_send(part)
86
86
  end
87
87
  end
88
- else
88
+ elsif target.respond_to?(:to_param) || target.respond_to?(:id)
89
89
  target
90
+ else
91
+ {id: target}
90
92
  end
91
93
  end
92
94
 
93
95
  # Generate the path
94
- path = routes[action].format(formattable || EMPTY_HASH)
96
+ path = (routes[action].format(formattable || EMPTY_HASH) rescue nil)
95
97
 
96
98
  if path.present? && opts.present?
97
99
  uri = URI.parse(path)
@@ -26,6 +26,7 @@ module EffectiveResources
26
26
  ActiveRecord::Base.extend(ActsAsTokened::Base)
27
27
  ActiveRecord::Base.extend(ActsAsSlugged::Base)
28
28
  ActiveRecord::Base.extend(ActsAsStatused::Base)
29
+ ActiveRecord::Base.extend(ActsAsWizard::Base)
29
30
  ActiveRecord::Base.extend(EffectiveResource::Base)
30
31
  end
31
32
  end
@@ -50,7 +51,9 @@ module EffectiveResources
50
51
  # Register the acts_as_archived routes concern
51
52
  # resources :things, concerns: :acts_as_archived
52
53
  initializer 'effective_resources.routes_concern' do |app|
53
- ActionDispatch::Routing::Mapper.include(ActsAsArchived::RoutesConcern)
54
+ ActiveSupport.on_load :action_controller_base do
55
+ ActionDispatch::Routing::Mapper.include(ActsAsArchived::RoutesConcern)
56
+ end
54
57
  end
55
58
 
56
59
  # 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.1'.freeze
2
+ VERSION = '1.6.6'.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.1
4
+ version: 1.6.6
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-11-20 00:00:00.000000000 Z
11
+ date: 2020-12-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -94,6 +94,20 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: wicked
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
97
111
  description: Make any controller an effective resource controller.
98
112
  email:
99
113
  - info@codeandeffect.com
@@ -114,12 +128,18 @@ files:
114
128
  - app/controllers/concerns/effective/crud_controller/save.rb
115
129
  - app/controllers/concerns/effective/crud_controller/submits.rb
116
130
  - app/controllers/concerns/effective/flash_messages.rb
131
+ - app/controllers/concerns/effective/wizard_controller.rb
132
+ - app/controllers/concerns/effective/wizard_controller/actions.rb
133
+ - app/controllers/concerns/effective/wizard_controller/before_actions.rb
134
+ - app/controllers/concerns/effective/wizard_controller/save.rb
117
135
  - app/helpers/effective_resources_helper.rb
118
136
  - app/helpers/effective_resources_private_helper.rb
137
+ - app/helpers/effective_resources_wizard_helper.rb
119
138
  - app/models/concerns/acts_as_archived.rb
120
139
  - app/models/concerns/acts_as_slugged.rb
121
140
  - app/models/concerns/acts_as_statused.rb
122
141
  - app/models/concerns/acts_as_tokened.rb
142
+ - app/models/concerns/acts_as_wizard.rb
123
143
  - app/models/concerns/effective_resource.rb
124
144
  - app/models/effective/access_denied.rb
125
145
  - app/models/effective/action_failed.rb