Browse Source

remove hard validation of range for discount question and allow shared validation of numeric questions to handle this case

pull/1214/head
Arthur Campbell 3 years ago
parent
commit
9f1ac16bc2
  1. 8
      app/models/validations/sales/financial_validations.rb
  2. 2
      config/locales/en.yml
  3. 27
      spec/models/validations/sales/financial_validations_spec.rb

8
app/models/validations/sales/financial_validations.rb

@ -27,12 +27,4 @@ module Validations::Sales::FinancialValidations
record.errors.add :cashdis, I18n.t("validations.financial.cash_discount_invalid") record.errors.add :cashdis, I18n.t("validations.financial.cash_discount_invalid")
end end
end end
def validate_percentage(record)
return unless record.discount
if record.discount.negative? || record.discount > 100
record.errors.add :discount, I18n.t("validations.financial.percentage.invalid_percentage")
end
end
end end

2
config/locales/en.yml

@ -275,8 +275,6 @@ en:
out_of_range: "Household rent and other charges must be between %{min_chcharge} and %{max_chcharge} if paying %{period}" out_of_range: "Household rent and other charges must be between %{min_chcharge} and %{max_chcharge} if paying %{period}"
not_provided: "Enter how much rent and other charges the household pays %{period}" not_provided: "Enter how much rent and other charges the household pays %{period}"
cash_discount_invalid: "Cash discount must be £0 - £999,999" cash_discount_invalid: "Cash discount must be £0 - £999,999"
percentage:
invalid_percentage: "Percentage discount must be between 0 - 100"
household: household:
reasonpref: reasonpref:
not_homeless: "Answer cannot be ‘homeless or about to lose their home’ as the tenant was not homeless immediately prior to this letting" not_homeless: "Answer cannot be ‘homeless or about to lose their home’ as the tenant was not homeless immediately prior to this letting"

27
spec/models/validations/sales/financial_validations_spec.rb

@ -99,31 +99,4 @@ RSpec.describe Validations::Sales::FinancialValidations do
expect(record.errors["cashdis"]).to be_empty expect(record.errors["cashdis"]).to be_empty
end end
end end
describe "#validate_percentage" do
let(:record) { FactoryBot.create(:sales_log, ownershipsch: 2, type: 9) }
it "does not add an error when percentage discount is not yet given" do
financial_validator.validate_percentage(record)
expect(record.errors["discount"]).to be_empty
end
it "does not add an error when percentage discount is in correct range" do
record.discount = 2
financial_validator.validate_percentage(record)
expect(record.errors["discount"]).to be_empty
end
it "adds an error when percentage declared is below zero" do
record.discount = -2
financial_validator.validate_percentage(record)
expect(record.errors["discount"]).to include(match I18n.t("validations.financial.percentage.invalid_percentage"))
end
it "adds an error when percentage declared is above 100" do
record.discount = 102
financial_validator.validate_percentage(record)
expect(record.errors["discount"]).to include(match I18n.t("validations.financial.percentage.invalid_percentage"))
end
end
end end

Loading…
Cancel
Save