Browse Source

Fix armed forces case

pull/235/head
baarkerlounger 3 years ago
parent
commit
5b44a38328
  1. 21
      app/models/form/page.rb
  2. 8
      app/models/validations/household_validations.rb
  3. 2
      app/views/form/page.html.erb
  4. 17
      config/forms/2021_2022.json
  5. 8
      spec/features/form/conditional_questions_spec.rb
  6. 24
      spec/fixtures/forms/2021_2022.json
  7. 4
      spec/helpers/conditional_questions_helper_spec.rb

21
app/models/form/page.rb

@ -27,14 +27,19 @@ class Form::Page
subsection.enabled?(case_log) && depends_on_met(case_log)
end
# We expect to render only one radio question (with conditionals)
# def questions_to_render
# if questions.first.type == "radio"
# [questions.first]
# else
# questions
# end
# end
def non_conditional_questions
@non_conditional_questions ||= questions.reject do |q|
conditional_questions.include?(q.id)
end
end
def conditional_questions
@conditional_questions ||= questions.flat_map { |q|
next if q.conditional_for.blank?
q.conditional_for.keys
}.compact
end
private

8
app/models/validations/household_validations.rb

@ -26,20 +26,12 @@ module Validations::HouseholdValidations
end
def validate_armed_forces_injured(record)
if (record.armedforces == "A current or former regular in the UK Armed Forces (excluding National Service)" || record.armedforces == "A current or former reserve in the UK Armed Forces (excluding National Service)") && record.reservist.blank?
record.errors.add :reservist, I18n.t("validations.household.reservist.injury_required")
end
if (record.armedforces == "No" || record.armedforces == "Prefer not to say") && record.reservist.present?
record.errors.add :reservist, I18n.t("validations.household.reservist.injury_not_required")
end
end
def validate_armed_forces_active_response(record)
if record.armedforces == "A current or former regular in the UK Armed Forces (excluding National Service)" && record.leftreg.blank?
record.errors.add :leftreg, I18n.t("validations.household.leftreg.question_required")
end
if record.armedforces != "A current or former regular in the UK Armed Forces (excluding National Service)" && record.leftreg.present?
record.errors.add :leftreg, I18n.t("validations.household.leftreg.question_not_required")
end

2
app/views/form/page.html.erb

@ -31,7 +31,7 @@
<%= form_with model: @case_log, url: form_case_log_path(@case_log), method: "post" do |f| %>
<%= f.govuk_error_summary %>
<% @page.questions.map do |question| %>
<% @page.non_conditional_questions.map do |question| %>
<div id=<%= question.id + "_div " %> >
<% if question.read_only? %>
<hr class="govuk-section-break govuk-section-break--visible govuk-section-break--m">

17
config/forms/2021_2022.json

@ -924,16 +924,15 @@
"2": "A spouse / civil partner of a UK Armed Forces member who has separated or been bereaved within the last 2 years",
"3": "No",
"4": "Tenant prefers not to say"
},
"conditional_for": {
"leftreg": [
"A current or former regular in the UK Armed Forces (excluding National Service)"
],
"reservist": [
"A current or former regular in the UK Armed Forces (excluding National Service)"
]
}
},
}
}
},
"armed_forces_member": {
"header": "Experience of the UK Armed Forces",
"description": "",
"depends_on": [{ "armedforces": "A current or former regular in the UK Armed Forces (excluding National Service)" }],
"questions": {
"leftreg": {
"header": "Are they still serving?",
"hint_text": "",

8
spec/features/form/conditional_questions_spec.rb

@ -30,13 +30,11 @@ RSpec.describe "Form Conditional Questions" do
visit("/logs/#{id}/armed-forces")
# Something about our styling makes the selenium webdriver think the actual radio buttons are not visible so we allow label click here
choose("case-log-armedforces-a-current-or-former-regular-in-the-uk-armed-forces-excluding-national-service-field", allow_label_click: true)
expect(page).to have_selector("#reservist_div")
choose("case-log-reservist-no-field", allow_label_click: true)
expect(page).to have_checked_field("case-log-reservist-no-field", visible: false)
fill_in("case-log-leftreg-field", with: "text")
choose("case-log-armedforces-no-field", allow_label_click: true)
expect(page).not_to have_selector("#reservist_div")
expect(page).not_to have_field("case-log-leftreg-field")
choose("case-log-armedforces-a-current-or-former-regular-in-the-uk-armed-forces-excluding-national-service-field", allow_label_click: true)
expect(page).to have_unchecked_field("case-log-reservist-no-field", visible: false)
expect(page).to have_css("#case-log-leftreg-field", text: "")
end
end
end

24
spec/fixtures/forms/2021_2022.json vendored

@ -124,32 +124,14 @@
"4": "Tenant prefers not to say"
},
"conditional_for": {
"leftreg": ["A current or former regular in the UK Armed Forces (excluding National Service)"],
"reservist": ["A current or former regular in the UK Armed Forces (excluding National Service)"]
"leftreg": ["A current or former regular in the UK Armed Forces (excluding National Service)"]
}
},
"leftreg": {
"header": "Are they still serving?",
"hint_text": "",
"type": "radio",
"check_answer_label": "When did they leave the Armed Forces?",
"answer_options": {
"0": "Yes",
"1": "No - they left up to 5 years ago",
"2": "No - they left more than 5 years ago",
"3": "Prefer not to say"
}
},
"reservist": {
"header": "Were they seriously injured or ill as a result of their service?",
"hint_text": "",
"type": "radio",
"check_answer_label": "Has anyone in the household been seriously injured or ill as a result of their service in the armed forces?",
"answer_options": {
"0": "Yes",
"1": "No",
"2": "Prefer not to say"
}
"type": "text",
"check_answer_label": "When did they leave the Armed Forces?"
}
}
},

4
spec/helpers/conditional_questions_helper_spec.rb

@ -5,7 +5,7 @@ RSpec.describe ConditionalQuestionsHelper do
let(:page) { case_log.form.get_page("armed_forces") }
describe "conditional questions for page" do
let(:conditional_pages) { %w[leftreg reservist] }
let(:conditional_pages) { %w[leftreg] }
it "returns the question keys of all conditional questions on the given page" do
expect(conditional_questions_for_page(page)).to eq(conditional_pages)
@ -13,7 +13,7 @@ RSpec.describe ConditionalQuestionsHelper do
end
describe "display question key div" do
let(:conditional_question) { page.questions.find { |q| q.id == "reservist" } }
let(:conditional_question) { page.questions.find { |q| q.id == "leftreg" } }
it "returns a non visible div for conditional questions" do
expect(display_question_key_div(page, conditional_question)).to match("style='display:none;'")

Loading…
Cancel
Save