diff --git a/app/models/case_log.rb b/app/models/case_log.rb index a8250056d..fef4f2911 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -23,7 +23,7 @@ class CaseLog < ApplicationRecord validates_with CaseLogValidator before_validation :recalculate_start_year!, if: :startdate_changed? - before_validation :reset_scheme_location!, if: :scheme_changed? + before_validation :reset_scheme_location!, if: :scheme_changed?, unless: :location_changed? before_validation :process_postcode_changes!, if: :postcode_full_changed? before_validation :process_previous_postcode_changes!, if: :ppostcode_full_changed? before_validation :reset_invalidated_dependent_fields! @@ -696,8 +696,4 @@ private def upcase_and_remove_whitespace(string) string.present? ? string.upcase.gsub(/\s+/, "") : string end - - def reset_scheme_location! - self.location = nil - end end diff --git a/app/models/derived_variables/case_log_variables.rb b/app/models/derived_variables/case_log_variables.rb index 312c5a719..31209dd50 100644 --- a/app/models/derived_variables/case_log_variables.rb +++ b/app/models/derived_variables/case_log_variables.rb @@ -68,10 +68,7 @@ module DerivedVariables::CaseLogVariables self.new_old = new_or_existing_tenant self.vacdays = property_vacant_days - if is_supported_housing? && scheme - if scheme.locations.size == 1 - self.location = scheme.locations.first - end + if is_supported_housing? if location # TODO: Remove and replace with mobility type self.wchair = location.wheelchair_adaptation_before_type_cast @@ -189,4 +186,11 @@ private (startdate - voiddate).to_i / 1.day end end + + def reset_scheme_location! + self.location = nil + if scheme && scheme.locations.size == 1 + self.location = scheme.locations.first + end + end end diff --git a/spec/services/imports/case_logs_import_service_spec.rb b/spec/services/imports/case_logs_import_service_spec.rb index 27b6b975c..b3b10a0ca 100644 --- a/spec/services/imports/case_logs_import_service_spec.rb +++ b/spec/services/imports/case_logs_import_service_spec.rb @@ -28,11 +28,12 @@ RSpec.describe Imports::CaseLogsImportService do # Scheme and Location scheme = FactoryBot.create(:scheme, old_visible_id: 123) - FactoryBot.create(:location, - old_visible_id: 10, - scheme_id: scheme.id, - wheelchair_adaptation: 1, - postcode: "LS166FT") + locations = FactoryBot.create_list(:location, 3, scheme_id: scheme.id) + locations[0].update!( + old_visible_id: 10, + wheelchair_adaptation: 1, + postcode: "LS166FT" + ) # Stub the form handler to use the real form allow(FormHandler.instance).to receive(:get_form).with("2021_2022").and_return(real_2021_2022_form)