Skip to content

Commit 86a07a6

Browse files
committed
Clear empty none_of_the_above_question when saving question
This is to handle the case when a user clicks the "Change" link on the edit question page for a selection question and changes their answer for "Should the list include an option for ‘None of the above’?" to "Yes, and let people provide a different answer" but then clicks back without entering a question text. In this case, when they come to save their question, remove the blank hash for none_of_the_above_question in the answer_settings. Form fillers will be shown a "None of the above" option with no question in this case.
1 parent 0b64c69 commit 86a07a6

2 files changed

Lines changed: 42 additions & 12 deletions

File tree

app/input_objects/pages/question_input.rb

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

20-
update_draft_question
20+
prepare_for_save
2121

2222
Page.create_and_update_form!(form_id:,
2323
question_text:,
@@ -33,16 +33,16 @@ def submit
3333
def update_page(page)
3434
return false if invalid?
3535

36-
update_draft_question
36+
prepare_for_save
3737

3838
page.assign_attributes(question_text:,
39-
hint_text:,
40-
is_optional:,
41-
is_repeatable:,
42-
answer_settings:,
43-
page_heading:,
44-
guidance_markdown:,
45-
answer_type:)
39+
hint_text:,
40+
is_optional:,
41+
is_repeatable:,
42+
answer_settings:,
43+
page_heading:,
44+
guidance_markdown:,
45+
answer_type:)
4646
page.save_and_update_form
4747
end
4848

@@ -79,6 +79,15 @@ def validate_question_text_length
7979

8080
private
8181

82+
def prepare_for_save
83+
compact_answer_settings
84+
update_draft_question
85+
end
86+
87+
def compact_answer_settings
88+
answer_settings.delete(:none_of_the_above_question) if answer_settings[:none_of_the_above_question].blank?
89+
end
90+
8291
def update_draft_question
8392
draft_question.assign_attributes(
8493
question_text:,

spec/input_objects/pages/question_input_spec.rb

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
RSpec.describe Pages::QuestionInput, type: :model do
44
let(:form) { create :form }
5+
let(:answer_settings) { { foo: "bar" } }
56
let(:question_input) do
67
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)
8+
is_repeatable:, form_id: form.id, answer_settings:, page_heading: draft_question.page_heading,
9+
guidance_markdown: draft_question.guidance_markdown)
910
end
1011
let(:draft_question) { build :address_draft_question, :with_guidance, question_text:, form_id: form.id }
1112
let(:question_text) { "What is your full name?" }
@@ -204,7 +205,10 @@
204205
end
205206

206207
context "when not given a draft_question" do
207-
let(:draft_question) { nil }
208+
let(:question_input) do
209+
build(:question_input, answer_type:, question_text:, draft_question: nil, is_optional:,
210+
is_repeatable:, form_id: form.id)
211+
end
208212

209213
it "is invalid" do
210214
expect(question_input).to be_invalid
@@ -321,6 +325,7 @@
321325
end
322326

323327
it "updates the page attributes" do
328+
page.reload
324329
expect(page.question_text).to eq question_input.question_text
325330
expect(page.hint_text).to eq question_input.hint_text
326331
expect(page.is_optional.to_s).to eq question_input.is_optional
@@ -330,6 +335,22 @@
330335
expect(page.guidance_markdown).to eq question_input.guidance_markdown
331336
expect(page.answer_type).to eq question_input.answer_type
332337
end
338+
339+
context "when the answer_settings has an empty hash for none_of_the_above_question" do
340+
let(:answer_settings) { { foo: "bar", none_of_the_above_question: {} } }
341+
342+
it "removes the empty hash from the answer_settings" do
343+
expect(page.reload.answer_settings).to eq DataStruct.recursive_new({ "foo": "bar" })
344+
end
345+
end
346+
347+
context "when the answer_settings has popolated none_of_the_above_question" do
348+
let(:answer_settings) { { foo: "bar", none_of_the_above_question: { "question_text": "Enter something" } } }
349+
350+
it "keeps the populated none_of_the_above_question in the answer_settings" do
351+
expect(page.reload.answer_settings).to eq DataStruct.recursive_new({ "foo": "bar", "none_of_the_above_question": { "question_text": "Enter something" } })
352+
end
353+
end
333354
end
334355
end
335356
end

0 commit comments

Comments
 (0)