From e3990eb328c7dc818bafac0242f3ff43c0a790db Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire Date: Tue, 1 Nov 2022 15:26:08 +0000 Subject: [PATCH] feat: null coalescing and validations on changing las to one that invalidates rent in supported housing --- app/models/lettings_log.rb | 12 ++++++++++-- app/models/validations/financial_validations.rb | 6 +++++- app/models/validations/soft_validations.rb | 12 ++++++++++-- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/app/models/lettings_log.rb b/app/models/lettings_log.rb index bb154ad2a..5fd503f0d 100644 --- a/app/models/lettings_log.rb +++ b/app/models/lettings_log.rb @@ -437,7 +437,11 @@ class LettingsLog < Log def soft_min_for_period beds = needstype == 2 ? 0 : [self.beds, 4].min - la = needstype == 2 ? Location.find(location_id).location_code : self.la + la = if needstype == 2 + defined?(self.location.location_code) ? self.location.location_code : nil + else + self.la + end soft_min = LaRentRange.find_by(start_year: collection_start_year, la:, beds:, lettype:).soft_min "#{soft_value_for_period(soft_min)} #{SUFFIX_FROM_PERIOD[period].presence || 'every week'}" @@ -445,7 +449,11 @@ class LettingsLog < Log def soft_max_for_period beds = needstype == 2 ? 0 : [self.beds, 4].min - la = needstype == 2 ? Location.find(location_id).location_code : self.la + la = if needstype == 2 + defined?(self.location.location_code) ? self.location.location_code : nil + else + self.la + end soft_max = LaRentRange.find_by(start_year: collection_start_year, la:, beds:, lettype:).soft_max "#{soft_value_for_period(soft_max)} #{SUFFIX_FROM_PERIOD[period].presence || 'every week'}" diff --git a/app/models/validations/financial_validations.rb b/app/models/validations/financial_validations.rb index 203a9b1dc..d6b92c566 100644 --- a/app/models/validations/financial_validations.rb +++ b/app/models/validations/financial_validations.rb @@ -183,7 +183,11 @@ private collection_year = record.collection_start_year beds = record.needstype == 2 ? 0 : [record.beds, 4].min - la = record.needstype == 2 ? Location.find(record.location_id).location_code : record.la + la = if record.needstype == 2 + defined?(record.location.location_code) ? record.location.location_code : nil + else + record.la + end rent_range = LaRentRange.find_by(start_year: collection_year, la:, beds:, lettype: record.lettype) diff --git a/app/models/validations/soft_validations.rb b/app/models/validations/soft_validations.rb index d8730150d..eb9deac16 100644 --- a/app/models/validations/soft_validations.rb +++ b/app/models/validations/soft_validations.rb @@ -29,7 +29,11 @@ module Validations::SoftValidations return unless brent && weekly_value(brent) && startdate beds = needstype == 2 ? 0 : [self.beds, 4].min - la = needstype == 2 ? Location.find(location_id).location_code : self.la + la = if needstype == 2 + defined?(self.location.location_code) ? self.location.location_code : nil + else + self.la + end rent_range = LaRentRange.find_by(start_year: collection_start_year, la:, beds:, lettype: get_lettype) rent_range.present? && weekly_value(brent).between?(rent_range.hard_min, rent_range.soft_min) @@ -39,7 +43,11 @@ module Validations::SoftValidations return unless brent && weekly_value(brent) && startdate beds = needstype == 2 ? 0 : [self.beds, 4].min - la = needstype == 2 ? Location.find(location_id).location_code : self.la + la = if needstype == 2 + defined?(self.location.location_code) ? self.location.location_code : nil + else + self.la + end rent_range = LaRentRange.find_by(start_year: collection_start_year, la:, beds:, lettype: get_lettype) rent_range.present? && weekly_value(brent).between?(rent_range.soft_max, rent_range.hard_max)