From fa0751b144f83bf838c0105a4b6a1b1e0b44f741 Mon Sep 17 00:00:00 2001 From: Nat Dean-Lewis Date: Fri, 24 Apr 2026 14:35:29 +0100 Subject: [PATCH] CLDC-4352: refactoring --- .../lettings_log_variables.rb | 2 +- app/models/lettings_log.rb | 3 +- app/models/log.rb | 8 +++++ app/models/sales_log.rb | 1 + .../validations/financial_validations.rb | 4 +-- app/models/validations/soft_validations.rb | 35 ++++++------------- 6 files changed, 24 insertions(+), 29 deletions(-) diff --git a/app/models/derived_variables/lettings_log_variables.rb b/app/models/derived_variables/lettings_log_variables.rb index 6c7dc955e..f0f4ff279 100644 --- a/app/models/derived_variables/lettings_log_variables.rb +++ b/app/models/derived_variables/lettings_log_variables.rb @@ -339,7 +339,7 @@ private def infer_only_partner!(partner_number) return unless hhmemb - (2..[hhmemb, 8].min).each do |i| + (2..people_with_details).each do |i| next if i == partner_number if ["P", nil].include?(public_send("relat#{i}")) diff --git a/app/models/lettings_log.rb b/app/models/lettings_log.rb index 15e89fa58..0dd805ef4 100644 --- a/app/models/lettings_log.rb +++ b/app/models/lettings_log.rb @@ -191,6 +191,7 @@ class LettingsLog < Log NUM_OF_WEEKS_FROM_PERIOD = { 2 => 26, 3 => 13, 4 => 12, 5 => 50, 6 => 49, 7 => 48, 8 => 47, 9 => 46, 11 => 51, 1 => 52, 10 => 53 }.freeze SUFFIX_FROM_PERIOD = { 2 => "every 2 weeks", 3 => "every 4 weeks", 4 => "every month" }.freeze DUPLICATE_LOG_ATTRIBUTES = %w[owning_organisation_id tenancycode startdate age1_known age1 sex1 sexrab1 ecstat1 tcharge household_charge chcharge].freeze + MAX_PEOPLE_WITH_DETAILS = 8 RENT_TYPE = { social_rent: 0, affordable_rent: 1, @@ -284,7 +285,7 @@ class LettingsLog < Log range = ALLOWED_INCOME_RANGES[ecstat1].clone if hhmemb > 1 - (2..[hhmemb, 8].min).each do |person_index| + (2..people_with_details).each do |person_index| ecstat = self["ecstat#{person_index}"] if ecstat.nil? diff --git a/app/models/log.rb b/app/models/log.rb index 5500991b6..abb652474 100644 --- a/app/models/log.rb +++ b/app/models/log.rb @@ -204,6 +204,14 @@ class Log < ApplicationRecord false end + def people_with_details + [hhmemb || max_people_with_details, max_people_with_details].min + end + + def max_people_with_details + self.class::MAX_PEOPLE_WITH_DETAILS + end + def ethnic_refused? ethnic_group == 17 end diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index 4d6e152a2..5c2bcf156 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -104,6 +104,7 @@ class SalesLog < Log OPTIONAL_FIELDS = %w[purchid othtype buyers_organisations].freeze DUPLICATE_LOG_ATTRIBUTES = %w[owning_organisation_id purchid saledate age1_known age1 sex1 sexrab1 ecstat1 postcode_full uprn address_line1].freeze + MAX_PEOPLE_WITH_DETAILS = 6 def lettings? false diff --git a/app/models/validations/financial_validations.rb b/app/models/validations/financial_validations.rb index 0e75d05c4..dd8029ebb 100644 --- a/app/models/validations/financial_validations.rb +++ b/app/models/validations/financial_validations.rb @@ -41,7 +41,7 @@ module Validations::FinancialValidations :over_hard_max, message: I18n.t("validations.lettings.financial.hhmemb.earnings_over_hard_max", earnings: format_as_currency(record.earnings), frequency:), ) - (1..[record.hhmemb, 8].min).each do |n| + (1..record.people_with_details).each do |n| record.errors.add( "ecstat#{n}", :over_hard_max, @@ -70,7 +70,7 @@ module Validations::FinancialValidations :under_hard_min, message: I18n.t("validations.lettings.financial.hhmemb.earnings_under_hard_min", earnings: format_as_currency(record.earnings), frequency:), ) - (1..[record.hhmemb, 8].min).each do |n| + (1..record.people_with_details).each do |n| record.errors.add( "ecstat#{n}", :under_hard_min, diff --git a/app/models/validations/soft_validations.rb b/app/models/validations/soft_validations.rb index 150633e15..71ef99a4f 100644 --- a/app/models/validations/soft_validations.rb +++ b/app/models/validations/soft_validations.rb @@ -208,8 +208,7 @@ module Validations::SoftValidations def multiple_partners? return unless hhmemb - max_person_with_details = sales? ? [hhmemb, 6].min : [hhmemb, 8].min - (2..max_person_with_details).many? { |n| public_send("relat#{n}") == "P" } + (2..people_with_details).many? { |n| public_send("relat#{n}") == "P" } end def at_least_one_working_situation_is_sickness_and_household_sickness_is_no? @@ -219,22 +218,18 @@ module Validations::SoftValidations private def all_tenants_age_and_gender_information_completed? - return false if hhmemb.present? && hhmemb > 8 + return false if hhmemb.present? && hhmemb > max_people_with_details return false unless all_tenants_gender_information_completed? - person_count = hhmemb || 8 - - (1..person_count).all? do |n| + (1..people_with_details).all? do |n| public_send("age#{n}").present? && public_send("age#{n}_known").present? && public_send("age#{n}_known").zero? end end def all_tenants_gender_information_completed? - return false if hhmemb.present? && hhmemb > 8 - - person_count = hhmemb || 8 + return false if hhmemb.present? && hhmemb > max_people_with_details - (1..person_count).all? do |n| + (1..people_with_details).all? do |n| tenant_gender_information_completed?(n) end end @@ -258,27 +253,21 @@ private end def any_non_male_in_expected_pregnancy_age_range(min, max) - max_person_with_details = [hhmemb || 8, 8].min - - (1..max_person_with_details).any? do |n| + (1..people_with_details).any? do |n| person_in_expected_pregnancy_age_range(n, min, max) && person_is_non_male(n) end end def non_males_in_the_household? - max_person_with_details = [hhmemb || 8, 8].min - - (1..max_person_with_details).any? do |n| + (1..people_with_details).any? do |n| person_is_non_male(n) end end def all_male_tenants_in_the_household? - return false if hhmemb.present? && hhmemb > 8 + return false if hhmemb.present? && hhmemb > max_people_with_details - person_count = hhmemb || 8 - - (1..person_count).all? do |n| + (1..people_with_details).all? do |n| person_is_male(n) end end @@ -344,11 +333,7 @@ private end def at_least_one_person_working_situation_is_illness? - return if hhmemb.present? && hhmemb > 8 - - person_count = hhmemb || 8 - - (1..person_count).any? { |n| public_send("ecstat#{n}") == 8 } + (1..people_with_details).any? { |n| public_send("ecstat#{n}") == 8 } end def no_one_in_household_with_illness?