From 469eb220ac42b06aaf0aee5d62176f8accd848bd Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 21 Feb 2022 12:14:36 +0000 Subject: [PATCH] Reset values for dependant derived questions (only layear) --- app/models/case_log.rb | 12 +++++++++++- spec/fixtures/forms/2021_2022.json | 3 ++- spec/models/case_log_spec.rb | 29 +++++++++++++++++++++++++++++ spec/models/form_spec.rb | 4 ++-- 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 4c2ae50d7..58240885f 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -219,6 +219,16 @@ private form.invalidated_page_questions(self).each do |question| public_send("#{question.id}=", nil) if respond_to?(question.id.to_s) end + + dependent_questions = { layear: [{ key: :renewal, value: "No" }] } + + dependent_questions.each do |dependent, conditions| + condition_key = conditions.first[:key] + condition_value = conditions.first[:value] + if public_send("#{condition_key}_changed?") && condition_value == public_send(condition_key) && !public_send("#{dependent}_changed?") + self.layear = nil + end + end end def dynamically_not_required @@ -256,11 +266,11 @@ private end self.has_benefits = get_has_benefits self.nocharge = household_charge == "Yes" ? "No" : "Yes" - self.layear = "Less than 1 year" if renewal == "Yes" self.underoccupation_benefitcap = "No" if renewal == "Yes" && year == 2021 if renewal == "Yes" self.homeless = "No" self.referral = "Internal transfer" + self.layear = "Less than 1 year" end if needstype == "General needs" self.prevten = "Fixed-term private registered provider (PRP) general needs tenancy" if managing_organisation.provider_type == "PRP" diff --git a/spec/fixtures/forms/2021_2022.json b/spec/fixtures/forms/2021_2022.json index 6c7f5e546..970f47c15 100644 --- a/spec/fixtures/forms/2021_2022.json +++ b/spec/fixtures/forms/2021_2022.json @@ -725,7 +725,8 @@ } } } - } + }, + "depends_on": [{ "renewal": "No" }] }, "time_on_la_waiting_list": { "questions": { diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 0f96f1563..6708886bd 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -544,6 +544,35 @@ RSpec.describe CaseLog do expect { case_log.update(startdate: Time.zone.local(2015, 1, 1)) }.not_to raise_error end end + + context "when it changes from a renewal to not a renewal" do + let(:case_log) { FactoryBot.create(:case_log) } + + it "resets inferred layear value" do + case_log.update!({ renewal: "Yes" }) + + record_from_db = ActiveRecord::Base.connection.execute("select layear from case_logs where id=#{case_log.id}").to_a[0] + expect(record_from_db["layear"]).to eq(2) + expect(case_log["layear"]).to eq("Less than 1 year") + + case_log.update!({ renewal: "No" }) + record_from_db = ActiveRecord::Base.connection.execute("select layear from case_logs where id=#{case_log.id}").to_a[0] + expect(record_from_db["layear"]).to eq(nil) + expect(case_log["layear"]).to eq(nil) + end + end + + context "when it is not a renewal" do + let(:case_log) { FactoryBot.create(:case_log) } + + it "saves layear value" do + case_log.update!({ renewal: "No", layear: "1 year but under 2 years" }) + + record_from_db = ActiveRecord::Base.connection.execute("select layear from case_logs where id=#{case_log.id}").to_a[0] + expect(record_from_db["layear"]).to eq(7) + expect(case_log["layear"]).to eq("1 year but under 2 years") + end + end end describe "paper trail" do diff --git a/spec/models/form_spec.rb b/spec/models/form_spec.rb index 685a20212..b14502f97 100644 --- a/spec/models/form_spec.rb +++ b/spec/models/form_spec.rb @@ -131,7 +131,7 @@ RSpec.describe Form, type: :model do describe "invalidated_page_questions" do context "when dependencies are not met" do - let(:expected_invalid) { %w[la_known cbl conditional_question_no_second_question dependent_question 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 expect(form.invalidated_page_questions(case_log).map(&:id).uniq).to eq(expected_invalid) @@ -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 declaration] } + let(:expected_invalid) { %w[la_known 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"