Browse Source

do not reset questions that have selected answer options

pull/322/head
Kat 3 years ago
parent
commit
fa6faf99e5
  1. 21
      app/models/case_log.rb
  2. 7
      app/models/form.rb
  3. 3
      spec/fixtures/forms/2021_2022.json
  4. 11
      spec/models/case_log_spec.rb
  5. 2
      spec/models/form_spec.rb

21
app/models/case_log.rb

@ -213,14 +213,18 @@ private
end
end
def reset_invalidated_dependent_fields!
return unless form
def reset_not_routed_questions
form.invalidated_page_questions(self).each do |question|
public_send("#{question.id}=", nil) if respond_to?(question.id.to_s)
enabled = form.enabled_page_questions(self)
contains_selected_answer_option = enabled.map(&:id).include?(question.id) && enabled.find { |q| q.id == question.id }.answer_options.values.map { |x| x["value"] }.include?(public_send(question.id))
unless contains_selected_answer_option
public_send("#{question.id}=", nil) if respond_to?(question.id.to_s)
end
end
end
dependent_questions = { layear: [{ key: :renewal, value: "No" }] }
def reset_derived_questions
dependent_questions = { layear: [{ key: :renewal, value: "No" }], homeless: [{ key: :renewal, value: "No" }] }
dependent_questions.each do |dependent, conditions|
condition_key = conditions.first[:key]
@ -231,6 +235,13 @@ private
end
end
def reset_invalidated_dependent_fields!
return unless form
reset_not_routed_questions
reset_derived_questions
end
def dynamically_not_required
(form.invalidated_questions(self) + form.readonly_questions).map(&:id).uniq
end

7
app/models/form.rb

@ -116,9 +116,12 @@ class Form
def invalidated_page_questions(case_log)
# we're already treating address fields as a special case and reset their values upon saving a case_log
address_questions = %w[postcode_known la previous_postcode_known prevloc property_postcode previous_postcode]
invalidated_pages(case_log).flat_map(&:questions).reject { |q| address_questions.include?(q.id) } || []
end
def enabled_page_questions(case_log)
pages_that_are_routed_to = pages.select { |p| p.routed_to?(case_log) }
enabled_question_ids = pages_that_are_routed_to.flat_map(&:questions).map(&:id) || []
invalidated_pages(case_log).flat_map(&:questions).reject { |q| enabled_question_ids.include?(q.id) || address_questions.include?(q.id) } || []
pages_that_are_routed_to.flat_map(&:questions) || []
end
def invalidated_conditional_questions(case_log)

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

@ -396,9 +396,6 @@
"answer_options": {
"0": {
"value": "Yes"
},
"1": {
"value": "No"
}
}
}

11
spec/models/case_log_spec.rb

@ -532,9 +532,17 @@ RSpec.describe CaseLog do
context "with two pages having the same question key, only one's dependency is met" do
let(:case_log) { FactoryBot.create(:case_log, :in_progress, cbl: "Yes", preg_occ: "No") }
it "does not clear the answer" do
it "does not clear the value for answers that apply to both pages" do
expect(case_log.cbl).to eq("Yes")
end
it "does clear the value for answers that do not apply for invalidated page" do
case_log.update!({ wchair: "Yes", sex2: "Female", age2: 33 })
case_log.update!({ cbl: "No" })
case_log.update!({ preg_occ: "Yes" })
expect(case_log.cbl).to eq(nil)
end
end
context "when the case log does not have a valid form set yet" do
@ -573,6 +581,7 @@ RSpec.describe CaseLog do
expect(case_log["layear"]).to eq("1 year but under 2 years")
end
end
end
describe "paper trail" do

2
spec/models/form_spec.rb

@ -139,7 +139,7 @@ RSpec.describe Form, type: :model do
end
context "with two pages having the same question and only one has dependencies met" do
let(:expected_invalid) { %w[la_known conditional_question_no_second_question dependent_question layear declaration] }
let(:expected_invalid) { %w[la_known cbl conditional_question_no_second_question dependent_question layear declaration] }
it "returns an array of question keys whose pages conditions are not met" do
case_log["preg_occ"] = "No"

Loading…
Cancel
Save