From 69fae70991a3e04b6a6e6b134ed980a2095e054e Mon Sep 17 00:00:00 2001 From: Sam Seed Date: Thu, 9 Mar 2023 16:24:53 +0000 Subject: [PATCH] refactor: dry out validation --- .../validations/sales/financial_validations.rb | 12 ++++++++---- config/locales/en.yml | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/app/models/validations/sales/financial_validations.rb b/app/models/validations/sales/financial_validations.rb index 6e9e0814c..dfba04878 100644 --- a/app/models/validations/sales/financial_validations.rb +++ b/app/models/validations/sales/financial_validations.rb @@ -55,10 +55,14 @@ module Validations::Sales::FinancialValidations def validate_percentage_bought_at_least_threshold(record) return unless record.stairbought && record.type - if ([2, 18, 16, 24].include? record.type) && record.stairbought < 10 - record.errors.add :stairbought, I18n.t("validations.financial.staircasing.percentage_bought_must_be_at_least_threshold", percentage: 10) - elsif record.type == 30 && record.stairbought < 1 - record.errors.add :stairbought, I18n.t("validations.financial.staircasing.percentage_bought_must_be_at_least_threshold", percentage: 1) + if [2, 18, 16, 24].include? record.type + threshold = 10 + elsif record.type == 30 + threshold = 1 + end + + if threshold && record.stairbought < threshold + record.errors.add :stairbought, I18n.t("validations.financial.staircasing.percentage_bought_must_be_at_least_threshold", threshold:) end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 7c7ffe1ee..045397650 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -302,7 +302,7 @@ en: staircasing: percentage_bought_must_be_greater_than_percentage_owned: "Total percentage buyer now owns must be more than percentage bought in this transaction" older_person_percentage_owned_maximum_75: "Percentage cannot be above 75% under Older Person's Shared Ownership" - percentage_bought_must_be_at_least_threshold: "The minimum increase in equity while staircasing is %{percentage}%" + percentage_bought_must_be_at_least_threshold: "The minimum increase in equity while staircasing is %{threshold}%" household: reasonpref: