From d2964d73db4f276f1db841a282ed3b0dfe34b225 Mon Sep 17 00:00:00 2001 From: Rachael Booth Date: Thu, 18 Jul 2024 13:01:55 +0100 Subject: [PATCH] CLDC-3549: Ensure location page routing is updated when log is changed (#2502) --- .../lettings_log_variables.rb | 4 +- spec/models/lettings_log_spec.rb | 46 +++++++++++++++++-- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/app/models/derived_variables/lettings_log_variables.rb b/app/models/derived_variables/lettings_log_variables.rb index e316d6efd..91e645c47 100644 --- a/app/models/derived_variables/lettings_log_variables.rb +++ b/app/models/derived_variables/lettings_log_variables.rb @@ -31,8 +31,8 @@ module DerivedVariables::LettingsLogVariables def scheme_has_multiple_locations? return false unless scheme - @scheme_locations_count ||= scheme.locations.active_in_2_weeks.size - @scheme_locations_count > 1 + scheme_locations_count = scheme.locations.active_in_2_weeks.size + scheme_locations_count > 1 end def set_derived_fields! diff --git a/spec/models/lettings_log_spec.rb b/spec/models/lettings_log_spec.rb index 2f4576719..5c40fbe08 100644 --- a/spec/models/lettings_log_spec.rb +++ b/spec/models/lettings_log_spec.rb @@ -486,18 +486,56 @@ RSpec.describe LettingsLog do end describe "when changing a log's scheme and hence calling reset_scheme_location!" do - let(:scheme) { FactoryBot.create(:scheme) } - let(:invalid_location_1) { FactoryBot.create(:location, scheme:, startdate: Time.zone.today + 3.weeks) } - let(:valid_location) { FactoryBot.create(:location, scheme:, startdate: Time.zone.yesterday) } - let(:invalid_location_2) { FactoryBot.create(:location, scheme:, startdate: Time.zone.today + 3.weeks) } + before do + Timecop.return + Singleton.__init__(FormHandler) + end context "when there is one valid location and many invalid locations in the new scheme" do + let(:scheme) { create(:scheme) } + let(:invalid_location_1) { create(:location, scheme:, startdate: Time.zone.today + 3.weeks) } + let(:valid_location) { create(:location, scheme:, startdate: Time.zone.yesterday) } + let(:invalid_location_2) { create(:location, scheme:, startdate: Time.zone.today + 3.weeks) } let(:log) { create(:lettings_log, scheme: nil, location_id: nil, startdate: Time.zone.today) } it "infers that the log is for the valid location" do expect { log.update!(scheme:) }.to change(log, :location_id).from(nil).to(valid_location.id) end end + + context "when there are many valid locations in the new scheme" do + let(:old_scheme) { create(:scheme, owning_organisation:) } + let(:old_location) { create(:location, scheme: old_scheme) } + let(:new_scheme) { create(:scheme, owning_organisation:) } + + before do + create_list(:location, 2, scheme: new_scheme) + end + + context "with a 2023 log" do + let(:log) { create(:lettings_log, :completed, :sh, :ignore_validation_errors, startdate: Time.zone.local(2024, 1, 1), owning_organisation:, scheme_id: old_scheme.id, location_id: old_location.id) } + + it "clears the location set on the log" do + expect { log.update!(scheme: new_scheme) }.to change(log, :location_id).from(old_location.id).to(nil) + end + + it "recalculates the log status" do + expect { log.update!(scheme: new_scheme) }.to change(log, :status).from("completed").to("in_progress") + end + end + + context "with a current year log" do + let(:log) { create(:lettings_log, :completed, :sh, :startdate_today, owning_organisation:, scheme_id: old_scheme.id, location_id: old_location.id) } + + it "clears the location set on the log" do + expect { log.update!(scheme: new_scheme) }.to change(log, :location_id).from(old_location.id).to(nil) + end + + it "recalculates the log status" do + expect { log.update!(scheme: new_scheme) }.to change(log, :status).from("completed").to("in_progress") + end + end + end end context "and a scheme with a single log is selected" do