Class: RailsOnboarding::SkipLogic
- Inherits:
-
Object
- Object
- RailsOnboarding::SkipLogic
- Defined in:
- lib/rails_onboarding/skip_logic.rb
Overview
Skip Logic Service Allows conditional step skipping based on user data and custom conditions
Class Method Summary collapse
-
.auto_skip_step(user, step_name) ⇒ Hash?
Check if user should auto-skip to next step.
-
.evaluate_conditions(user, conditions) ⇒ Boolean
Evaluate skip conditions.
-
.next_unskipped_step(user, current_step_name) ⇒ Hash?
Get next unskipped step.
-
.progress_excluding_skipped(user) ⇒ Integer
Calculate actual progress excluding skipped steps.
-
.required_steps(user) ⇒ Array<Hash>
Get all steps that are required for a user.
-
.should_skip_step?(user, step) ⇒ Boolean
Check if a step should be skipped for a user.
-
.skippable_steps(user) ⇒ Array<Hash>
Get all steps that should be skipped for a user.
Class Method Details
.auto_skip_step(user, step_name) ⇒ Hash?
Check if user should auto-skip to next step
101 102 103 104 105 106 107 108 |
# File 'lib/rails_onboarding/skip_logic.rb', line 101 def auto_skip_step(user, step_name) step = RailsOnboarding.configuration.step_by_name(step_name) return nil unless step if should_skip_step?(user, step) && step[:auto_skip] next_unskipped_step(user, step_name) end end |
.evaluate_conditions(user, conditions) ⇒ Boolean
Evaluate skip conditions
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rails_onboarding/skip_logic.rb', line 26 def evaluate_conditions(user, conditions) case conditions when Proc # Call proc with user conditions.call(user) when Symbol # Call method on user user.respond_to?(conditions) && user.send(conditions) when Hash # Evaluate hash conditions evaluate_hash_conditions(user, conditions) else false end rescue StandardError => e Rails.logger.error("Error evaluating skip conditions: #{e.}") false end |
.next_unskipped_step(user, current_step_name) ⇒ Hash?
Get next unskipped step
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rails_onboarding/skip_logic.rb', line 50 def next_unskipped_step(user, current_step_name) current_index = RailsOnboarding.configuration.step_index(current_step_name) return nil unless current_index steps = RailsOnboarding.configuration.steps remaining_steps = steps[(current_index + 1)..-1] return nil unless remaining_steps remaining_steps.find { |step| !should_skip_step?(user, step) } end |
.progress_excluding_skipped(user) ⇒ Integer
Calculate actual progress excluding skipped steps
86 87 88 89 90 91 92 93 94 |
# File 'lib/rails_onboarding/skip_logic.rb', line 86 def progress_excluding_skipped(user) required = required_steps(user) return 100 if required.empty? current_index = required.find_index { |s| s[:name] == user.onboarding_current_step&.to_sym } return 0 unless current_index ((current_index + 1).to_f / required.size * 100).round end |
.required_steps(user) ⇒ Array<Hash>
Get all steps that are required for a user
76 77 78 79 80 |
# File 'lib/rails_onboarding/skip_logic.rb', line 76 def required_steps(user) RailsOnboarding.configuration.steps.reject do |step| should_skip_step?(user, step) end end |
.should_skip_step?(user, step) ⇒ Boolean
Check if a step should be skipped for a user
11 12 13 14 15 16 17 18 19 |
# File 'lib/rails_onboarding/skip_logic.rb', line 11 def should_skip_step?(user, step) return false unless step.is_a?(Hash) # Check if step has skip conditions skip_conditions = step[:skip_if] return false unless skip_conditions evaluate_conditions(user, skip_conditions) end |
.skippable_steps(user) ⇒ Array<Hash>
Get all steps that should be skipped for a user
66 67 68 69 70 |
# File 'lib/rails_onboarding/skip_logic.rb', line 66 def skippable_steps(user) RailsOnboarding.configuration.steps.select do |step| should_skip_step?(user, step) end end |