Browse Source

add min and max to household count question and remove bespoke validation. Remove code allowing max and no min given this situation does not seem to exist int eh form

pull/1253/head
Arthur Campbell 3 years ago
parent
commit
dc3cf3a418
  1. 2
      app/models/form/sales/questions/number_of_others_in_property.rb
  2. 8
      app/models/validations/sales/household_validations.rb
  3. 2
      app/models/validations/shared_validations.rb
  4. 1
      config/locales/en.yml
  5. 42
      spec/models/validations/sales/household_validations_spec.rb

2
app/models/form/sales/questions/number_of_others_in_property.rb

@ -7,5 +7,7 @@ class Form::Sales::Questions::NumberOfOthersInProperty < ::Form::Question
@type = "numeric"
@hint_text = "You can provide details for a maximum of 4 other people."
@width = 2
@min = 0
@max = 4
end
end

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

@ -1,14 +1,6 @@
module Validations::Sales::HouseholdValidations
include Validations::SharedValidations
def validate_number_of_other_people_living_in_the_property(record)
return if record.hholdcount.blank?
unless record.hholdcount >= 0 && record.hholdcount <= 4
record.errors.add :hholdcount, I18n.t("validations.numeric.within_range", field: "Number of other people living in the property", min: 0, max: 4)
end
end
def validate_household_number_of_other_members(record)
(2..6).each do |n|
validate_person_age_matches_relationship(record, n)

2
app/models/validations/shared_validations.rb

@ -114,8 +114,6 @@ private
record.errors.add question.id.to_sym, I18n.t("validations.numeric.within_range", field:, min:, max:)
elsif min
record.errors.add question.id.to_sym, I18n.t("validations.numeric.above_min", field:, min:)
else
record.errors.add question.id.to_sym, I18n.t("validations.numeric.below_max", field:, max:)
end
end
end

1
config/locales/en.yml

@ -133,7 +133,6 @@ en:
numeric:
within_range: "%{field} must be between %{min} and %{max}"
above_min: "%{field} must be at least %{min}"
below_max: "%{field} must be at most %{max}"
date:
invalid_date: "Enter a date in the correct format, for example 31 1 2022"
outside_collection_window: "Enter a date within the current collection windows"

42
spec/models/validations/sales/household_validations_spec.rb

@ -5,48 +5,6 @@ RSpec.describe Validations::Sales::HouseholdValidations do
let(:validator_class) { Class.new { include Validations::Sales::HouseholdValidations } }
describe "#validate_number_of_other_people_living_in_the_property" do
context "when within permitted bounds" do
let(:record) { build(:sales_log, hholdcount: 2) }
it "does not add an error" do
household_validator.validate_number_of_other_people_living_in_the_property(record)
expect(record.errors[:hholdcount]).not_to be_present
end
end
context "when blank" do
let(:record) { build(:sales_log, hholdcount: nil) }
it "does not add an error" do
household_validator.validate_number_of_other_people_living_in_the_property(record)
expect(record.errors[:hholdcount]).not_to be_present
end
end
context "when below lower bound" do
let(:record) { build(:sales_log, hholdcount: -1) }
it "adds an error" do
household_validator.validate_number_of_other_people_living_in_the_property(record)
expect(record.errors[:hholdcount]).to be_present
end
end
context "when higher than upper bound" do
let(:record) { build(:sales_log, hholdcount: 5) }
it "adds an error" do
household_validator.validate_number_of_other_people_living_in_the_property(record)
expect(record.errors[:hholdcount]).to be_present
end
end
end
describe "household member validations" do
let(:record) { build(:sales_log) }

Loading…
Cancel
Save