Skip to content

Commit 47005e4

Browse files
committed
Access page_slug from page inside Step class
This removes the separate page_slug instance variable, which was passed into the initializer for Step via the StepFactory. As the original Page object is also passed in, we can reference it directly from that object. This helps simplify the code and makes it easier for future refactoring. `page_slug` will always equal the `page.id` as the StepFactory matches them, otherwise it raises an error.
1 parent ccd8baa commit 47005e4

20 files changed

+58
-68
lines changed

app/controllers/forms/add_another_answer_controller.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class AddAnotherAnswerController < PageController
44

55
def show
66
@rows = rows
7-
back_link(@step.page_slug)
7+
back_link(@step.id)
88
@add_another_answer_input = AddAnotherAnswerInput.new
99
end
1010

@@ -13,7 +13,7 @@ def save
1313

1414
if @add_another_answer_input.invalid?
1515
@rows = rows
16-
back_link(@step.page_slug)
16+
back_link(@step.id)
1717
return render :show
1818
end
1919

@@ -28,18 +28,18 @@ def save
2828

2929
def add_another_path
3030
if changing_existing_answer
31-
form_change_answer_path(@form.id, @form.form_slug, @step.page_slug, answer_index: @step.next_answer_index)
31+
form_change_answer_path(@form.id, @form.form_slug, @step.id, answer_index: @step.next_answer_index)
3232
else
33-
form_page_path(@form.id, @form.form_slug, @step.page_slug, answer_index: @step.next_answer_index)
33+
form_page_path(@form.id, @form.form_slug, @step.id, answer_index: @step.next_answer_index)
3434
end
3535
end
3636

3737
def rows
3838
@step.questions.map.with_index(1) do |question, answer_index|
39-
actions = [{ text: t("forms.add_another_answer.rows.change"), href: form_change_answer_path(@form.id, @form.form_slug, @step.page_slug, answer_index:), visually_hidden_text: I18n.t("forms.add_another_answer.rows.action_hidden_text", answer_index:) }]
39+
actions = [{ text: t("forms.add_another_answer.rows.change"), href: form_change_answer_path(@form.id, @form.form_slug, @step.id, answer_index:), visually_hidden_text: I18n.t("forms.add_another_answer.rows.action_hidden_text", answer_index:) }]
4040

4141
unless @step.min_answers?
42-
actions << { text: t("forms.add_another_answer.rows.remove"), href: form_remove_answer_path(@form.id, @form.form_slug, @step.page_slug, answer_index:, changing_existing_answer:), visually_hidden_text: I18n.t("forms.add_another_answer.rows.action_hidden_text", answer_index:) }
42+
actions << { text: t("forms.add_another_answer.rows.remove"), href: form_remove_answer_path(@form.id, @form.form_slug, @step.id, answer_index:, changing_existing_answer:), visually_hidden_text: I18n.t("forms.add_another_answer.rows.action_hidden_text", answer_index:) }
4343
end
4444

