diff --git a/app/models/log.rb b/app/models/log.rb index 63612326a..ea8a637af 100644 --- a/app/models/log.rb +++ b/app/models/log.rb @@ -52,6 +52,16 @@ class Log < ApplicationRecord form.end_date > Time.zone.today end + def blank_invalid_non_setup_fields! + setup_ids = form.setup_sections.flat_map(&:subsections).flat_map(&:questions).map(&:id) + + errors.each do |error| + next if setup_ids.include?(error.attribute.to_s) + + public_send("#{error.attribute}=", nil) + end + end + private def update_status! diff --git a/spec/models/lettings_log_spec.rb b/spec/models/lettings_log_spec.rb index 13f8eafd2..8a4325880 100644 --- a/spec/models/lettings_log_spec.rb +++ b/spec/models/lettings_log_spec.rb @@ -2536,4 +2536,24 @@ RSpec.describe LettingsLog do end end end + + describe "#blank_invalid_non_setup_fields!" do + context "when a setup field is invalid" do + subject(:model) { described_class.new(needstype: 404) } + + it "does not blank it" do + model.valid? + expect { model.blank_invalid_non_setup_fields! }.not_to change(model, :needstype) + end + end + + context "when a non setup field is invalid" do + subject(:model) { described_class.new(beds: 404) } + + it "blanks it" do + model.valid? + expect { model.blank_invalid_non_setup_fields! }.to change(model, :beds) + end + end + end end