Skip to content

Commit e8a584f

Browse files
committed
Fold token_b_param into find_b_param
One finder, with create passing build: true. It now writes the session token on every action, so a tokenized link is what the next tokenless request lands on. Finished says "Registration saved" while the email is unconfirmed, and the stolen bullet names the community network.
1 parent 2aa2b53 commit e8a584f

6 files changed

Lines changed: 27 additions & 23 deletions

File tree

app/components/register/step_finished/component.en.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ en:
88
confirming_lets_you: 'Confirming your email lets you:'
99
register_another: Register another %{cycle_type}
1010
registration_complete: Registration complete!
11-
report_it_stolen: Report your %{cycle_type} stolen
11+
registration_saved: Registration saved
12+
report_it_stolen: >-
13+
Finish reporting your stolen %{cycle_type} and take advantage of our
14+
community network
1215
verify_your_email_html: >-
1316
One more step — verify your email. We sent a confirmation link to %{email};
1417
click it to finish your registration. Everything you've entered is saved.

app/components/register/step_finished/component.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
translation(".verify_your_email_html", email: content_tag(:strong, @b_param.owner_email))
2828
end %>
2929

30-
<%= render UI::Header::Component.new(text: translation(".registration_complete"), subtitle:) %>
30+
<%= render UI::Header::Component.new(text: heading_text, subtitle:) %>
3131

3232
<%# What's still locked behind the confirmation, so the link is worth clicking %>
3333
<% if @bike.blank? %>

app/components/register/step_finished/component.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ def initialize(b_param:, current_user: nil)
1515
def cycle_type
1616
@b_param.type
1717
end
18+
19+
# Without the bike the registration is only held, waiting on the email
20+
def heading_text
21+
translation(@bike.present? ? ".registration_complete" : ".registration_saved")
22+
end
1823
end
1924
end
2025
end

app/controllers/register_controller.rb

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
class RegisterController < ApplicationController
22
before_action :find_b_param, except: %i[new create]
3-
# Step 1 carries everything it needs, so an expired token starts a registration
4-
# rather than bouncing and losing the submission
5-
before_action :find_or_build_b_param, only: %i[create]
3+
# An expired token starts a registration rather than bouncing and losing the
4+
# submission. assign_organization runs next, so the form's organization_id lands on it
5+
before_action -> { find_b_param(build: true) }, only: %i[create]
66
before_action :assign_organization, except: %i[new]
77
before_action :redirect_finished, only: %i[create update]
88
# The step shown depends on server state - a cached page could show a step
@@ -101,25 +101,21 @@ def confirm
101101
end
102102
end
103103

104-
def find_b_param
105-
@b_param = token_b_param
106-
return if @b_param.present?
107-
108-
flash[:info] = translation(:registration_not_found) if params[:b_param_token].present?
109-
redirect_to new_register_path
110-
end
104+
# build: only step 1's submission, which carries everything a registration needs
105+
def find_b_param(build: false)
106+
@b_param = BikeServices::Register.find_token(params_token: params[:b_param_token],
107+
session_token: session[:register_b_param_token], user: current_user)
108+
@b_param ||= BikeServices::Register.b_param_for(user: current_user) if build
109+
if @b_param.blank?
110+
flash[:info] = translation(:registration_not_found) if params[:b_param_token].present?
111+
return redirect_to(new_register_path)
112+
end
111113

112-
# assign_organization runs next, so the form's organization_id still lands on it
113-
def find_or_build_b_param
114-
@b_param = token_b_param || BikeServices::Register.b_param_for(user: current_user)
114+
# The session follows whichever registration the token named, so the next
115+
# tokenless request stays on it
115116
session[:register_b_param_token] = @b_param.id_token
116117
end
117118

118-
def token_b_param
119-
BikeServices::Register.find_token(params_token: params[:b_param_token],
120-
session_token: session[:register_b_param_token], user: current_user)
121-
end
122-
123119
# A finished registration (bike created, or awaiting the email) only shows
124120
# the completion page - submissions redirect there too, saving nothing
125121
def redirect_finished

spec/integration/registration/register_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@
122122

123123
click_button "Complete Bike Registration"
124124

125-
expect(page).to have_content("Registration complete")
125+
expect(page).to have_content("Registration saved")
126126
expect(page).to have_content("verify your email")
127127
b_param = BParam.last
128128
expect(b_param.bike).to include("frame_model" => "Marlin 7", "year" => "2023",

spec/requests/register_request_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,11 +492,11 @@ def phone_statuses_watched
492492
expect(b_param.bike).to match_hash_indifferently(bike_details.merge(owner_email:,
493493
manufacturer_id: manufacturer.id, primary_frame_color_id: color.id.to_s))
494494
follow_redirect!
495-
expect(response.body).to include "Registration complete"
495+
expect(response.body).to include "Registration saved"
496496
expect(response.body).to include "verify your email"
497497
# Why the confirmation link is worth clicking
498498
expect(response.body).to include "Confirming your email lets you"
499-
expect(response.body).to include "Report your bike stolen"
499+
expect(response.body).to include "Finish reporting your stolen bike"
500500
end
501501

502502
context "with a photo" do

0 commit comments

Comments
 (0)