Module: RailsOnboarding::Personalizable

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/rails_onboarding/personalizable.rb

Overview

Concern for personalization capabilities on User models Adapts onboarding flows based on user type, role, or other attributes

Examples:

Include in your User model

class User < ApplicationRecord
  include RailsOnboarding::Onboardable
  include RailsOnboarding::Personalizable
end

Configure personalized flows

RailsOnboarding.configure do |config|
  config.personalization_enabled = true
  config.user_type_method = :account_type
  config.personalized_flows = {
    individual: [
      { name: :welcome, title: "Welcome", icon: "🎉" },
      { name: :profile, title: "Setup Profile", icon: "👤" }
    ],
    business: [
      { name: :welcome, title: "Welcome", icon: "🎉" },
      { name: :company, title: "Company Info", icon: "🏢" },
      { name: :team, title: "Team Setup", icon: "👥" }
    ]
  }
end

Instance Method Summary collapse

Instance Method Details

#personalization_typeSymbol, String

Get the user's type for personalization

Examples:

user.personalization_type
# => :business

Returns:

  • (Symbol, String)

    The user type



44
45
46
47
48
49
50
51
# File 'app/models/concerns/rails_onboarding/personalizable.rb', line 44

def personalization_type
  return nil unless RailsOnboarding.configuration.personalization_enabled

  method_name = RailsOnboarding.configuration.user_type_method
  return nil unless respond_to?(method_name)

  send(method_name)
end

#personalized_first_step?Boolean

Check if this is the first step in the personalized flow

Returns:

  • (Boolean)


125
126
127
128
# File 'app/models/concerns/rails_onboarding/personalizable.rb', line 125

def personalized_first_step?
  return false if onboarding_current_step.nil?
  personalized_step_index(onboarding_current_step)&.zero? || false
end

#personalized_last_step?Boolean

Check if this is the last step in the personalized flow

Returns:

  • (Boolean)


133
134
135
136
137
138
139
140
# File 'app/models/concerns/rails_onboarding/personalizable.rb', line 133

def personalized_last_step?
  return false if onboarding_current_step.nil?

  current_index = personalized_step_index(onboarding_current_step)
  return false if current_index.nil?

  current_index == personalized_total_steps - 1
end

#personalized_next_stepHash?

Get the next step in the personalized flow

Returns:

  • (Hash, nil)

    Next step configuration or nil



98
99
100
101
102
103
104
105
106
107
108
# File 'app/models/concerns/rails_onboarding/personalizable.rb', line 98

def personalized_next_step
  return nil if onboarding_current_step.nil?

  current_index = personalized_step_index(onboarding_current_step)
  return nil if current_index.nil?

  next_index = current_index + 1
  return nil if next_index >= personalized_total_steps

  personalized_steps[next_index]
end

#personalized_previous_stepHash?

Get the previous step in the personalized flow

Returns:

  • (Hash, nil)

    Previous step configuration or nil



113
114
115
116
117
118
119
120
# File 'app/models/concerns/rails_onboarding/personalizable.rb', line 113

def personalized_previous_step
  return nil if onboarding_current_step.nil?

  current_index = personalized_step_index(onboarding_current_step)
  return nil if current_index.nil? || current_index.zero?

  personalized_steps[current_index - 1]
end

#personalized_progress_percentageInteger

Get the progress percentage for the personalized flow

Returns:

  • (Integer)

    Progress as percentage (0-100)



145
146
147
148
149
150
151
152
# File 'app/models/concerns/rails_onboarding/personalizable.rb', line 145

def personalized_progress_percentage
  return 0 if onboarding_current_step.nil?

  current_index = personalized_step_index(onboarding_current_step)
  return 0 if current_index.nil?

  ((current_index + 1).to_f / personalized_total_steps * 100).round
end

#personalized_recommendationsArray<Hash>

Get recommended next actions based on user type

Examples:

user.personalized_recommendations
# => [{title: "Invite Team", description: "...", action: :invite_team}]

Returns:

  • (Array<Hash>)

    Array of recommended actions



179
180
181
182
183
184
# File 'app/models/concerns/rails_onboarding/personalizable.rb', line 179

def personalized_recommendations
  return [] unless RailsOnboarding.configuration.personalization_enabled
  return [] unless respond_to?(:personalization_recommendations)

  send(:personalization_recommendations)
end

#personalized_step_by_name(name) ⇒ Hash?

Get a specific step from the personalized flow

Parameters:

  • name (String, Symbol)

    Step name

Returns:

  • (Hash, nil)

    Step configuration or nil



81
82
83
84
# File 'app/models/concerns/rails_onboarding/personalizable.rb', line 81

def personalized_step_by_name(name)
  return nil if name.nil?
  personalized_steps.find { |s| s[:name].to_s == name.to_s }
end

#personalized_step_index(name) ⇒ Integer?

Get the index of a step in the personalized flow

Parameters:

  • name (String, Symbol)

    Step name

Returns:

  • (Integer, nil)

    Step index or nil



90
91
92
93
# File 'app/models/concerns/rails_onboarding/personalizable.rb', line 90

def personalized_step_index(name)
  return nil if name.nil?
  personalized_steps.find_index { |s| s[:name].to_s == name.to_s }
end

#personalized_stepsArray<Hash>

Get the personalized onboarding steps for this user

Examples:

user.personalized_steps
# => [{name: :welcome, title: "Welcome"}, ...]

Returns:

  • (Array<Hash>)

    Array of step configurations



60
61
62
63
64
65
66
67
68
# File 'app/models/concerns/rails_onboarding/personalizable.rb', line 60

def personalized_steps
  return RailsOnboarding.configuration.steps unless RailsOnboarding.configuration.personalization_enabled

  user_type = personalization_type
  return RailsOnboarding.configuration.steps unless user_type

  flow = RailsOnboarding.configuration.personalized_flow(user_type)
  flow || RailsOnboarding.configuration.steps
end

#personalized_total_stepsInteger

Get the total number of steps in the personalized flow

Returns:

  • (Integer)

    Number of steps



73
74
75
# File 'app/models/concerns/rails_onboarding/personalizable.rb', line 73

def personalized_total_steps
  personalized_steps.size
end

#show_personalized_feature?(feature_name) ⇒ Boolean

Check if a specific feature should be shown to this user type

Examples:

user.show_personalized_feature?(:team_invite)
# => true (for business users)

Parameters:

  • feature_name (String, Symbol)

    Feature name

Returns:

  • (Boolean)

    True if feature should be shown



162
163
164
165
166
167
168
169
170
# File 'app/models/concerns/rails_onboarding/personalizable.rb', line 162

def show_personalized_feature?(feature_name)
  return true unless RailsOnboarding.configuration.personalization_enabled

  user_type = personalization_type
  return true unless user_type

  # Check if feature is in the personalized steps
  personalized_steps.any? { |step| step[:name].to_s == feature_name.to_s }
end