Skip to content

Commit 303d98d

Browse files
committed
Clear none_of_the_above_answer if None of the above not selected
Add a before_validation callback to clear the `none_of_the_above_answer` for a selection question if 'None of the above' is not the selected option. This is because we don't want to include it in the saved answer in this case. This also means that we no longer need to check the selected option when applying length validation for `none_of_the_above_answer`.
1 parent 514ebef commit 303d98d

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

app/models/question/selection.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ module Question
22
class Selection < QuestionBase
33
attribute :selection
44
attribute :none_of_the_above_answer
5+
6+
before_validation :clear_none_of_the_above_answer_if_not_selected
7+
58
validates :selection, presence: true
69
validate :selection, :validate_checkbox, if: :allow_multiple_answers?
710
validate :selection, :validate_radio, unless: :allow_multiple_answers?
8-
validates :none_of_the_above_answer, length: { maximum: 499 }, if: :none_of_the_above_selected?
11+
validates :none_of_the_above_answer, length: { maximum: 499 }
912

1013
with_options unless: :autocomplete_component? do
1114
validates :none_of_the_above_answer, presence: true, if: :validate_none_of_the_above_answer_presence?
@@ -58,6 +61,10 @@ def has_none_of_the_above_question?
5861

5962
private
6063

64+
def clear_none_of_the_above_answer_if_not_selected
65+
self.none_of_the_above_answer = nil unless none_of_the_above_selected?
66+
end
67+
6168
def allowed_options
6269
selection_options_with_none_of_the_above.map(&:name)
6370
end

spec/models/question/selection_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,15 @@
338338
expect(question.errors[:none_of_the_above_answer]).to include(I18n.t("activemodel.errors.models.question/selection.attributes.none_of_the_above_answer.too_long"))
339339
end
340340
end
341+
342+
context "when 'None of the above' is not selected" do
343+
it "clears the none_of_the_above_answer before validating" do
344+
question.selection = ["option 1"]
345+
question.none_of_the_above_answer = "Some answer"
346+
expect(question).to be_valid
347+
expect(question.none_of_the_above_answer).to be_nil
348+
end
349+
end
341350
end
342351

343352
context "when none_of_the_above_question is mandatory" do
@@ -357,6 +366,7 @@
357366
question.none_of_the_above_answer = "Some answer"
358367
expect(question).to be_valid
359368
expect(question.errors[:none_of_the_above_answer]).to be_empty
369+
expect(question.none_of_the_above_answer).to eq("Some answer")
360370
end
361371
end
362372

@@ -375,6 +385,12 @@
375385
expect(question).to be_valid
376386
expect(question.errors[:none_of_the_above_answer]).to be_empty
377387
end
388+
389+
it "clears the none_of_the_above_answer before validating" do
390+
question.none_of_the_above_answer = "Some answer"
391+
expect(question).to be_valid
392+
expect(question.none_of_the_above_answer).to be_nil
393+
end
378394
end
379395
end
380396
end

0 commit comments

Comments
 (0)