Browse Source

Compare as float (#384)

pull/389/head
baarkerlounger 3 years ago committed by GitHub
parent
commit
02cb31947e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      app/models/validations/financial_validations.rb
  2. 2
      app/models/validations/shared_validations.rb
  3. 48
      config/forms/2021_2022.json
  4. 10
      spec/models/validations/financial_validations_spec.rb

1
app/models/validations/financial_validations.rb

@ -1,4 +1,5 @@
module Validations::FinancialValidations
include Validations::SharedValidations
# Validations methods need to be called 'validate_<page_name>' to run on model save
# or 'validate_' to run on submit as well
def validate_outstanding_rent_amount(record)

2
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

48
config/forms/2021_2022.json

@ -4532,7 +4532,7 @@
"hint_text": "",
"type": "numeric",
"min": 0,
"step": "1",
"step": 1,
"width": 5,
"prefix": "£",
"suffix": [
@ -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"

10
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

Loading…
Cancel
Save