From bea7c54e5a0922d551bf92e03f41cecfeef642e7 Mon Sep 17 00:00:00 2001 From: magicmilo Date: Wed, 27 Oct 2021 11:23:46 +0100 Subject: [PATCH] inital --- app/models/case_log.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 0b347a784..09b98f381 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -61,6 +61,23 @@ class CaseLogValidator < ActiveModel::Validator end end + def validate_shared_housing_rooms(record) + number_of_tenants = people_in_household(record) + if record.property_unit_type == "Bed-sit" && record.property_number_of_bedrooms != 1 + record.errors.add :property_unit_type, "A bedsit can only have one bedroom" + end + + if people_in_household(record) > 1 + if record.property_unit_type.include? == "Shared" && (record.property_number_of_bedrooms == 0 && record.property_number_of_bedrooms > 7) + record.errors.add :property_unit_type, "A shared house must have 1 to 7 bedrooms" + end + else + if record.property_unit_type.include? == "Shared" && (record.property_number_of_bedrooms == 0 && record.property_number_of_bedrooms > 3) + record.errors.add :property_unit_type, "A shared house with one tenant must have 1 to 3 bedrooms" + end + 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 @@ -87,6 +104,14 @@ private record["person_#{n}_gender"] == "Female" && record["person_#{n}_age"] >= 16 && record["person_#{n}_age"] <= 50 end end + + def people_in_household(record) + count = 0 + (1..8).any? do |n| + next if record["person_#{n}_gender"].nil? || record["person_#{n}_age"].nil? + count += 1 + end + end end class CaseLog < ApplicationRecord