diff --git a/app/models/validations/soft_validations.rb b/app/models/validations/soft_validations.rb index d63d6394c..8b51767dc 100644 --- a/app/models/validations/soft_validations.rb +++ b/app/models/validations/soft_validations.rb @@ -48,24 +48,40 @@ module Validations::SoftValidations end end - def no_females_in_the_household? - (1..8).none? do |n| - public_send("sex#{n}") == "F" - end && preg_occ == 1 + def no_females_in_a_pregnant_household? + !females_in_the_household? && all_tenants_age_and_gender_information_completed? && preg_occ == 1 end def female_in_pregnant_household_in_soft_validation_range? - (females_in_age_range(11, 15) || females_in_age_range(51, 65)) && !females_in_age_range(16, 50) + all_tenants_age_and_gender_information_completed? && (females_in_age_range(11, 15) || females_in_age_range(51, 65)) && !females_in_age_range(16, 50) && preg_occ == 1 + end + + def all_tenants_age_and_gender_information_completed? + (1..hhmemb).all? do |n| + public_send("sex#{n}").present? && public_send("age#{n}").present? && details_known_or_lead_tenant?(n) && public_send("age#{n}_known") + end end private + def details_known_or_lead_tenant?(tenant_number) + return true if tenant_number == 1 + + public_send("details_known_#{tenant_number}").zero? + end + def females_in_age_range(min, max) - (1..8).any? do |n| + (1..hhmemb).any? do |n| public_send("sex#{n}") == "F" && public_send("age#{n}").present? && public_send("age#{n}").between?(min, max) end end + def females_in_the_household? + (1..hhmemb).any? do |n| + public_send("sex#{n}") == "F" || public_send("sex#{n}").nil? + end + end + def tenant_is_retired?(economic_status) economic_status == 5 end diff --git a/spec/models/validations/soft_validations_spec.rb b/spec/models/validations/soft_validations_spec.rb index 92f03aadf..ff432ecb9 100644 --- a/spec/models/validations/soft_validations_spec.rb +++ b/spec/models/validations/soft_validations_spec.rb @@ -168,29 +168,37 @@ RSpec.describe Validations::SoftValidations do describe "pregnancy soft validations" do context "when there are no female tenants" do it "shows the interruption screen" do - record.update!(age1: 43, sex1: "M", preg_occ: 1) - expect(record.no_females_in_the_household?).to be true + record.update!(age1: 43, sex1: "M", preg_occ: 1, hhmemb: 1, age1_known: 0) + expect(record.no_females_in_a_pregnant_household?).to be true end end context "when female tenants are in 11-16 age range" do it "shows the interruption screen" do - record.update!(age3: 14, sex3: "F", preg_occ: 1) + record.update!(age2: 14, sex2: "F", preg_occ: 1, hhmemb: 2, details_known_2: 0, age2_known: 0, age1: 18, sex1: "M", age1_known: 0) expect(record.female_in_pregnant_household_in_soft_validation_range?).to be true end end context "when female tenants are in 50-65 age range" do it "shows the interruption screen" do - record.update!(age1: 54, sex1: "F", preg_occ: 1) + record.update!(age1: 54, sex1: "F", preg_occ: 1, hhmemb: 1, age1_known: 0) expect(record.female_in_pregnant_household_in_soft_validation_range?).to be true end end context "when female tenants are outside or soft validation ranges" do it "does not show the interruption screen" do - record.update!(age1: 44, sex1: "F", preg_occ: 1) + record.update!(age1: 44, sex1: "F", preg_occ: 1, hhmemb: 1) + expect(record.no_females_in_a_pregnant_household?).to be false expect(record.female_in_pregnant_household_in_soft_validation_range?).to be false + end + end + + context "when the information about the tenants is not given" do + it "does not show the interruption screen" do + record.update!(preg_occ: 1, hhmemb: 2) + expect(record.no_females_in_a_pregnant_household?).to be false expect(record.female_in_pregnant_household_in_soft_validation_range?).to be false end end