Skip to content

Commit 00f2dc5

Browse files
authored
Merge pull request #2023 from govuk-forms/isolate-form-document-steps
Make form_document_step internal to the Step model
2 parents 93232cf + 28b2c2b commit 00f2dc5

5 files changed

Lines changed: 19 additions & 18 deletions

File tree

app/controllers/forms/step_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ class StepController < BaseController
44

55
def set_request_logging_attributes
66
super
7-
CurrentRequestLoggingAttributes.question_number = @step.step_number if @step&.step_number
8-
CurrentRequestLoggingAttributes.answer_type = @step&.form_document_step&.answer_type if @step&.form_document_step&.answer_type
7+
CurrentRequestLoggingAttributes.question_number = @step&.step_number
8+
CurrentRequestLoggingAttributes.answer_type = @step&.answer_type
99
end
1010

1111
def show

app/models/step.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class Step
2-
attr_accessor :form_document_step, :question
2+
attr_accessor :question
3+
private attr_reader :form_document_step
34

45
GOTO_PAGE_ERROR_NAMES = %w[cannot_have_goto_page_before_routing_page goto_page_doesnt_exist].freeze
56

@@ -8,6 +9,8 @@ def initialize(form_document_step:, question:)
89
@question = question
910
end
1011

12+
delegate :answer_type, to: :form_document_step
13+
1114
def id
1215
form_document_step&.id.to_s
1316
end

spec/lib/json_submission_generator_spec.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,24 @@
6363
"submitted_at" => "2022-09-14T07:00:00.000Z",
6464
"answers" => [
6565
{
66-
"question_id" => text_step.form_document_step.id,
66+
"question_id" => text_step.id,
6767
"question_text" => "What is the meaning of life?",
6868
"answer_text" => text_question.text,
6969
},
7070
{
71-
"question_id" => name_step.form_document_step.id,
71+
"question_id" => name_step.id,
7272
"question_text" => "What is your name?",
7373
"first_name" => name_question.first_name,
7474
"last_name" => name_question.last_name,
7575
"answer_text" => name_question.show_answer,
7676
},
7777
{
78-
"question_id" => file_step.form_document_step.id,
78+
"question_id" => file_step.id,
7979
"question_text" => "Upload a file",
8080
"answer_text" => "test_#{submission_reference}.txt",
8181
},
8282
{
83-
"question_id" => address_step.form_document_step.id,
83+
"question_id" => address_step.id,
8484
"question_text" => "What is your address?",
8585
"address1" => address_question.address1,
8686
"address2" => "",
@@ -90,7 +90,7 @@
9090
"answer_text" => address_question.show_answer,
9191
},
9292
{
93-
"question_id" => selection_step.form_document_step.id,
93+
"question_id" => selection_step.id,
9494
"question_text" => "Select your options",
9595
"selections" => ["Option 1", "Option 2"],
9696
"answer_text" => "Option 1, Option 2",
@@ -115,13 +115,13 @@
115115
"submitted_at" => "2022-09-14T07:00:00.000Z",
116116
"answers" => [
117117
{
118-
"question_id" => repeatable_step.form_document_step.id,
118+
"question_id" => repeatable_step.id,
119119
"question_text" => "What is the meaning of life?",
120120
"can_have_multiple_answers" => true,
121121
"answer_text" => [first_answer.text, second_answer.text],
122122
},
123123
{
124-
"question_id" => name_step.form_document_step.id,
124+
"question_id" => name_step.id,
125125
"question_text" => "What is your name?",
126126
"first_name" => name_question.first_name,
127127
"last_name" => name_question.last_name,
@@ -148,7 +148,7 @@
148148

149149
it "generates JSON without including the submission reference in the filename for the file upload question" do
150150
expect(parsed_json["answers"]).to include({
151-
"question_id" => file_step.form_document_step.id,
151+
"question_id" => file_step.id,
152152
"question_text" => "Upload a file",
153153
"answer_text" => "test.txt",
154154
})

spec/lib/ses_email_formatter_spec.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,13 @@
9191

9292
context "when there is an error formatting an answer" do
9393
before do
94-
text_step.form_document_step.id = 99
9594
allow(text_step).to receive(:show_answer_in_email).and_raise(NoMethodError, "undefined method 'strip' for an instance of Array")
9695
end
9796

9897
it "raises an error with the page id" do
9998
expect {
10099
ses_email_formatter.build_question_answers_section_html
101-
}.to raise_error(SesEmailFormatter::FormattingError, "could not format answer for question step 99")
100+
}.to raise_error(SesEmailFormatter::FormattingError, "could not format answer for question step #{text_step.id}")
102101
end
103102
end
104103
end
@@ -170,14 +169,13 @@
170169

171170
context "when there is an error formatting an answer" do
172171
before do
173-
text_step.form_document_step.id = 99
174172
allow(text_step).to receive(:show_answer_in_email).and_raise(NoMethodError, "undefined method 'strip' for an instance of Array")
175173
end
176174

177175
it "raises an error with the page id" do
178176
expect {
179177
ses_email_formatter.build_question_answers_section_plain_text
180-
}.to raise_error(SesEmailFormatter::FormattingError, "could not format answer for question step 99")
178+
}.to raise_error(SesEmailFormatter::FormattingError, "could not format answer for question step #{text_step.id}")
181179
end
182180
end
183181
end

spec/models/step_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,16 @@
4949
describe "#state" do
5050
it "returns an array of instance variable values" do
5151
expected_state = [
52-
step.form_document_step,
53-
step.question,
52+
form_document_step,
53+
question,
5454
]
5555

5656
expect(step.state).to match_array(expected_state)
5757
end
5858

5959
it "changes when an instance variable is modified" do
6060
original_state = step.state.dup
61-
step.form_document_step = build(:v2_question_page_step, position: 2)
61+
step.question = build(:name)
6262
expect(step.state).not_to eq(original_state)
6363
end
6464
end

0 commit comments

Comments
 (0)