From 5dd4faae287786eb2f60fef11be5c827c165355d Mon Sep 17 00:00:00 2001 From: Rachael Booth Date: Thu, 23 Jan 2025 09:55:43 +0000 Subject: [PATCH] CLDC-3850: Clear postcode known when clearing postcode full --- app/models/log.rb | 7 +++++++ spec/models/log_spec.rb | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/app/models/log.rb b/app/models/log.rb index 66e996892..d55289997 100644 --- a/app/models/log.rb +++ b/app/models/log.rb @@ -248,6 +248,11 @@ class Log < ApplicationRecord end def blank_compound_invalid_non_setup_fields! + if errors.attribute_names.include? :postcode_full + self.postcode_known = nil if lettings? + self.pcodenk = nil if sales? + end + self.ppcodenk = nil if errors.attribute_names.include? :ppostcode_full self.previous_la_known = nil if errors.attribute_names.include? :prevloc @@ -258,6 +263,8 @@ class Log < ApplicationRecord self.address_line2 = nil self.town_or_city = nil self.postcode_full = nil + self.postcode_known = nil if lettings? + self.pcodenk = nil if sales? self.county = nil process_postcode_changes! end diff --git a/spec/models/log_spec.rb b/spec/models/log_spec.rb index c5f133730..c5f9075e4 100644 --- a/spec/models/log_spec.rb +++ b/spec/models/log_spec.rb @@ -101,5 +101,29 @@ RSpec.describe Log, type: :model do expect(model.joint).to be_nil end end + + context "when postcode_full is invalid" do + context "for a lettings log" do + subject(:model) { build_stubbed(:lettings_log, :setup_completed, postcode_full: "not a postcode", postcode_known: 1) } + + it "blanks it and postcode_known" do + model.valid? + model.blank_invalid_non_setup_fields! + expect(model.postcode_full).to be_nil + expect(model.postcode_known).to be_nil + end + end + + context "for a sales log" do + subject(:model) { build_stubbed(:sales_log, :discounted_ownership_setup_complete, postcode_full: "not a postcode", pcodenk: 0) } + + it "blanks it and pcodenk" do + model.valid? + model.blank_invalid_non_setup_fields! + expect(model.postcode_full).to be_nil + expect(model.pcodenk).to be_nil + end + end + end end end