Class: RailsOnboarding::OnboardingController

Inherits:
ApplicationController show all
Includes:
RateLimitable
Defined in:
app/controllers/rails_onboarding/onboarding_controller.rb

Instance Method Summary collapse

Instance Method Details

#backObject



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'app/controllers/rails_onboarding/onboarding_controller.rb', line 163

def back
  unless current_user.can_go_back?
    respond_to do |format|
      format.html { redirect_to onboarding_path, alert: "Cannot go back from the first step." }
      format.turbo_stream do
        render turbo_stream: turbo_stream.replace(
          "flash-messages",
          partial: "rails_onboarding/shared/flash",
          locals: { alert: "Cannot go back from the first step." }
        ), status: :unprocessable_entity
      end
    end
    return
  end

  session_id = RailsOnboarding::SessionManager.session_id(current_user, session)
  current_user.go_back!(session_id: session_id)

  redirect_to onboarding_path
end

#completeObject



125
126
127
128
129
130
131
132
133
134
# File 'app/controllers/rails_onboarding/onboarding_controller.rb', line 125

def complete
  completion_milestones = []
  if defined?(RailsOnboarding::MilestoneService)
    completion_milestones = RailsOnboarding::MilestoneService.check_onboarding_completion_milestones(current_user) || []
  end

  current_user.complete_onboarding!

  redirect_to_after_completion(completion_milestones)
end

#nextObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/controllers/rails_onboarding/onboarding_controller.rb', line 81

def next
  unless @current_step
    respond_to do |format|
      format.html { redirect_to onboarding_path, alert: "Invalid step. Redirecting to current step." }
      format.turbo_stream do
        render turbo_stream: turbo_stream.replace(
          "flash-messages",
          partial: "rails_onboarding/shared/flash",
          locals: { alert: "Invalid step. Please try again." }
        )
      end
    end
    return
  end

  if params[:step_data].present?
    # Process any step-specific data
    process_step_data(@current_step[:name], params[:step_data])
  end

  # Award milestones for completing this step
  awarded_milestones = []
  if defined?(RailsOnboarding::MilestoneService)
    awarded_milestones = RailsOnboarding::MilestoneService.check_onboarding_step_milestones(
      current_user,
      @current_step[:name]
    ) || []
  end

  current_user.complete_onboarding_step!(@current_step[:name])

  if current_user.onboarding_completed?
    # Award completion milestones
    if defined?(RailsOnboarding::MilestoneService)
      completion_milestones = RailsOnboarding::MilestoneService.check_onboarding_completion_milestones(current_user) || []
      awarded_milestones.concat(completion_milestones)
    end

    redirect_to_after_completion(awarded_milestones)
  else
    redirect_to onboarding_path(awarded_milestones: awarded_milestones.map { |m| m[:key] })
  end
end

#restartObject



184
185
186
187
188
189
190
# File 'app/controllers/rails_onboarding/onboarding_controller.rb', line 184

def restart
  session_id = RailsOnboarding::SessionManager.session_id(current_user, session)
  current_user.restart_onboarding!(session_id: session_id)
  RailsOnboarding::SessionManager.clear_session(current_user, session)

  redirect_to onboarding_path, notice: "Onboarding has been restarted."
end

#showObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/controllers/rails_onboarding/onboarding_controller.rb', line 18

def show
  unless @current_step
    # Reset onboarding to first step
    first_step = RailsOnboarding.configuration.steps.first

    unless first_step
      handle_configuration_error("No onboarding steps configured")
      return
    end

    current_user.update!(onboarding_current_step: first_step[:name])
    @current_step = current_user.current_onboarding_step
  end

  # Reset invalid step to first step
  if current_user.onboarding_current_step &&
     !RailsOnboarding.configuration.step_by_name(current_user.onboarding_current_step)
    first_step = RailsOnboarding.configuration.steps.first
    current_user.update!(onboarding_current_step: first_step[:name])
    @current_step = current_user.current_onboarding_step
  end

  # Skip past steps whose :complete_if criteria are already satisfied.
  # This is what lets a step live on a real host-app page: the host
  # controller never has to know onboarding exists - landing back on
  # /onboarding re-checks and moves on. May redirect (completion).
  advance_completed_steps
  return if performed?

  # Track step entry - the funnel's "reached this step" signal. Emitted
  # only on a user's first entry to a step, so refreshes and back-navigation
  # don't record duplicate events.
  if RailsOnboarding.configuration.enable_analytics && first_entry_to_step?(@current_step[:name])
    RailsOnboarding::AnalyticsEvent.track_step_started(
      user: current_user,
      step_name: @current_step[:name],
      step_index: RailsOnboarding.configuration.step_index(@current_step[:name])
    )
  end

  # Record that the user has now been shown this step. That's what lets
  # :complete_if advance them past it when they come back, and it is what
  # makes a restart re-walk a flow the user's data already satisfies.
  # No-op outside a replay.
  mark_step_replayed(@current_step)

  # Steps that own a real page in the host app: route there instead of
  # rendering a gem template, so the existing controller/view do the work.
  if @current_step[:path]
    redirect_to_step_page and return
  end

  # Dynamic action based on current step
  step_template = @current_step[:name]

  # Render the appropriate template
  if template_exists?(step_template)
    render step_template
  else
    render :step
  end
end

#skipObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'app/controllers/rails_onboarding/onboarding_controller.rb', line 136

def skip
  if params[:skip_all] == "true"
    current_user.skip_onboarding!
    redirect_to_after_skip
  elsif @current_step && @current_step[:skippable]
    current_user.skip_onboarding_step!(@current_step[:name])
    if current_user.onboarding_completed?
      redirect_to_after_completion
    else
      redirect_to onboarding_path
    end
  else
    respond_to do |format|
      format.html do
        redirect_to onboarding_path, alert: "This step cannot be skipped."
      end
      format.turbo_stream do
        render turbo_stream: turbo_stream.replace(
          "flash-messages",
          partial: "rails_onboarding/shared/flash",
          locals: { alert: "This step cannot be skipped." }
        ), status: :unprocessable_entity
      end
    end
  end
end