From bea7c54e5a0922d551bf92e03f41cecfeef642e7 Mon Sep 17 00:00:00 2001 From: magicmilo Date: Wed, 27 Oct 2021 11:23:46 +0100 Subject: [PATCH 1/7] 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 From 1cf119709660fc6059dffc1a22038ef6c266090e Mon Sep 17 00:00:00 2001 From: magicmilo Date: Wed, 27 Oct 2021 14:07:18 +0100 Subject: [PATCH 2/7] tests --- app/models/case_log.rb | 1 + spec/models/case_log_spec.rb | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 09b98f381..f4fa0b9cf 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -111,6 +111,7 @@ private next if record["person_#{n}_gender"].nil? || record["person_#{n}_age"].nil? count += 1 end + return count end end diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index c9339d719..111a69943 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -96,6 +96,36 @@ RSpec.describe Form, type: :model do }.to raise_error(ActiveRecord::RecordInvalid) end end + + context "Shared accomodation bedrooms validation" do + it "you must have more than zero bedrooms" do + expect { + CaseLog.create!(property_unit_type: "Shared house", + property_number_of_bedrooms: 0) + }.to raise_error(ActiveRecord::RecordInvalid) + end + + it "you must answer less than 8 bedrooms" do + expect { + CaseLog.create!(property_unit_type: "Shared bungalow", + property_number_of_bedrooms: 8) + }.to raise_error(ActiveRecord::RecordInvalid) + end + + it "A bedsit must only have one room" do + expect { + CaseLog.create!(property_unit_type: "Bed-sit", + property_number_of_bedrooms: 2) + }.to raise_error(ActiveRecord::RecordInvalid) + end + + it "A bedsit must only have one room" do + expect { + CaseLog.create!(property_unit_type: "Bed-sit", + property_number_of_bedrooms: 0) + }.to raise_error(ActiveRecord::RecordInvalid) + end + end end describe "status" do From 2367ad16d505fc86ef6ba0b81d0f64b4f7352bd0 Mon Sep 17 00:00:00 2001 From: magicmilo Date: Thu, 28 Oct 2021 10:41:54 +0100 Subject: [PATCH 3/7] tests and tweaks --- app/models/case_log.rb | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index f4fa0b9cf..d77e59c7a 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -63,17 +63,20 @@ class CaseLogValidator < ActiveModel::Validator 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" + unless record.property_unit_type.nil? + 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 - 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" + + if people_in_household(record) > 1 + if record.property_unit_type.include?("Shared") && (record.property_number_of_bedrooms.to_i == 0 || record.property_number_of_bedrooms.to_i > 7) + record.errors.add :property_unit_type, "A shared house must have 1 to 7 bedrooms" + end + end + + if record.property_unit_type.include?("Shared") && (record.property_number_of_bedrooms.to_i == 0 || record.property_number_of_bedrooms.to_i > 3) + record.errors.add :property_unit_type, "A shared house with less than two tenants must have 1 to 3 bedrooms" end end end From ee2323a805ec2e74af3899be72b81b207f647e98 Mon Sep 17 00:00:00 2001 From: magicmilo Date: Fri, 29 Oct 2021 13:24:53 +0100 Subject: [PATCH 4/7] Switch to household number of other members --- app/models/case_log.rb | 19 +++++-------------- spec/models/case_log_spec.rb | 11 ++++++++++- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index fde1c3101..2f411021f 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -106,16 +106,16 @@ class CaseLogValidator < ActiveModel::Validator end def validate_shared_housing_rooms(record) - number_of_tenants = people_in_household(record) - unless record.property_unit_type.nil? 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.to_i == 0 || record.property_number_of_bedrooms.to_i > 7) - record.errors.add :property_unit_type, "A shared house must have 1 to 7 bedrooms" + unless record.household_number_of_other_members.nil? + if record.household_number_of_other_members > 0 + if record.property_unit_type.include?("Shared") && (record.property_number_of_bedrooms.to_i == 0 || record.property_number_of_bedrooms.to_i > 7) + record.errors.add :property_unit_type, "A shared house must have 1 to 7 bedrooms" + end end end @@ -163,15 +163,6 @@ 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 - return count - end end class CaseLog < ApplicationRecord diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 317794b60..df480af78 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -108,7 +108,16 @@ RSpec.describe Form, type: :model do it "you must answer less than 8 bedrooms" do expect { CaseLog.create!(property_unit_type: "Shared bungalow", - property_number_of_bedrooms: 8) + property_number_of_bedrooms: 8, + household_number_of_other_members: 1) + }.to raise_error(ActiveRecord::RecordInvalid) + end + + it "you must answer less than 8 bedrooms" do + expect { + CaseLog.create!(property_unit_type: "Shared bungalow", + property_number_of_bedrooms: 4, + household_number_of_other_members: 0) }.to raise_error(ActiveRecord::RecordInvalid) end From 31b594d51c873d93834b2162e8a2ba6793f4531b Mon Sep 17 00:00:00 2001 From: magicmilo Date: Fri, 29 Oct 2021 13:37:27 +0100 Subject: [PATCH 5/7] switched to between for range --- app/models/case_log.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index c0ab8bbd5..da8110796 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -119,13 +119,13 @@ class CaseLogValidator < ActiveModel::Validator unless record.household_number_of_other_members.nil? if record.household_number_of_other_members > 0 - if record.property_unit_type.include?("Shared") && (record.property_number_of_bedrooms.to_i == 0 || record.property_number_of_bedrooms.to_i > 7) + if record.property_unit_type.include?("Shared") && !record.property_number_of_bedrooms.to_i.between?(1, 7) record.errors.add :property_unit_type, "A shared house must have 1 to 7 bedrooms" end end end - if record.property_unit_type.include?("Shared") && (record.property_number_of_bedrooms.to_i == 0 || record.property_number_of_bedrooms.to_i > 3) + if record.property_unit_type.include?("Shared") && !record.property_number_of_bedrooms.to_i.between?(1, 3) record.errors.add :property_unit_type, "A shared house with less than two tenants must have 1 to 3 bedrooms" end end From a83a0c53fd83c2fd71ec55a7c301d0d9de3d16f2 Mon Sep 17 00:00:00 2001 From: magicmilo Date: Mon, 1 Nov 2021 10:01:53 +0000 Subject: [PATCH 6/7] fix merge and move validation --- app/models/case_log.rb | 77 ------------------------ app/validations/household_validations.rb | 20 ++++++ 2 files changed, 20 insertions(+), 77 deletions(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index da8110796..2761e2223 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -2,53 +2,12 @@ class CaseLogValidator < ActiveModel::Validator # Methods to be used on save and continue need to be named 'validate_' # followed by field name this is how the metaprogramming of the method # name being call in the validate method works. - - 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_property_number_of_times_relet(record) if record.property_number_of_times_relet && !/^[1-9]$|^0[1-9]$|^1[0-9]$|^20$/.match?(record.property_number_of_times_relet.to_s) record.errors.add :property_number_of_times_relet, "Must be between 0 and 20" 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" - elsif record.reasonable_preference == "Yes" - if !record.reasonable_preference_reason_homeless && !record.reasonable_preference_reason_unsatisfactory_housing && !record.reasonable_preference_reason_medical_grounds && !record.reasonable_preference_reason_avoid_hardship && !record.reasonable_preference_reason_do_not_know - record.errors.add :reasonable_preference_reason, "If reasonable preference is Yes, a reason must be given" - end - elsif record.reasonable_preference == "No" - if record.reasonable_preference_reason_homeless || record.reasonable_preference_reason_unsatisfactory_housing || record.reasonable_preference_reason_medical_grounds || record.reasonable_preference_reason_avoid_hardship || record.reasonable_preference_reason_do_not_know - record.errors.add :reasonable_preference_reason, "If reasonable preference is No, no reasons should be given" - end - end - end - - def validate_other_reason_for_leaving_last_settled_home(record) - validate_other_field(record, "reason_for_leaving_last_settled_home", "other_reason_for_leaving_last_settled_home") - end - - def validate_reason_for_leaving_last_settled_home(record) - if record.reason_for_leaving_last_settled_home == "Do not know" && record.benefit_cap_spare_room_subsidy != "Do not know" - record.errors.add :benefit_cap_spare_room_subsidy, "must be do not know if tenant’s main reason for leaving is do not know" - end - end - - def validate_armed_forces_injured(record) - if (record.armed_forces == "Yes - a regular" || record.armed_forces == "Yes - a reserve") && record.armed_forces_injured.blank? - record.errors.add :armed_forces_injured, "You must answer the armed forces injury question if the tenant has served in the armed forces" - end - - if (record.armed_forces == "No" || record.armed_forces == "Prefer not to say") && record.armed_forces_injured.present? - record.errors.add :armed_forces_injured, "You must not answer the armed forces injury question if the tenant has not served in the armed forces or prefer not to say was chosen" - end - end - def validate_outstanding_rent_amount(record) if record.outstanding_rent_or_charges == "Yes" && record.outstanding_amount.blank? record.errors.add :outstanding_amount, "You must answer the oustanding amout question if you have outstanding rent or charges." @@ -70,22 +29,6 @@ class CaseLogValidator < ActiveModel::Validator end end end - - def validate_armed_forces_active_response(record) - if record.armed_forces == "Yes - a regular" && record.armed_forces_active.blank? - record.errors.add :armed_forces_active, "You must answer the armed forces active question if the tenant has served as a regular in the armed forces" - end - - if record.armed_forces != "Yes - a regular" && record.armed_forces_active.present? - record.errors.add :armed_forces_active, "You must not answer the armed forces active question if the tenant has not served as a regular in the armed forces" - end - end - - def validate_household_pregnancy(record) - if (record.pregnancy == "Yes" || record.pregnancy == "Prefer not to say") && !women_of_child_bearing_age_in_household(record) - record.errors.add :pregnancy, "You must answer no as there are no female tenants aged 16-50 in the property" - end - end def validate_fixed_term_tenancy(record) is_present = record.fixed_term_tenancy.present? @@ -111,26 +54,6 @@ class CaseLogValidator < ActiveModel::Validator include FinancialValidations include TenancyValidations - def validate_shared_housing_rooms(record) - unless record.property_unit_type.nil? - 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 - - unless record.household_number_of_other_members.nil? - if record.household_number_of_other_members > 0 - if record.property_unit_type.include?("Shared") && !record.property_number_of_bedrooms.to_i.between?(1, 7) - record.errors.add :property_unit_type, "A shared house must have 1 to 7 bedrooms" - end - end - end - - if record.property_unit_type.include?("Shared") && !record.property_number_of_bedrooms.to_i.between?(1, 3) - record.errors.add :property_unit_type, "A shared house with less than two tenants 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 diff --git a/app/validations/household_validations.rb b/app/validations/household_validations.rb index 8ff4e684a..769266748 100644 --- a/app/validations/household_validations.rb +++ b/app/validations/household_validations.rb @@ -56,6 +56,26 @@ module HouseholdValidations end end + def validate_shared_housing_rooms(record) + unless record.property_unit_type.nil? + 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 + + unless record.household_number_of_other_members.nil? + if record.household_number_of_other_members > 0 + if record.property_unit_type.include?("Shared") && !record.property_number_of_bedrooms.to_i.between?(1, 7) + record.errors.add :property_unit_type, "A shared house must have 1 to 7 bedrooms" + end + end + end + + if record.property_unit_type.include?("Shared") && !record.property_number_of_bedrooms.to_i.between?(1, 3) + record.errors.add :property_unit_type, "A shared house with less than two tenants must have 1 to 3 bedrooms" + end + end + end + private def women_of_child_bearing_age_in_household(record) From 6ca1fa1bc263dca0797576d62fa6f6f58b057fbc Mon Sep 17 00:00:00 2001 From: magicmilo Date: Mon, 1 Nov 2021 10:16:01 +0000 Subject: [PATCH 7/7] fix merge --- app/models/case_log.rb | 49 ------------------------------------------ 1 file changed, 49 deletions(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 2761e2223..ffe5e5397 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -1,53 +1,4 @@ class CaseLogValidator < ActiveModel::Validator - # Methods to be used on save and continue need to be named 'validate_' - # followed by field name this is how the metaprogramming of the method - # name being call in the validate method works. - def validate_property_number_of_times_relet(record) - if record.property_number_of_times_relet && !/^[1-9]$|^0[1-9]$|^1[0-9]$|^20$/.match?(record.property_number_of_times_relet.to_s) - record.errors.add :property_number_of_times_relet, "Must be between 0 and 20" - end - end - - def validate_outstanding_rent_amount(record) - if record.outstanding_rent_or_charges == "Yes" && record.outstanding_amount.blank? - record.errors.add :outstanding_amount, "You must answer the oustanding amout question if you have outstanding rent or charges." - end - if record.outstanding_rent_or_charges == "No" && record.outstanding_amount.present? - record.errors.add :outstanding_amount, "You must not answer the oustanding amout question if you don't have outstanding rent or charges." - end - end - - EMPLOYED_STATUSES = ["Full-time - 30 hours or more", "Part-time - Less than 30 hours"].freeze - def validate_net_income_uc_proportion(record) - (1..8).any? do |n| - economic_status = record["person_#{n}_economic_status"] - is_employed = EMPLOYED_STATUSES.include?(economic_status) - relationship = record["person_#{n}_relationship"] - is_partner_or_main = relationship == "Partner" || (relationship.nil? && economic_status.present?) - if is_employed && is_partner_or_main && record.net_income_uc_proportion == "All" - record.errors.add :net_income_uc_proportion, "income is from Universal Credit, state pensions or benefits cannot be All if the tenant or the partner works part or full time" - end - end - end - - def validate_fixed_term_tenancy(record) - is_present = record.fixed_term_tenancy.present? - is_in_range = record.fixed_term_tenancy.to_i.between?(2, 99) - is_secure = record.tenancy_type == "Fixed term – Secure" - is_ast = record.tenancy_type == "Fixed term – Assured Shorthold Tenancy (AST)" - conditions = [ - { condition: !(is_secure || is_ast) && is_present, error: "You must only answer the fixed term tenancy length question if the tenancy type is fixed term" }, - { condition: is_ast && !is_in_range, error: "Fixed term – Assured Shorthold Tenancy (AST) should be between 2 and 99 years" }, - { condition: is_secure && (!is_in_range && is_present), error: "Fixed term – Secure should be between 2 and 99 years or not specified" }, - ] - - conditions.each { |condition| condition[:condition] ? (record.errors.add :fixed_term_tenancy, condition[:error]) : nil } - end - - def validate_other_tenancy_type(record) - validate_other_field(record, "tenancy_type", "other_tenancy_type") - end - # Validations methods need to be called 'validate_' to run on model save include HouseholdValidations include PropertyValidations