From 02cb31947ed896c312c7e7634384c5228ca04fec Mon Sep 17 00:00:00 2001 From: baarkerlounger <5101747+baarkerlounger@users.noreply.github.com> Date: Mon, 14 Mar 2022 15:24:40 +0000 Subject: [PATCH] Compare as float (#384) --- .../validations/financial_validations.rb | 1 + app/models/validations/shared_validations.rb | 2 +- config/forms/2021_2022.json | 50 ++++++++++--------- .../validations/financial_validations_spec.rb | 10 ++++ 4 files changed, 39 insertions(+), 24 deletions(-) diff --git a/app/models/validations/financial_validations.rb b/app/models/validations/financial_validations.rb index 74d49fad2..75f1e475d 100644 --- a/app/models/validations/financial_validations.rb +++ b/app/models/validations/financial_validations.rb @@ -1,4 +1,5 @@ module Validations::FinancialValidations + include Validations::SharedValidations # Validations methods need to be called 'validate_' to run on model save # or 'validate_' to run on submit as well def validate_outstanding_rent_amount(record) diff --git a/app/models/validations/shared_validations.rb b/app/models/validations/shared_validations.rb index 29c588a06..d83c1801a 100644 --- a/app/models/validations/shared_validations.rb +++ b/app/models/validations/shared_validations.rb @@ -21,7 +21,7 @@ module Validations::SharedValidations field = question.check_answer_label || question.id begin - answer = Integer(record.public_send("#{question.id}_before_type_cast")) + answer = Float(record.public_send("#{question.id}_before_type_cast")) rescue ArgumentError record.errors.add question.id.to_sym, I18n.t("validations.numeric.valid", field:, min: question.min, max: question.max) end diff --git a/config/forms/2021_2022.json b/config/forms/2021_2022.json index c3b2c6d22..3e74fd27a 100644 --- a/config/forms/2021_2022.json +++ b/config/forms/2021_2022.json @@ -4532,7 +4532,7 @@ "hint_text": "", "type": "numeric", "min": 0, - "step": "1", + "step": 1, "width": 5, "prefix": "£", "suffix": [ @@ -4580,7 +4580,7 @@ "depends_on": [{ "net_income_soft_validation_triggered?": true }], "title_text": "Net income is outside the expected range based on the main tenant’s working situation", "informative_text": { - "translation": "soft_validations.net_income.hint_text", + "translation": "soft_validations.net_income.hint_text", "argument": ["ecstat1", "earnings"] }, "questions": { @@ -4770,6 +4770,7 @@ "hint_text": "", "type": "numeric", "width": 5, + "step": 0.01, "prefix": "£", "suffix": " every week" } @@ -4866,6 +4867,7 @@ "hint_text": "", "type": "numeric", "width": 5, + "step": 0.01, "prefix": "£", "suffix": " every 2 weeks" } @@ -4912,6 +4914,7 @@ "hint_text": "", "type": "numeric", "width": 5, + "step": 0.01, "prefix": "£", "suffix": " every 4 weeks" } @@ -4958,6 +4961,7 @@ "hint_text": "", "type": "numeric", "width": 5, + "step": 0.01, "prefix": "£", "suffix": " every month" } @@ -4985,7 +4989,7 @@ "hint_text": "This is the amount paid before any charges are added for services (for example, hot water or cleaning). Households may receive housing benefit or Universal Credit towards basic rent.", "type": "numeric", "min": 0, - "step": 1, + "step": 0.01, "width": 5, "prefix": "£", "suffix": " every week", @@ -5004,7 +5008,7 @@ "hint_text": "For example, for cleaning. Households may receive housing benefit or Universal Credit towards the service charge.", "type": "numeric", "min": 0, - "step": 1, + "step": 0.01, "width": 5, "prefix": "£", "suffix": " every week", @@ -5023,7 +5027,7 @@ "hint_text": "For example, for heating or hot water. This doesn’t include housing benefit or Universal Credit.", "type": "numeric", "min": 0, - "step": 1, + "step": 0.01, "width": 5, "prefix": "£", "suffix": " every week", @@ -5042,7 +5046,7 @@ "hint_text": "Any charges made to fund support services included in tenancy agreement.", "type": "numeric", "min": 0, - "step": 1, + "step": 0.01, "width": 5, "prefix": "£", "suffix": " every week", @@ -5061,7 +5065,7 @@ "hint_text": "This is the total for rent and all charges.", "type": "numeric_output", "min": 0, - "step": 1, + "step": 0.01, "width": 5, "prefix": "£", "suffix": " every week", @@ -5208,7 +5212,7 @@ "hint_text": "This is the amount paid before any charges are added for services (for example, hot water or cleaning). Households may receive housing benefit or Universal Credit towards basic rent.", "type": "numeric", "min": 0, - "step": 1, + "step": 0.01, "width": 5, "prefix": "£", "suffix": " every 2 weeks", @@ -5227,7 +5231,7 @@ "hint_text": "For example, for cleaning. Households may receive housing benefit or Universal Credit towards the service charge.", "type": "numeric", "min": 0, - "step": 1, + "step": 0.01, "width": 5, "prefix": "£", "suffix": " every 2 weeks", @@ -5246,7 +5250,7 @@ "hint_text": "For example, for heating or hot water. This doesn’t include housing benefit or Universal Credit.", "type": "numeric", "min": 0, - "step": 1, + "step": 0.01, "width": 5, "prefix": "£", "suffix": " every 2 weeks", @@ -5265,7 +5269,7 @@ "hint_text": "Any charges made to fund support services included in tenancy agreement.", "type": "numeric", "min": 0, - "step": 1, + "step": 0.01, "width": 5, "prefix": "£", "suffix": " every 2 weeks", @@ -5284,7 +5288,7 @@ "hint_text": "This is the total for rent and all charges.", "type": "numeric_output", "min": 0, - "step": 1, + "step": 0.01, "width": 5, "prefix": "£", "suffix": " every 2 weeks", @@ -5331,7 +5335,7 @@ "hint_text": "This is the amount paid before any charges are added for services (for example, hot water or cleaning). Households may receive housing benefit or Universal Credit towards basic rent.", "type": "numeric", "min": 0, - "step": 1, + "step": 0.01, "width": 5, "prefix": "£", "suffix": " every 4 weeks", @@ -5350,7 +5354,7 @@ "hint_text": "For example, for cleaning. Households may receive housing benefit or Universal Credit towards the service charge.", "type": "numeric", "min": 0, - "step": 1, + "step": 0.01, "width": 5, "prefix": "£", "suffix": " every 4 weeks", @@ -5369,7 +5373,7 @@ "hint_text": "For example, for heating or hot water. This doesn’t include housing benefit or Universal Credit.", "type": "numeric", "min": 0, - "step": 1, + "step": 0.01, "width": 5, "prefix": "£", "suffix": " every 4 weeks", @@ -5388,7 +5392,7 @@ "hint_text": "Any charges made to fund support services included in tenancy agreement.", "type": "numeric", "min": 0, - "step": 1, + "step": 0.01, "width": 5, "prefix": "£", "suffix": " every 4 weeks", @@ -5407,7 +5411,7 @@ "hint_text": "This is the total for rent and all charges.", "type": "numeric_output", "min": 0, - "step": 1, + "step": 0.01, "width": 5, "prefix": "£", "suffix": " every 4 weeks", @@ -5454,7 +5458,7 @@ "hint_text": "This is the amount paid before any charges are added for services (for example, hot water or cleaning). Households may receive housing benefit or Universal Credit towards basic rent.", "type": "numeric", "min": 0, - "step": 1, + "step": 0.01, "width": 5, "prefix": "£", "suffix": " every month", @@ -5473,7 +5477,7 @@ "hint_text": "For example, for cleaning. Households may receive housing benefit or Universal Credit towards the service charge.", "type": "numeric", "min": 0, - "step": 1, + "step": 0.01, "width": 5, "prefix": "£", "suffix": " every month", @@ -5492,7 +5496,7 @@ "hint_text": "For example, for heating or hot water. This doesn’t include housing benefit or Universal Credit.", "type": "numeric", "min": 0, - "step": 1, + "step": 0.01, "width": 5, "prefix": "£", "suffix": " every month", @@ -5511,7 +5515,7 @@ "hint_text": "Any charges made to fund support services included in tenancy agreement.", "type": "numeric", "min": 0, - "step": 1, + "step": 0.01, "width": 5, "prefix": "£", "suffix": " every month", @@ -5530,7 +5534,7 @@ "hint_text": "This is the total for rent and all charges.", "type": "numeric_output", "min": 0, - "step": 1, + "step": 0.01, "width": 5, "prefix": "£", "suffix": " every month", @@ -5607,7 +5611,7 @@ "hint_text": "Give an estimated amount if you don’t know the exact figure.", "type": "numeric", "min": 0, - "step": 1, + "step": 0.01, "width": 5, "prefix": "£", "suffix": " every month" diff --git a/spec/models/validations/financial_validations_spec.rb b/spec/models/validations/financial_validations_spec.rb index 5be25672c..7528bef98 100644 --- a/spec/models/validations/financial_validations_spec.rb +++ b/spec/models/validations/financial_validations_spec.rb @@ -175,5 +175,15 @@ RSpec.describe Validations::FinancialValidations do .to include(match I18n.t("validations.financial.negative_currency")) end end + + context "when the field allows decimals" do + it "correctly co-erces values" do + record.brent = "20" + record.pscharge = "0.0" + record.period = "2" + financial_validator.validate_numeric_min_max(record) + expect(record.errors["pscharge"]).to be_empty + end + end end end