Browse Source

Check if a question is enabled before validating it's answer (#753)

pull/754/head
baarkerlounger 2 years ago committed by GitHub
parent
commit
e50bafd64e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      app/controllers/form_controller.rb
  2. 27
      spec/requests/form_controller_spec.rb

11
app/controllers/form_controller.rb

@ -138,8 +138,15 @@ private
end end
def question_is_required?(question) def question_is_required?(question)
CaseLog::OPTIONAL_FIELDS.exclude?(question.id) && CaseLog::OPTIONAL_FIELDS.exclude?(question.id) && required_questions.include?(question.id)
@page.subsection.applicable_questions(@case_log).map(&:id).include?(question.id) end
def required_questions
@required_questions ||= begin
log = @case_log
log.assign_attributes(responses_for_page(@page))
@page.subsection.applicable_questions(log).select { |q| q.enabled?(log) }.map(&:id)
end
end end
def question_missing_response?(responses_for_page, question) def question_missing_response?(responses_for_page, question)

27
spec/requests/form_controller_spec.rb

@ -263,6 +263,33 @@ RSpec.describe FormController, type: :request do
expect(whodunnit_actor.id).to eq(user.id) expect(whodunnit_actor.id).to eq(user.id)
end end
end end
context "when the question has a conditional question" do
context "and the conditional question is not enabled" do
context "but is applicable because it has an inferred check answers display value" do
let(:page_id) { "property_postcode" }
let(:valid_params) do
{
id: case_log.id,
case_log: {
page: page_id,
postcode_known: "0",
postcode_full: "",
},
}
end
before do
case_log.update!(postcode_known: 1, postcode_full: "NW1 8RR")
post "/logs/#{case_log.id}/form", params: valid_params
end
it "does not require you to answer that question" do
expect(response).to redirect_to("/logs/#{case_log.id}/do-you-know-the-local-authority")
end
end
end
end
end end
context "with checkbox questions" do context "with checkbox questions" do

Loading…
Cancel
Save