Browse Source

validate_other_household_members refactor

Co-authored-by: Daniel Baark <baarkerlounger@users.noreply.github.com>
pull/74/head
Matthew Phelan 3 years ago
parent
commit
c3e1f07797
  1. 87
      app/models/case_log.rb
  2. 74
      app/validations/household_validations.rb
  3. 6
      spec/models/case_log_spec.rb

87
app/models/case_log.rb

@ -5,90 +5,13 @@ class CaseLogValidator < ActiveModel::Validator
include FinancialValidations
include TenancyValidations
def validate_other_household_members(record)
index = 0
number_of_other_members = record.household_number_of_other_members
partner = false
while index < number_of_other_members
member_number = index+2
relationship = record["person_#{member_number}_relationship"]
age = record["person_#{member_number}_age"]
gender = record["person_#{member_number}_gender"]
economic_status = record["person_#{member_number}_economic_status"]
binding.pry
if relationship || age || gender || economic_status
if relationship.nil? || age.nil? || gender.nil? || economic_status.nil?
record.errors.add "person_#{member_number}_age", "If any of the person is filled out it must all be filled"
end
end
if age<1 || age>120
record.errors.add "person_#{member_number}_age", "Tenant #{member_number} age must be between 1 and 120 (i.e. infants must be entered as 1)"
end
if age>70 && economic_status != "Retired"
record.errors.add "person_#{member_number}_economic_status", "Tenant #{member_number} must be retired if over 70"
end
if gender=="Male" && economic_status == "Retired" && age<65
record.errors.add "person_#{member_number}_age", "Male tenant who is retired must be 65 or over"
end
if gender=="Female" && economic_status == "Retired" && age<60
record.errors.add "person_#{member_number}_age", "Female tenant who is retired must be 60 or over"
end
if age>70 && economic_status != "Retired"
record.errors.add "person_#{member_number}_economic_status", "Tenant #{member_number} must be retired if over 70"
end
if age<16
if relationship != "Child - includes young adult and grown-up"
record.errors.add "person_#{member_number}_relationship", "Tenant #{member_number}'s relationship to tenant 1 must be Child if their age is under 16"
end
if economic_status != "Child under 16"
record.errors.add "person_#{member_number}_economic_status", "Tenant #{member_number} economic status must be Child under 16 if their age is under 16"
end
end
if relationship == "Partner"
if partner
record.errors.add "person_#{member_number}_relationship", "Tenant can not have multiple partners"
elsif age<16 || economic_status == "Child under 16"
record.errors.add "person_#{member_number}_relationship", "Tenant can not be tenant 1's partner if they are under 16"
else
partner = true
end
end
if relationship == "Child - includes young adult and grown-up"
if economic_status!="Unable to work because of long term sick or disability" || economic_status!="Other" || economic_status!="Prefer not to say"
record.errors.add "person_#{member_number}_economic_status", "This is not a valid economic status for a child"
end
if age>=16 && age<=19
if economic_status != "Full-time student" || economic_status != "Prefer not to say"
record.errors.add "person_#{member_number}_economic_status", "If relationship is child and age is between 16 and 19 - tenant #{member_number} must be a full time student or prefer not to say."
end
end
end
index = index+1
end
end
def validate(record)
# If we've come from the form UI we only want to validate the specific fields
# that have just been submitted. If we're submitting a log via API or Bulk Upload
# we want to validate all data fields.
question_to_validate = options[:previous_page]
if question_to_validate
if respond_to?("validate_#{question_to_validate}")
public_send("validate_#{question_to_validate}", record)
end
public_send("validate_#{question_to_validate}", record) if respond_to?("validate_#{question_to_validate}")
else
validation_methods = public_methods.select { |method| method.starts_with?("validate_") }
validation_methods.each { |meth| public_send(meth, record) }
@ -209,6 +132,14 @@ private
dynamically_not_required << "net_income"
dynamically_not_required << "net_income_frequency"
end
start_range = (household_number_of_other_members || 0) + 2
(start_range..8).each do |n|
dynamically_not_required << "person_#{n}_age"
dynamically_not_required << "person_#{n}_gender"
dynamically_not_required << "person_#{n}_relationship"
dynamically_not_required << "person_#{n}_economic_status"
end
required.delete_if { |key, _value| dynamically_not_required.include?(key) }
end

