From ababf7510352c6f57c7c138a2b4a0e3c2fc8898e Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Mon, 6 Dec 2021 13:31:11 +0000 Subject: [PATCH] Add la validation if rent type is lar or london living rent (#148) --- app/models/constants/case_log.rb | 34 +++++++++++++++++++ .../validations/property_validations.rb | 7 ++++ spec/models/case_log_spec.rb | 18 ++++++++++ 3 files changed, 59 insertions(+) diff --git a/app/models/constants/case_log.rb b/app/models/constants/case_log.rb index 535cd1f83..c503ec1b3 100644 --- a/app/models/constants/case_log.rb +++ b/app/models/constants/case_log.rb @@ -699,4 +699,38 @@ module Constants::CaseLog "London living rent" => "Intermediate Rent", "Other intermediate rent product" => "Intermediate Rent", }.freeze + + LONDON_BOROUGHS = ["City of London", + "Barking & Dagenham", + "Barnet", + "Bexley", + "Brent", + "Bromley", + "Camden", + "Croydon", + "Ealing", + "Enfield", + "Greenwich", + "Hackney", + "Hammersmith & Fulham", + "Haringey", + "Harrow", + "Havering", + "Hillingdon", + "Hounslow", + "Islington", + "Kensington & Chelsea", + "Kingston-upon-Thames", + "Lambeth", + "Lewisham", + "Merton", + "Newham", + "Redbridge", + "Richmond-upon-Thames", + "Southwark", + "Sutton", + "Tower Hamlets", + "Waltham Forest", + "Wandsworth", + "Westminster"].freeze end diff --git a/app/models/validations/property_validations.rb b/app/models/validations/property_validations.rb index e1ba93d35..e11632544 100644 --- a/app/models/validations/property_validations.rb +++ b/app/models/validations/property_validations.rb @@ -1,9 +1,16 @@ module Validations::PropertyValidations # Validations methods need to be called 'validate_' to run on model save # or 'validate_' to run on submit as well + include Constants::CaseLog def validate_property_number_of_times_relet(record) if record.offered && !/^[1-9]$|^0[1-9]$|^1[0-9]$|^20$/.match?(record.offered.to_s) record.errors.add :offered, "Property number of times relet must be between 0 and 20" end end + + def validate_la(record) + if record.la.present? && !LONDON_BOROUGHS.include?(record.la) && (record.rent_type == "London Affordable rent" || record.rent_type == "London living rent") + record.errors.add :la, "Local authority has to be in London" + end + end end diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 28bbc877a..ed39fcb1f 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -808,6 +808,24 @@ RSpec.describe Form, type: :model do }.not_to raise_error end end + + context "Validate local authority" do + it "Has to be london if rent type london affordable rent" do + expect { + CaseLog.create!(la: "Ashford", + rent_type: "London Affordable rent", + owning_organisation: owning_organisation, + managing_organisation: managing_organisation) + }.to raise_error(ActiveRecord::RecordInvalid) + + expect { + CaseLog.create!(la: "Westminster", + rent_type: "London Affordable rent", + owning_organisation: owning_organisation, + managing_organisation: managing_organisation) + }.not_to raise_error + end + end end describe "status" do