Skip to content

Commit a2bb793

Browse files
authored
Merge pull request #1727 from alphagov/prepare-for-switching-step-ids-to-be-external-ids
Prepare for switching step ids to be external ids
2 parents 30663e5 + 61a8866 commit a2bb793

18 files changed

+307
-129
lines changed

app/controllers/forms/base_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def redirect_to_friendly_url_start
88
LogEventService.log_form_start unless mode.preview?
99
end
1010

11-
rescue_from ActiveResource::ResourceNotFound, Flow::StepFactory::PageNotFoundError, RepeatableStep::AnswerIndexError do
11+
rescue_from ActiveResource::ResourceNotFound, RepeatableStep::AnswerIndexError do
1212
I18n.with_locale(locale) do
1313
render template: "errors/not_found", status: :not_found
1414
end

app/controllers/forms/page_controller.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ def save
3838

3939
def prepare_step
4040
page_slug = params.require(:page_slug)
41-
@step = current_context.find_or_create(page_slug)
41+
begin
42+
@step = current_context.find_or_create(page_slug)
43+
rescue Flow::StepFactory::PageNotFoundError
44+
return redirect_to form_page_path(@form.id, @form.form_slug, current_context.next_page_slug)
45+
end
4246

4347
if @step.respond_to?(:answer_index)
4448
@step.answer_index = answer_index

app/lib/flow/step_factory.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
module Flow
22
class StepFactory
33
START_PAGE = "_start".freeze
4-
PAGE_SLUG_REGEX = Regexp.union([Page::PAGE_ID_REGEX, Regexp.new(CheckYourAnswersStep::CHECK_YOUR_ANSWERS_PAGE_SLUG)])
54

65
class PageNotFoundError < StandardError
76
def initialize(msg = "Page not found.")
@@ -22,6 +21,7 @@ def create_step(page_slug_or_start)
2221

2322
# for now, we use the page id as slug
2423
page = @form.pages.find { |p| p.id.to_s == page_slug }
24+
page = @form.pages.find { |p| p.respond_to?(:database_id) && p.database_id.to_s == page_slug } if page.nil?
2525
raise PageNotFoundError, "Can't find page #{page_slug}" if page.nil?
2626

2727
question = QuestionRegister.from_page(page)

app/lib/store/access.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,9 @@ module Access
33
def page_key(step)
44
step.page_id.to_s
55
end
6+
7+
def database_id_key(step)
8+
step.database_id&.to_s
9+
end
610
end
711
end

app/lib/store/database_answer_store.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ def initialize(answers)
77
end
88

99
def get_stored_answer(step)
10-
@answers[page_key(step)]
10+
if @answers.key?(page_key(step))
11+
@answers[page_key(step)]
12+
elsif database_id_key(step) && @answers.key?(database_id_key(step))
13+
@answers[database_id_key(step)]
14+
end
1115
end
1216
end
1317
end

app/lib/store/session_answer_store.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,20 @@ def save_step(step, answer)
1616
end
1717

1818
def get_stored_answer(step)
19-
@store.dig(ANSWERS_KEY, @form_key, page_key(step))
19+
return nil if answers.nil?
20+
21+
if answers.key?(page_key(step))
22+
answers[page_key(step)]
23+
elsif database_id_key(step) && answers.key?(database_id_key(step))
24+
answers[database_id_key(step)]
25+
end
2026
end
2127

2228
def clear_stored_answer(step)
23-
@store.dig(ANSWERS_KEY, @form_key)&.delete(page_key(step))
29+
return nil if answers.nil?
30+
31+
answers.delete(page_key(step)) if answers.key?(page_key(step))
32+
answers.delete(database_id_key(step)) if database_id_key(step) && answers.key?(database_id_key(step))
2433
end
2534

2635
def clear

app/models/form.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def pages
2727
"id" => step["id"],
2828
"position" => step["position"],
2929
"next_page" => step["next_step_id"],
30+
"database_id" => step["database_id"], # temporary attribute while we switch to using external IDs
3031
}
3132
if step["type"] == "question_page"
3233
attrs.merge!(step["data"])

app/models/page.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class Page < ActiveResource::Base
2-
PAGE_ID_REGEX = /\d+/
2+
PAGE_ID_REGEX_FOR_ROUTES = /(?:[a-zA-Z0-9]{8}|\d+)/
3+
PAGE_ID_REGEX = /\A#{PAGE_ID_REGEX_FOR_ROUTES}\z/
34

45
self.site = Settings.forms_api.base_url
56
self.prefix = "/api/v2/forms/:form_id/"

app/models/step.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ def page_id
1414
page&.id.to_s
1515
end
1616

17+
# Temporary attribute while we switch to using external IDs so we don't lose in-progress user sessions
18+
def database_id
19+
page.database_id if page && page.respond_to?(:database_id)
20+
end
21+
1722
def page_number
1823
page&.position
1924
end

config/routes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
get "/submitted" => "forms/submitted#submitted", as: :form_submitted
2525
get "/privacy" => "forms/privacy_page#show", as: :form_privacy
2626

27-
page_constraints = { page_slug: Flow::StepFactory::PAGE_SLUG_REGEX }
27+
page_constraints = { page_slug: Regexp.union([Page::PAGE_ID_REGEX_FOR_ROUTES, Regexp.new(CheckYourAnswersStep::CHECK_YOUR_ANSWERS_PAGE_SLUG)]) }
2828
answer_constraints = { answer_index: /\d+/ }
2929
page_answer_defaults = { answer_index: 1 }
3030

0 commit comments

Comments
 (0)