Browse Source

Reset values for dependant derived questions (only layear)

pull/322/head
Kat 3 years ago
parent
commit
469eb220ac
  1. 12
      app/models/case_log.rb
  2. 3
      spec/fixtures/forms/2021_2022.json
  3. 29
      spec/models/case_log_spec.rb
  4. 4
      spec/models/form_spec.rb

12
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"

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

@ -725,7 +725,8 @@
}
}
}
}
},
"depends_on": [{ "renewal": "No" }]
},
"time_on_la_waiting_list": {
"questions": {

29
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

4
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"

Loading…
Cancel
Save