From 4284f52b1ebea46fc005d751ebf852d422c27c46 Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Thu, 28 Oct 2021 17:36:34 +0100 Subject: [PATCH] Add financial and tenancy validations --- app/models/case_log.rb | 16 +++++ app/validations/financial_validations.rb | 35 ++++++++++ .../household_validations.rb | 64 ------------------- .../property_validations.rb | 0 app/validations/tenancy_validations.rb | 19 ++++++ 5 files changed, 70 insertions(+), 64 deletions(-) create mode 100644 app/validations/financial_validations.rb rename app/{modules => validations}/household_validations.rb (51%) rename app/{modules => validations}/property_validations.rb (100%) create mode 100644 app/validations/tenancy_validations.rb diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 94400b737..eb092b4ae 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -4,6 +4,8 @@ class CaseLogValidator < ActiveModel::Validator # name being call in the validate method works. include HouseholdValidations include PropertyValidations + include FinancialValidations + include TenancyValidations def validate(record) # If we've come from the form UI we only want to validate the specific fields @@ -21,6 +23,20 @@ class CaseLogValidator < ActiveModel::Validator validation_methods.each { |meth| public_send(meth, record) } end end + +private + + def validate_other_field(record, main_field, other_field) + main_field_label = main_field.humanize(capitalize: false) + other_field_label = other_field.humanize(capitalize: false) + if record[main_field] == "Other" && record[other_field].blank? + record.errors.add other_field.to_sym, "If #{main_field_label} is other then #{other_field_label} must be provided" + end + + if record[main_field] != "Other" && record[other_field].present? + record.errors.add other_field.to_sym, "#{other_field_label} must not be provided if #{main_field_label} was not other" + end + end end class CaseLog < ApplicationRecord diff --git a/app/validations/financial_validations.rb b/app/validations/financial_validations.rb new file mode 100644 index 000000000..ee294e52a --- /dev/null +++ b/app/validations/financial_validations.rb @@ -0,0 +1,35 @@ +module FinancialValidations + def validate_outstanding_rent_amount(record) + if record.outstanding_rent_or_charges == "Yes" && record.outstanding_amount.blank? + record.errors.add :outstanding_amount, "You must answer the oustanding amout question if you have outstanding rent or charges." + end + if record.outstanding_rent_or_charges == "No" && record.outstanding_amount.present? + record.errors.add :outstanding_amount, "You must not answer the oustanding amout question if you don't have outstanding rent or charges." + end + end + + EMPLOYED_STATUSES = ["Full-time - 30 hours or more", "Part-time - Less than 30 hours"].freeze + def validate_net_income_uc_proportion(record) + (1..8).any? do |n| + economic_status = record["person_#{n}_economic_status"] + is_employed = EMPLOYED_STATUSES.include?(economic_status) + relationship = record["person_#{n}_relationship"] + is_partner_or_main = relationship == "Partner" || (relationship.nil? && economic_status.present?) + if is_employed && is_partner_or_main && record.net_income_uc_proportion == "All" + record.errors.add :net_income_uc_proportion, "income is from Universal Credit, state pensions or benefits cannot be All if the tenant or the partner works part or full time" + end + end + end + + def validate_net_income(record) + return unless record.person_1_economic_status && record.weekly_net_income + + if record.weekly_net_income > record.applicable_income_range.hard_max + record.errors.add :net_income, "Net income cannot be greater than #{record.applicable_income_range.hard_max} given the tenant's working situation" + end + + if record.weekly_net_income < record.applicable_income_range.hard_min + record.errors.add :net_income, "Net income cannot be less than #{record.applicable_income_range.hard_min} given the tenant's working situation" + end + end +end diff --git a/app/modules/household_validations.rb b/app/validations/household_validations.rb similarity index 51% rename from app/modules/household_validations.rb rename to app/validations/household_validations.rb index 5b36609ac..39e9f34f4 100644 --- a/app/modules/household_validations.rb +++ b/app/validations/household_validations.rb @@ -39,28 +39,6 @@ module HouseholdValidations end end - def validate_outstanding_rent_amount(record) - if record.outstanding_rent_or_charges == "Yes" && record.outstanding_amount.blank? - record.errors.add :outstanding_amount, "You must answer the oustanding amout question if you have outstanding rent or charges." - end - if record.outstanding_rent_or_charges == "No" && record.outstanding_amount.present? - record.errors.add :outstanding_amount, "You must not answer the oustanding amout question if you don't have outstanding rent or charges." - end - end - - EMPLOYED_STATUSES = ["Full-time - 30 hours or more", "Part-time - Less than 30 hours"].freeze - def validate_net_income_uc_proportion(record) - (1..8).any? do |n| - economic_status = record["person_#{n}_economic_status"] - is_employed = EMPLOYED_STATUSES.include?(economic_status) - relationship = record["person_#{n}_relationship"] - is_partner_or_main = relationship == "Partner" || (relationship.nil? && economic_status.present?) - if is_employed && is_partner_or_main && record.net_income_uc_proportion == "All" - record.errors.add :net_income_uc_proportion, "income is from Universal Credit, state pensions or benefits cannot be All if the tenant or the partner works part or full time" - end - end - end - def validate_armed_forces_active_response(record) if record.armed_forces == "Yes - a regular" && record.armed_forces_active.blank? record.errors.add :armed_forces_active, "You must answer the armed forces active question if the tenant has served as a regular in the armed forces" @@ -77,50 +55,8 @@ module HouseholdValidations end end - def validate_fixed_term_tenancy(record) - is_present = record.fixed_term_tenancy.present? - is_in_range = record.fixed_term_tenancy.to_i.between?(2, 99) - is_secure = record.tenancy_type == "Fixed term – Secure" - is_ast = record.tenancy_type == "Fixed term – Assured Shorthold Tenancy (AST)" - conditions = [ - { condition: !(is_secure || is_ast) && is_present, error: "You must only answer the fixed term tenancy length question if the tenancy type is fixed term" }, - { condition: is_ast && !is_in_range, error: "Fixed term – Assured Shorthold Tenancy (AST) should be between 2 and 99 years" }, - { condition: is_secure && (!is_in_range && is_present), error: "Fixed term – Secure should be between 2 and 99 years or not specified" }, - ] - - conditions.each { |condition| condition[:condition] ? (record.errors.add :fixed_term_tenancy, condition[:error]) : nil } - end - - def validate_net_income(record) - return unless record.person_1_economic_status && record.weekly_net_income - - if record.weekly_net_income > record.applicable_income_range.hard_max - record.errors.add :net_income, "Net income cannot be greater than #{record.applicable_income_range.hard_max} given the tenant's working situation" - end - - if record.weekly_net_income < record.applicable_income_range.hard_min - record.errors.add :net_income, "Net income cannot be less than #{record.applicable_income_range.hard_min} given the tenant's working situation" - end - end - - def validate_other_tenancy_type(record) - validate_other_field(record, "tenancy_type", "other_tenancy_type") - end - private - def validate_other_field(record, main_field, other_field) - main_field_label = main_field.humanize(capitalize: false) - other_field_label = other_field.humanize(capitalize: false) - if record[main_field] == "Other" && record[other_field].blank? - record.errors.add other_field.to_sym, "If #{main_field_label} is other then #{other_field_label} must be provided" - end - - if record[main_field] != "Other" && record[other_field].present? - record.errors.add other_field.to_sym, "#{other_field_label} must not be provided if #{main_field_label} was not other" - end - end - def women_of_child_bearing_age_in_household(record) (1..8).any? do |n| next if record["person_#{n}_gender"].nil? || record["person_#{n}_age"].nil? diff --git a/app/modules/property_validations.rb b/app/validations/property_validations.rb similarity index 100% rename from app/modules/property_validations.rb rename to app/validations/property_validations.rb diff --git a/app/validations/tenancy_validations.rb b/app/validations/tenancy_validations.rb new file mode 100644 index 000000000..813cbabbd --- /dev/null +++ b/app/validations/tenancy_validations.rb @@ -0,0 +1,19 @@ +module TenancyValidations + def validate_fixed_term_tenancy(record) + is_present = record.fixed_term_tenancy.present? + is_in_range = record.fixed_term_tenancy.to_i.between?(2, 99) + is_secure = record.tenancy_type == "Fixed term – Secure" + is_ast = record.tenancy_type == "Fixed term – Assured Shorthold Tenancy (AST)" + conditions = [ + { condition: !(is_secure || is_ast) && is_present, error: "You must only answer the fixed term tenancy length question if the tenancy type is fixed term" }, + { condition: is_ast && !is_in_range, error: "Fixed term – Assured Shorthold Tenancy (AST) should be between 2 and 99 years" }, + { condition: is_secure && (!is_in_range && is_present), error: "Fixed term – Secure should be between 2 and 99 years or not specified" }, + ] + + conditions.each { |condition| condition[:condition] ? (record.errors.add :fixed_term_tenancy, condition[:error]) : nil } + end + + def validate_other_tenancy_type(record) + validate_other_field(record, "tenancy_type", "other_tenancy_type") + end +end