4545
{
@@ -61,9 +61,9 @@ def add_another_input_params
6161
def redirect_if_not_repeating
6262
unless @step.is_a?(RepeatableStep)
6363
if changing_existing_answer
64-
redirect_to form_change_answer_path(form_id: @form.id, form_slug: @form.form_slug, page_slug: @step.page_slug)
64+
redirect_to form_change_answer_path(form_id: @form.id, form_slug: @form.form_slug, page_slug: @step.id)
6565
else
66-
redirect_to form_page_path(form_id: @form.id, form_slug: @form.form_slug, page_slug: @step.page_slug)
66+
redirect_to form_page_path(form_id: @form.id, form_slug: @form.form_slug, page_slug: @step.id)
6767
end
6868
end
6969
end

app/controllers/forms/check_your_answers_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def back_link
100100
previous_step = current_context.previous_step(CheckYourAnswersStep::CHECK_YOUR_ANSWERS_PAGE_SLUG)
101101

102102
if previous_step.present?
103-
previous_step.repeatable? ? add_another_answer_path(form_id: current_context.form.id, form_slug: current_context.form.form_slug, page_slug: previous_step.page_slug) : form_page_path(current_context.form.id, current_context.form.form_slug, previous_step.page_id)
103+
previous_step.repeatable? ? add_another_answer_path(form_id: current_context.form.id, form_slug: current_context.form.form_slug, page_slug: previous_step.id) : form_page_path(current_context.form.id, current_context.form.form_slug, previous_step.page_id)
104104
end
105105
end
106106
end

app/controllers/forms/exit_pages_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
module Forms
22
class ExitPagesController < PageController
33
def show
4-
return redirect_to form_page_path(@form.id, @form.form_slug, current_context.next_page_slug) unless current_context.can_visit?(@step.page_slug)
4+
return redirect_to form_page_path(@form.id, @form.form_slug, current_context.next_page_slug) unless current_context.can_visit?(@step.id)
55

6-
@back_link = form_page_path(@form.id, @form.form_slug, @step.page_slug)
6+
@back_link = form_page_path(@form.id, @form.form_slug, @step.id)
77
@condition = @step.routing_conditions.first
88
end
99
end

app/controllers/forms/page_controller.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ def set_request_logging_attributes
99
end
1010

1111
def show
12-
return redirect_to form_page_path(@form.id, @form.form_slug, current_context.next_page_slug) unless current_context.can_visit?(@step.page_slug)
12+
return redirect_to form_page_path(@form.id, @form.form_slug, current_context.next_page_slug) unless current_context.can_visit?(@step.id)
1313
return redirect_to review_file_page if answered_file_question?
1414

15-
back_link(@step.page_slug)
15+
back_link(@step.id)
1616
setup_instance_vars_for_view
1717
end
1818

@@ -53,7 +53,7 @@ def answer_index
5353

5454
def setup_instance_vars_for_view
5555
@is_question = true
56-
@question_edit_link = "#{Settings.forms_admin.base_url}/forms/#{@form.id}/pages/#{@step.page_slug}/edit/question"
56+
@question_edit_link = "#{Settings.forms_admin.base_url}/forms/#{@form.id}/pages/#{@step.id}/edit/question"
5757
@save_url = save_url
5858
end
5959

@@ -67,20 +67,20 @@ def back_link(page_slug)
6767
if changing_existing_answer
6868
@back_link = check_your_answers_path(form_id: current_context.form.id)
6969
elsif previous_step
70-
@back_link = previous_step.repeatable? ? add_another_answer_path(form_id: current_context.form.id, form_slug: current_context.form.form_slug, page_slug: previous_step.page_slug) : form_page_path(@form.id, @form.form_slug, previous_step.page_id)
70+
@back_link = previous_step.repeatable? ? add_another_answer_path(form_id: current_context.form.id, form_slug: current_context.form.form_slug, page_slug: previous_step.id) : form_page_path(@form.id, @form.form_slug, previous_step.page_id)
7171
end
7272
end
7373

7474
def redirect_post_save
7575
return redirect_to review_file_page, success: t("banner.success.file_uploaded") if answered_file_question?
76-
return redirect_to exit_page_path(form_id: @form.id, form_slug: @form.form_slug, page_slug: @step.page_slug) if @step.exit_page_condition_matches?
76+
return redirect_to exit_page_path(form_id: @form.id, form_slug: @form.form_slug, page_slug: @step.id) if @step.exit_page_condition_matches?
7777

7878
redirect_to next_page
7979
end
8080

8181
def redirect_if_not_answered_file_question
8282
unless @step.question.is_a?(Question::File) && @step.question.file_uploaded?
83-
redirect_to form_page_path(@form.id, @form.form_slug, @step.page_slug)
83+
redirect_to form_page_path(@form.id, @form.form_slug, @step.id)
8484
end
8585
end
8686

@@ -89,7 +89,7 @@ def answered_file_question?
8989
end
9090

9191
def review_file_page
92-
review_file_path(form_id: @form.id, form_slug: @form.form_slug, page_slug: @step.page_slug, changing_existing_answer:)
92+
review_file_path(form_id: @form.id, form_slug: @form.form_slug, page_slug: @step.id, changing_existing_answer:)
9393
end
9494

9595
def next_page
@@ -102,15 +102,15 @@ def next_page
102102

103103
def next_step_path
104104
if should_show_add_another?(@step)
105-
return add_another_answer_path(form_id: @form.id, form_slug: @form.form_slug, page_slug: @step.page_slug)
105+
return add_another_answer_path(form_id: @form.id, form_slug: @form.form_slug, page_slug: @step.id)
106106
end
107107

108108
next_step_in_form_path
109109
end
110110

111111
def next_step_changing
112112
if should_show_add_another?(@step)
113-
return change_add_another_answer_path(form_id: @form.id, form_slug: @form.form_slug, page_slug: @step.page_slug)
113+
return change_add_another_answer_path(form_id: @form.id, form_slug: @form.form_slug, page_slug: @step.id)
114114
end
115115

116116
check_answers_path
@@ -160,7 +160,7 @@ def admin_edit_condition_url(form_id, page_id)
160160
end
161161

162162
def is_first_page?
163-
current_context.form.start_page == @step.id
163+
current_context.form.start_page.to_s == @step.id
164164
end
165165

166166
def save_url

app/controllers/forms/remove_answer_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ def remove_answer
3131

3232
def next_page_after_removing
3333
if @step.question.is_optional? && @step.show_answer.blank?
34-
form_page_path(@form.id, @form.form_slug, @step.page_slug, changing_existing_answer:)
34+
form_page_path(@form.id, @form.form_slug, @step.id, changing_existing_answer:)
3535
else
36-
add_another_answer_path(@form.id, @form.form_slug, @step.page_slug, changing_existing_answer:)
36+
add_another_answer_path(@form.id, @form.form_slug, @step.id, changing_existing_answer:)
3737
end
3838
end
3939
end

app/controllers/forms/remove_file_controller.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def destroy
2121
return redirect_to redirect_after_delete_path, success: t("banner.success.file_removed")
2222
end
2323

24-
redirect_to review_file_path(form_id: @form.id, form_slug: @form.form_slug, page_slug: @step.page_slug, changing_existing_answer:)
24+
redirect_to review_file_path(form_id: @form.id, form_slug: @form.form_slug, page_slug: @step.id, changing_existing_answer:)
2525
end
2626

2727
private
@@ -32,15 +32,15 @@ def remove_input_params
3232

3333
def redirect_after_delete_path
3434
if changing_existing_answer
35-
return form_change_answer_path(form_id: @form.id, form_slug: @form.form_slug, page_slug: @step.page_slug)
35+
return form_change_answer_path(form_id: @form.id, form_slug: @form.form_slug, page_slug: @step.id)
3636
end
3737

38-
form_page_path(form_id: @form.id, form_slug: @form.form_slug, page_slug: @step.page_slug)
38+
form_page_path(form_id: @form.id, form_slug: @form.form_slug, page_slug: @step.id)
3939
end
4040

4141
def setup_urls
42-
@back_link = review_file_path(form_id: @form.id, form_slug: @form.form_slug, page_slug: @step.page_slug, changing_existing_answer:)
43-
@remove_file_url = remove_file_path(form_id: @form.id, form_slug: @form.form_slug, page_slug: @step.page_slug, changing_existing_answer:)
42+
@back_link = review_file_path(form_id: @form.id, form_slug: @form.form_slug, page_slug: @step.id, changing_existing_answer:)
43+
@remove_file_url = remove_file_path(form_id: @form.id, form_slug: @form.form_slug, page_slug: @step.id, changing_existing_answer:)
4444
end
4545
end
4646
end

app/controllers/forms/review_file_controller.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ class ReviewFileController < PageController
33
before_action :redirect_if_not_answered_file_question
44

55
def show
6-
back_link(@step.page_slug)
7-
@remove_file_confirmation_url = remove_file_confirmation_path(form_id: @form.id, form_slug: @form.form_slug, page_slug: @step.page_slug, changing_existing_answer:)
8-
@continue_url = review_file_continue_path(form_id: @form.id, form_slug: @form.form_slug, page_slug: @step.page_slug, changing_existing_answer:)
6+
back_link(@step.id)
7+
@remove_file_confirmation_url = remove_file_confirmation_path(form_id: @form.id, form_slug: @form.form_slug, page_slug: @step.id, changing_existing_answer:)
8+
@continue_url = review_file_continue_path(form_id: @form.id, form_slug: @form.form_slug, page_slug: @step.id, changing_existing_answer:)
99
end
1010

1111
def continue

app/lib/flow/journey.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ def initialize(answer_store:, form:)
2929
end
3030

3131
def find_or_create(page_slug)
32-
step = completed_steps.find { |s| s.page_slug == page_slug }
32+
step = completed_steps.find { |s| s.id == page_slug }
3333
step || @step_factory.create_step(page_slug)
3434
end
3535

3636
def previous_step(page_slug)
37-
index = completed_steps.find_index { |step| step.page_slug == page_slug }
37+
index = completed_steps.find_index { |step| step.id == page_slug }
3838
return nil if completed_steps.empty? || index&.zero?
3939

4040
return completed_steps.last if index.nil?
@@ -45,7 +45,7 @@ def previous_step(page_slug)
4545
def next_page_slug
4646
return nil if completed_steps.last&.end_page?
4747

48-
completed_steps.last&.next_page_slug_after_routing || @step_factory.start_step.page_slug
48+
completed_steps.last&.next_page_slug_after_routing || @step_factory.start_step.id
4949
end
5050

5151
def next_step
@@ -55,7 +55,7 @@ def next_step
5555
end
5656

5757
def can_visit?(page_slug)
58-
(completed_steps.map(&:page_slug).include? page_slug) || page_slug == next_page_slug
58+
(completed_steps.map(&:id).include? page_slug) || page_slug == next_page_slug
5959
end
6060

6161
def all_steps
@@ -111,7 +111,7 @@ def each_step_with_routing
111111
break if visited_page_slugs.include?(next_page_slug)
112112

113113
yielder << current_step
114-
visited_page_slugs << current_step.page_slug
114+
visited_page_slugs << current_step.id
115115

116116
break if next_page_slug.nil?
117117

app/lib/flow/step_factory.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def create_step(page_slug_or_start)
2626

2727
question = QuestionRegister.from_page(page)
2828

29-
step_class(page).new(question:, page:, page_slug:)
29+
step_class(page).new(question:, page:)
3030
end
3131

3232
def start_step

app/models/step.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
class Step
22
attr_accessor :page, :question
3-
attr_reader :page_slug
43

54
GOTO_PAGE_ERROR_NAMES = %w[cannot_have_goto_page_before_routing_page goto_page_doesnt_exist].freeze
65

7-
def initialize(page:, question:, page_slug:)
6+
def initialize(page:, question:)
87
@page = page
98
@question = question
10-
11-
@page_slug = page_slug
129
end
1310

1411
alias_attribute :id, :page_id
1512

1613
def page_id
17-
page&.id
14+
page&.id.to_s
1815
end
1916

2017
def page_number

0 commit comments

Comments
 (0)