diff --git a/app/models/validations/sales/household_validations.rb b/app/models/validations/sales/household_validations.rb index ec73b4abb..e53b7154e 100644 --- a/app/models/validations/sales/household_validations.rb +++ b/app/models/validations/sales/household_validations.rb @@ -45,12 +45,6 @@ private student = person_is_fulltime_student?(economic_status) child = person_is_child?(relationship) - if age_between_16_19 && student && !child - record.errors.add "age#{person_num}", I18n.t("validations.household.age.student_16_19.cannot_be_16_19.student_not_child") - record.errors.add "ecstat#{person_num}", I18n.t("validations.household.ecstat.student_16_19.cannot_be_student.16_19_not_child") - record.errors.add "relat#{person_num}", I18n.t("validations.household.relat.student_16_19.must_be_child") - end - if age_between_16_19 && !student && child record.errors.add "age#{person_num}", I18n.t("validations.household.age.student_16_19.cannot_be_16_19.child_not_student") record.errors.add "ecstat#{person_num}", I18n.t("validations.household.ecstat.student_16_19.must_be_student") diff --git a/app/models/validations/sales/soft_validations.rb b/app/models/validations/sales/soft_validations.rb index a44e0a293..5819b0fe1 100644 --- a/app/models/validations/sales/soft_validations.rb +++ b/app/models/validations/sales/soft_validations.rb @@ -103,6 +103,17 @@ module Validations::Sales::SoftValidations mscharge > soft_max end + (2..6).each do |person_num| + define_method("person_#{person_num}_student_not_child?") do + relat = send("relat#{person_num}") + ecstat = send("ecstat#{person_num}") + age = send("age#{person_num}") + return unless age && ecstat && relat + + age.between?(16, 19) && ecstat == 7 && relat != "C" + end + end + private def sale_range diff --git a/config/locales/en.yml b/config/locales/en.yml index 2d17cf207..d07a84049 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -344,7 +344,6 @@ en: 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: - student_not_child: "Person cannot be aged 16-19 if they are a student but don't have relationship ‘child’" child_not_student: "Person cannot be aged 16-19 if they have relationship ‘child’ but are not a student" must_be_16_19: "Person must be aged 16-19 if they are a student and have relationship ‘child’" partner_under_16: "Cannot be under 16 if the relationship is partner" @@ -358,7 +357,6 @@ en: student_16_19: cannot_be_student: child_not_16_19: "Person cannot be a student if they are not aged 16-19 but have relationship ‘child’" - 16_19_not_child: "Person cannot be a student if they are aged 16-19 but don‘t have relationship ‘child’" must_be_student: "Person must be a student if they are aged 16-19 and have relationship ‘child’" retired_male: "Answer cannot be ‘retired’ as the male tenant is under 65" retired_female: "Answer cannot be ‘retired’ as the female tenant is under 60" @@ -372,7 +370,6 @@ en: cannot_be_child: student_not_16_19: "Answer cannot be ‘child’ if the person is a student but not aged 16-19" 16_19_not_student: "Answer cannot be ‘child’ if the person is aged 16-19 but not a student" - must_be_child: "Answer must be ‘child’ if the person is aged 16-19 and a student" housingneeds_a: one_or_two_choices: "You can only select one option or ‘other disabled access needs’ plus ‘wheelchair-accessible housing’, ‘wheelchair access to essential rooms’ or ‘level access housing’" housingneeds_type: diff --git a/spec/models/validations/sales/household_validations_spec.rb b/spec/models/validations/sales/household_validations_spec.rb index 10367390e..a872bc083 100644 --- a/spec/models/validations/sales/household_validations_spec.rb +++ b/spec/models/validations/sales/household_validations_spec.rb @@ -84,17 +84,14 @@ RSpec.describe Validations::Sales::HouseholdValidations do .to include(match I18n.t("validations.household.age.child_over_20")) end - it "adds errors for a person aged 16-19 who is a student but not a child of the buyer" do + it "adds does not add and error for a person aged 16-19 who is a student but not a child of the buyer" do record.age2 = 18 record.ecstat2 = "7" record.relat2 = "P" household_validator.validate_household_number_of_other_members(record) - expect(record.errors["relat2"]) - .to include(match I18n.t("validations.household.relat.student_16_19.must_be_child")) - expect(record.errors["age2"]) - .to include(match I18n.t("validations.household.age.student_16_19.cannot_be_16_19.student_not_child")) - expect(record.errors["ecstat2"]) - .to include(match I18n.t("validations.household.ecstat.student_16_19.cannot_be_student.16_19_not_child")) + expect(record.errors["relat2"]).to be_empty + expect(record.errors["ecstat2"]).to be_empty + expect(record.errors["age2"]).to be_empty end it "adds errors for a person aged 16-19 who is a child of the buyer but not a student" do diff --git a/spec/models/validations/sales/soft_validations_spec.rb b/spec/models/validations/sales/soft_validations_spec.rb index 5d475b450..dc8bbfedd 100644 --- a/spec/models/validations/sales/soft_validations_spec.rb +++ b/spec/models/validations/sales/soft_validations_spec.rb @@ -730,4 +730,72 @@ RSpec.describe Validations::Sales::SoftValidations do end end end + + describe "#person_2_student_not_child?" do + it "returns false if age is not given" do + record.age2 = nil + record.relat2 = "P" + record.ecstat2 = 7 + + expect(record).not_to be_person_2_student_not_child + end + + it "returns false if retaltionship is not given" do + record.age2 = 17 + record.relat2 = nil + record.ecstat2 = 7 + + expect(record).not_to be_person_2_student_not_child + end + + it "returns false if economic status is not given" do + record.age2 = 17 + record.relat2 = "P" + record.ecstat2 = nil + + expect(record).not_to be_person_2_student_not_child + end + + it "returns true if it's a student aged 16-19 and not a child" do + record.age2 = 17 + record.relat2 = "P" + record.ecstat2 = 7 + + expect(record).to be_person_2_student_not_child + end + end + + describe "#person_3_student_not_child?" do + it "returns false if age is not given" do + record.age3 = nil + record.relat3 = "P" + record.ecstat3 = 7 + + expect(record).not_to be_person_3_student_not_child + end + + it "returns false if retaltionship is not given" do + record.age3 = 17 + record.relat3 = nil + record.ecstat3 = 7 + + expect(record).not_to be_person_3_student_not_child + end + + it "returns false if economic status is not given" do + record.age3 = 17 + record.relat3 = "P" + record.ecstat3 = nil + + expect(record).not_to be_person_3_student_not_child + end + + it "returns true if it's a student aged 16-19 and not a child" do + record.age3 = 17 + record.relat3 = "P" + record.ecstat3 = 7 + + expect(record).to be_person_3_student_not_child + end + end end