Browse Source

CLDC-2015 Validate children to be 12 or more years younger than buyer 1 (#1444)

* feat: add validation

* feat: add tests

* refactor: improve test readability

* refactor: improve test readability
review-app-for-23-24-mobbing
natdeanlewissoftwire 2 years ago committed by GitHub
parent
commit
bd8fd3fc1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 14
      app/models/validations/sales/household_validations.rb
  2. 1
      config/locales/en.yml
  3. 23
      spec/models/validations/sales/household_validations_spec.rb

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

@ -6,6 +6,7 @@ module Validations::Sales::HouseholdValidations
validate_person_age_matches_relationship(record, n)
validate_person_age_and_relationship_matches_economic_status(record, n)
validate_person_age_matches_economic_status(record, n)
validate_child_12_years_younger(record, n)
end
shared_validate_partner_count(record, 6)
end
@ -73,6 +74,19 @@ private
end
end
def validate_child_12_years_younger(record, person_num)
buyer_1_age = record.public_send("age1")
person_age = record.public_send("age#{person_num}")
relationship = record.public_send("relat#{person_num}")
return unless buyer_1_age && person_age && relationship
if person_age > buyer_1_age - 12 && person_is_child?(relationship)
record.errors.add "age1", I18n.t("validations.household.age.child_12_years_younger")
record.errors.add "age#{person_num}", I18n.t("validations.household.age.child_12_years_younger")
record.errors.add "relat#{person_num}", I18n.t("validations.household.age.child_12_years_younger")
end
end
def person_is_partner?(relationship)
relationship == "P"
end

1
config/locales/en.yml

@ -347,6 +347,7 @@ en:
child_under_16: "Answer cannot be under 16 as person’s %{person_num} working situation is not ‘child under 16’"
child_over_16: "Answer cannot be over 16 as person’s %{person_num} working situation is ‘child under 16‘"
child_over_20: "Answer cannot be 20 or over as the relationship is ‘child’"
child_12_years_younger: "A child must be at least 12 years younger than their parent"
not_student_16_19: "Answer cannot be between 16 and 19 as person %{person_num} is a child of the lead tenant but is not a full-time student"
student_16_19:
cannot_be_16_19:

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

@ -72,6 +72,29 @@ RSpec.describe Validations::Sales::HouseholdValidations do
expect(record.errors["age2"])
.to include(match I18n.t("validations.household.age.child_over_16", person_num: 2))
end
it "validates the child is at least 12 years younger than buyer 1" do
record.age1 = 30
record.age2 = record.age1 - 11
record.relat2 = "C"
household_validator.validate_household_number_of_other_members(record)
expect(record.errors["age1"])
.to include(match I18n.t("validations.household.age.child_12_years_younger", person_num: 2))
expect(record.errors["age2"])
.to include(match I18n.t("validations.household.age.child_12_years_younger", person_num: 2))
expect(record.errors["relat2"])
.to include(match I18n.t("validations.household.age.child_12_years_younger", person_num: 2))
end
it "expects the child is at least 12 years younger than buyer 1" do
record.age1 = 30
record.age2 = record.age1 - 12
record.relat2 = "C"
household_validator.validate_household_number_of_other_members(record)
expect(record.errors["age1"]).to be_empty
expect(record.errors["age2"]).to be_empty
expect(record.errors["relate2"]).to be_empty
end
end
it "validates that a person over 20 must not be a child of the buyer" do

Loading…
Cancel
Save