Browse Source

CLDC-3234: Prevent errors from nil hhmemb (#2301)

* CLDC-3234: Handle nil hhmemb gracefully in soft validations

* CLDC-3234: Don't blank hhmemb when blanking invalid non setup fields
pull/2329/head^2
Rachael Booth 9 months ago committed by GitHub
parent
commit
e19f1e5b0d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 3
      app/models/log.rb
  2. 16
      app/models/validations/soft_validations.rb
  3. 11
      spec/models/log_spec.rb

3
app/models/log.rb

@ -182,12 +182,13 @@ class Log < ApplicationRecord
def blank_invalid_non_setup_fields! def blank_invalid_non_setup_fields!
setup_ids = form.setup_sections.flat_map(&:subsections).flat_map(&:questions).map(&:id) setup_ids = form.setup_sections.flat_map(&:subsections).flat_map(&:questions).map(&:id)
fields_to_keep = setup_ids + %w[hhmemb]
2.times do 2.times do
next if valid? next if valid?
errors.each do |error| errors.each do |error|
next if setup_ids.include?(error.attribute.to_s) next if fields_to_keep.include?(error.attribute.to_s)
question = form.questions.find { |q| q.id == error.attribute.to_s } question = form.questions.find { |q| q.id == error.attribute.to_s }
if question&.type == "checkbox" if question&.type == "checkbox"

16
app/models/validations/soft_validations.rb

@ -76,13 +76,17 @@ module Validations::SoftValidations
end end
def all_tenants_age_and_gender_information_completed? def all_tenants_age_and_gender_information_completed?
(1..hhmemb).all? do |n| person_count = hhmemb || 8
(1..person_count).all? do |n|
public_send("sex#{n}").present? && public_send("age#{n}").present? && details_known_or_lead_tenant?(n) && public_send("age#{n}_known").present? && public_send("age#{n}_known").zero? public_send("sex#{n}").present? && public_send("age#{n}").present? && details_known_or_lead_tenant?(n) && public_send("age#{n}_known").present? && public_send("age#{n}_known").zero?
end end
end end
def all_tenants_gender_information_completed? def all_tenants_gender_information_completed?
(1..hhmemb).all? do |n| person_count = hhmemb || 8
(1..person_count).all? do |n|
public_send("sex#{n}").present? && details_known_or_lead_tenant?(n) public_send("sex#{n}").present? && details_known_or_lead_tenant?(n)
end end
end end
@ -207,13 +211,17 @@ private
end end
def females_in_age_range(min, max) def females_in_age_range(min, max)
(1..hhmemb).any? do |n| person_count = hhmemb || 8
(1..person_count).any? do |n|
public_send("sex#{n}") == "F" && public_send("age#{n}").present? && public_send("age#{n}").between?(min, max) public_send("sex#{n}") == "F" && public_send("age#{n}").present? && public_send("age#{n}").between?(min, max)
end end
end end
def females_in_the_household? def females_in_the_household?
(1..hhmemb).any? do |n| person_count = hhmemb || 8
(1..person_count).any? do |n|
public_send("sex#{n}") == "F" || public_send("sex#{n}").nil? public_send("sex#{n}") == "F" || public_send("sex#{n}").nil?
end end
end end

11
spec/models/log_spec.rb

@ -76,5 +76,16 @@ RSpec.describe Log, type: :model do
expect(model.previous_la_known).to be_nil expect(model.previous_la_known).to be_nil
end end
end end
context "when hhmemb is invalid for a lettings log" do
subject(:model) { build(:lettings_log, :setup_completed, hhmemb: 1, joint: 1) }
it "does not blank it" do
model.valid?
model.blank_invalid_non_setup_fields!
expect(model.hhmemb).to be(1)
expect(model.joint).to be_nil
end
end
end end
end end

Loading…
Cancel
Save