Skip to content

Commit 0b64c69

Browse files
committed
Refactor updating question
To be more consistent with how we usually do updates, move updating the page when submitting the edit question page to the `submit` method on the QuestionInput. Previously we would have wanted this to be in the controller as it was calling forms-api using ActiveResource. But now the Page is updated in the local database instead. Improve the tests around this.
1 parent c1d9193 commit 0b64c69

4 files changed

Lines changed: 58 additions & 44 deletions

File tree

app/controllers/pages/questions_controller.rb

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ def edit
3535
end
3636

3737
def update
38-
page.assign_attributes(page_params_for_update)
39-
4038
@question_input = Pages::QuestionInput.new(page_params_for_form_object)
4139

4240
if @question_input.update_page(@page)
@@ -62,14 +60,6 @@ def page_params_for_form_object
6260
guidance_markdown: draft_question.guidance_markdown)
6361
end
6462

65-
def page_params_for_update
66-
page_params.merge(form_id: current_form.id,
67-
answer_settings: draft_question.answer_settings,
68-
page_heading: draft_question.page_heading,
69-
guidance_markdown: draft_question.guidance_markdown,
70-
answer_type: draft_question.answer_type)
71-
end
72-
7363
def check_draft_question
7464
render "errors/missing_draft_question", status: :unprocessable_content, formats: :html if draft_question.answer_type.blank?
7565
end

app/input_objects/pages/question_input.rb

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,7 @@ class Pages::QuestionInput < BaseInput
1717
def submit
1818
return false if invalid?
1919

20-
draft_question.assign_attributes(
21-
question_text:,
22-
hint_text:,
23-
is_optional:,
24-
is_repeatable:,
25-
)
26-
27-
draft_question.save!(validate: false)
20+
update_draft_question
2821

2922
Page.create_and_update_form!(form_id:,
3023
question_text:,
@@ -40,15 +33,16 @@ def submit
4033
def update_page(page)
4134
return false if invalid?
4235

43-
draft_question.assign_attributes(
44-
question_text:,
36+
update_draft_question
37+
38+
page.assign_attributes(question_text:,
4539
hint_text:,
4640
is_optional:,
4741
is_repeatable:,
48-
)
49-
50-
draft_question.save!(validate: false)
51-
42+
answer_settings:,
43+
page_heading:,
44+
guidance_markdown:,
45+
answer_type:)
5246
page.save_and_update_form
5347
end
5448

@@ -82,4 +76,17 @@ def validate_question_text_length
8276
translation_key = answer_type == "file" ? :too_long_file : :too_long
8377
errors.add(:question_text, translation_key, count: QuestionTextValidation::QUESTION_TEXT_MAX_LENGTH)
8478
end
79+
80+
private
81+
82+
def update_draft_question
83+
draft_question.assign_attributes(
84+
question_text:,
85+
hint_text:,
86+
is_optional:,
87+
is_repeatable:,
88+
)
89+
90+
draft_question.save!(validate: false)
91+
end
8592
end

spec/input_objects/pages/question_input_spec.rb

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
RSpec.describe Pages::QuestionInput, type: :model do
44
let(:form) { create :form }
5-
let(:question_input) { build :question_input, answer_type:, question_text:, draft_question:, is_optional:, is_repeatable:, form_id: form.id }
6-
let(:draft_question) { build :draft_question, question_text:, form_id: form.id }
5+
let(:question_input) do
6+
build(:question_input, answer_type:, question_text:, draft_question:, is_optional:,
7+
is_repeatable:, form_id: form.id, answer_settings: draft_question.answer_settings,
8+
page_heading: draft_question.page_heading, guidance_markdown: draft_question.guidance_markdown)
9+
end
10+
let(:draft_question) { build :address_draft_question, :with_guidance, question_text:, form_id: form.id }
711
let(:question_text) { "What is your full name?" }
812
let(:is_optional) { "false" }
913
let(:is_repeatable) { "false" }
10-
let(:answer_type) { "email" }
14+
let(:answer_type) { draft_question.answer_type }
1115

1216
it "has a valid factory" do
1317
expect(build(:question_input)).to be_valid
@@ -315,6 +319,17 @@
315319
it "sets a draft_question is_repeatable" do
316320
expect(question_input.draft_question.is_repeatable.to_s).to eq question_input.is_repeatable
317321
end
322+
323+
it "updates the page attributes" do
324+
expect(page.question_text).to eq question_input.question_text
325+
expect(page.hint_text).to eq question_input.hint_text
326+
expect(page.is_optional.to_s).to eq question_input.is_optional
327+
expect(page.is_repeatable.to_s).to eq question_input.is_repeatable
328+
expect(page.answer_settings).to eq DataStruct.recursive_new(question_input.answer_settings)
329+
expect(page.page_heading).to eq question_input.page_heading
330+
expect(page.guidance_markdown).to eq question_input.guidance_markdown
331+
expect(page.answer_type).to eq question_input.answer_type
332+
end
318333
end
319334
end
320335
end

spec/requests/pages/questions_controller_spec.rb

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -176,24 +176,19 @@
176176

177177
describe "#update" do
178178
let(:draft_question) do
179-
record = create :draft_question, user: standard_user, form_id: form.id
180-
record.question_text = nil
181-
record.save!(validate: false)
182-
record.reload
179+
create(:address_draft_question, :with_guidance, page_id: page.id, user: standard_user, form_id: form.id)
183180
end
184181

185182
let(:page) do
186183
create(
187184
:page,
188185
form_id: form.id,
189-
question_text: "What is your work address?",
190-
hint_text: "This should be the location stated in your contract.",
191-
answer_type: "address",
186+
question_text: "Old question text",
187+
hint_text: "Old hint text",
188+
answer_type: "email",
192189
answer_settings: nil,
193190
is_optional: false,
194191
is_repeatable: false,
195-
page_heading: "New page heading",
196-
guidance_markdown: "## Heading level 2",
197192
)
198193
end
199194

@@ -203,20 +198,27 @@
203198
form_id: form.id,
204199
question_text: "What is your home address?",
205200
hint_text: "This should be the location stated in your contract.",
206-
answer_type: "address",
207-
is_optional: "false",
208-
is_repeatable: "false",
209-
page_heading: "New page heading",
210-
guidance_markdown: "## Heading level 2",
201+
is_optional: "true",
202+
is_repeatable: "true",
211203
} }
212204
end
213205

214206
before do
207+
draft_question
215208
post update_question_path(form_id: form.id, page_id: page.id), params:
216209
end
217210

218-
it "Updates the page through the page repository" do
219-
expect(page.reload.question_text).to eq("What is your home address?")
211+
it "Updates the page" do
212+
expect(page.reload).to have_attributes(
213+
question_text: "What is your home address?",
214+
hint_text: "This should be the location stated in your contract.",
215+
is_optional: true,
216+
is_repeatable: true,
217+
answer_type: "address",
218+
answer_settings: DataStruct.recursive_new(draft_question.answer_settings),
219+
page_heading: draft_question.page_heading,
220+
guidance_markdown: draft_question.guidance_markdown,
221+
)
220222
end
221223

222224
it "Redirects you to edit page for question that was updated" do
@@ -233,7 +235,7 @@
233235
end
234236

235237
it "logs the answer type from the page", :capture_logging do
236-
expect(log_line["answer_type"]).to eq(page.answer_type)
238+
expect(log_line["answer_type"]).to eq(page.reload.answer_type)
237239
end
238240

239241
context "when question being updated has a question after it" do

0 commit comments

Comments
 (0)