Browse Source

Switch to household number of other members

pull/70/head
magicmilo 3 years ago
parent
commit
ee2323a805
  1. 15
      app/models/case_log.rb
  2. 11
      spec/models/case_log_spec.rb

15
app/models/case_log.rb

@ -106,18 +106,18 @@ class CaseLogValidator < ActiveModel::Validator
end end
def validate_shared_housing_rooms(record) def validate_shared_housing_rooms(record)
number_of_tenants = people_in_household(record)
unless record.property_unit_type.nil? unless record.property_unit_type.nil?
if record.property_unit_type == "Bed-sit" && record.property_number_of_bedrooms != 1 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" record.errors.add :property_unit_type, "A bedsit can only have one bedroom"
end end
if people_in_household(record) > 1 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 == 0 || record.property_number_of_bedrooms.to_i > 7)
record.errors.add :property_unit_type, "A shared house must have 1 to 7 bedrooms" record.errors.add :property_unit_type, "A shared house must have 1 to 7 bedrooms"
end end
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 == 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" record.errors.add :property_unit_type, "A shared house with less than two tenants must have 1 to 3 bedrooms"
@ -163,15 +163,6 @@ private
record["person_#{n}_gender"] == "Female" && record["person_#{n}_age"] >= 16 && record["person_#{n}_age"] <= 50 record["person_#{n}_gender"] == "Female" && record["person_#{n}_age"] >= 16 && record["person_#{n}_age"] <= 50
end end
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 end
class CaseLog < ApplicationRecord class CaseLog < ApplicationRecord

11
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 it "you must answer less than 8 bedrooms" do
expect { expect {
CaseLog.create!(property_unit_type: "Shared bungalow", 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) }.to raise_error(ActiveRecord::RecordInvalid)
end end

Loading…
Cancel
Save