74
app/validations/household_validations.rb

@ -1,11 +1,5 @@
module HouseholdValidations
# Validations methods need to be called 'validate_' to run on model save
def validate_person_1_age(record)
if record.person_1_age && !/^[1-9][0-9]?$|^120$/.match?(record.person_1_age.to_s)
record.errors.add :person_1_age, "Tenant age must be between 0 and 120"
end
end
def validate_reasonable_preference(record)
if record.homelessness == "No" && record.reasonable_preference == "Yes"
record.errors.add :reasonable_preference, "Can not be Yes if Not Homeless immediately prior to this letting has been selected"
@ -56,6 +50,16 @@ module HouseholdValidations
end
end
def validate_other_household_members(record)
(1..8).each do |n|
validate_person_age(record, n)
validate_person_age_matches_economic_status(record, n)
validate_person_age_matches_relationship(record, n) if n > 1
validate_person_age_and_gender_match_economic_status(record, n)
end
validate_partner_count(record)
end
private
def women_of_child_bearing_age_in_household(record)
@ -65,4 +69,62 @@ private
record["person_#{n}_gender"] == "Female" && record["person_#{n}_age"] >= 16 && record["person_#{n}_age"] <= 50
end
end
def validate_person_age(record, person_num)
age = record.public_send("person_#{person_num}_age")
return unless age
if !age.is_a?(Integer) || age < 1 || age > 120
record.errors.add "person_#{person_num}_age".to_sym, "Tenant age must be an integer between 0 and 120"
end
end
def validate_person_age_matches_economic_status(record, person_num)
age = record.public_send("person_#{person_num}_age")
economic_status = record.public_send("person_#{person_num}_economic_status")
return unless age && economic_status
if age > 70 && economic_status != "Retired"
record.errors.add "person_#{person_num}_economic_status", "Tenant #{person_num} must be retired if over 70"
end
if age < 16 && economic_status != "Child under 16"
record.errors.add "person_#{person_num}_economic_status", "Tenant #{person_num} economic status must be Child under 16 if their age is under 16"
end
if age >= 16 && age <= 19 && (economic_status != "Full-time student" || economic_status != "Prefer not to say")
record.errors.add "person_#{person_num}_economic_status", "If age is between 16 and 19 - tenant #{person_num} must be a full time student or prefer not to say."
end
end
def validate_person_age_matches_relationship(record, person_num)
age = record.public_send("person_#{person_num}_age")
relationship = record.public_send("person_#{person_num}_relationship")
return unless age && relationship
if age < 16 && relationship != "Child - includes young adult and grown-up"
record.errors.add "person_#{person_num}_relationship", "Tenant #{person_num}'s relationship to tenant 1 must be Child if their age is under 16"
end
end
def validate_person_age_and_gender_match_economic_status(record, person_num)
age = record.public_send("person_#{person_num}_age")
gender = record.public_send("person_#{person_num}_gender")
economic_status = record.public_send("person_#{person_num}_economic_status")
return unless age && economic_status && gender
if gender == "Male" && economic_status == "Retired" && age < 65
record.errors.add "person_#{person_num}_age", "Male tenant who is retired must be 65 or over"
end
if gender == "Female" && economic_status == "Retired" && age < 60
record.errors.add "person_#{person_num}_age", "Female tenant who is retired must be 60 or over"
end
end
def validate_partner_count(record)
# TODO probably need to keep track of which specific field is wrong so we can highlight it in the UI
partner_count = (2..8).map { |n| record.public_send("person_#{n}_relationship") }.uniq.count
if partner_count > 1
record.errors.add :base, "Number of partners cannot be greater than 1"
end
end
end

6
spec/models/case_log_spec.rb

@ -206,6 +206,12 @@ RSpec.describe Form, type: :model do
end
end
context "household_member_validations" do
# it "validate that persons aged under 16 must have relationship Child" do
# expect { CaseLog.create!(person_2_age: 14, person_2_relationship: "Partner") }.to raise_error(ActiveRecord::RecordInvalid)
# end
end
context "other tenancy type validation" do
it "must be provided if tenancy type was given as other" do
expect {

Loading…
Cancel
Save