From 9d95707e35eaa227e308fb0dd02ba95d6b2a6c3c Mon Sep 17 00:00:00 2001 From: MadeTech Dushan Date: Tue, 19 Oct 2021 15:10:06 +0100 Subject: [PATCH 1/2] add other reason left settled home validation --- app/models/case_log.rb | 10 ++++++++++ spec/models/case_log_spec.rb | 16 ++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 46d766ccc..1f4219d57 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -15,6 +15,16 @@ class CaseLogValidator < ActiveModel::Validator end end + def validate_other_reason_for_leaving_last_settled_home(record) + if record.reason_for_leaving_last_settled_home == "Other" && record.other_reason_for_leaving_last_settled_home.blank? + record.errors.add :other_reason_for_leaving_last_settled_home, "If reason for leaving settled home is other then the other reason must be provided" + end + + if record.reason_for_leaving_last_settled_home != "Other" && record.other_reason_for_leaving_last_settled_home.present? + record.errors.add :other_reason_for_leaving_last_settled_home, "The other reason must not be provided if the reason for leaving settled home was not other" + end + end + def validate(record) # If we've come from the form UI we only want to validate the specific fields # that have just been submitted. If we're submitting a log via API or Bulk Upload diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 6b0805250..dff400389 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -25,6 +25,22 @@ RSpec.describe Form, type: :model do it "validates number of relets is over 0" do expect { CaseLog.create!(property_number_of_times_relet: 0) }.to raise_error(ActiveRecord::RecordInvalid) end + + context "other reason for leaving last settled home validation" do + it "must be provided if main reason for leaving last settled home was given as other" do + expect { + CaseLog.create!(reason_for_leaving_last_settled_home: "Other", + other_reason_for_leaving_last_settled_home: nil) + }.to raise_error(ActiveRecord::RecordInvalid) + end + + it "must not be provided if the main reason for leaving settled home is not other" do + expect { + CaseLog.create!(reason_for_leaving_last_settled_home: "Repossession", + other_reason_for_leaving_last_settled_home: "the other reason provided") + }.to raise_error(ActiveRecord::RecordInvalid) + end + end end describe "status" do From 9a8945ea9a8667822ae4853287e3094617acdd15 Mon Sep 17 00:00:00 2001 From: MadeTech Dushan Date: Wed, 20 Oct 2021 10:10:22 +0100 Subject: [PATCH 2/2] dynamically not required attributes for case logs --- app/models/case_log.rb | 10 +++++++++- spec/fixtures/complete_case_log.json | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 1f4219d57..9a84c633e 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -97,6 +97,14 @@ private end def mandatory_fields - attributes.except(*AUTOGENERATED_FIELDS) + required = attributes.except(*AUTOGENERATED_FIELDS) + + dynamically_not_required = [] + + if reason_for_leaving_last_settled_home != "Other" + dynamically_not_required << "other_reason_for_leaving_last_settled_home" + end + + required.delete_if { |key, _value| dynamically_not_required.include?(key) } end end diff --git a/spec/fixtures/complete_case_log.json b/spec/fixtures/complete_case_log.json index 11f073714..da2e0da20 100644 --- a/spec/fixtures/complete_case_log.json +++ b/spec/fixtures/complete_case_log.json @@ -89,7 +89,7 @@ "chr_letting": false, "cap_letting": false, "outstanding_rent_or_charges": 25, - "other_reason_for_leaving_last_settled_home": "Other reason", + "other_reason_for_leaving_last_settled_home": null, "accessibility_requirements_fully_wheelchair_accessible_housing": true, "accessibility_requirements_wheelchair_access_to_essential_rooms": false, "accessibility_requirements_level_access_housing": false,