From 98705302203ef833cba46e6027af191a13bf9a42 Mon Sep 17 00:00:00 2001 From: Kat Date: Fri, 18 Nov 2022 08:31:02 +0000 Subject: [PATCH] Add reactivation status --- app/models/location.rb | 11 +++++++---- spec/models/location_spec.rb | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/app/models/location.rb b/app/models/location.rb index ee7bdef38..f5114d5e1 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -376,11 +376,14 @@ class Location < ApplicationRecord end def status - recent_deactivation = location_deactivation_periods.deactivations_without_reactivation.first - return :active if recent_deactivation.blank? - return :deactivating_soon if Time.zone.now < recent_deactivation.deactivation_date + open_deactivation = location_deactivation_periods.deactivations_without_reactivation.first + recent_deactivation = location_deactivation_periods.order("created_at").last - :deactivated + return :deactivated if open_deactivation&.deactivation_date.present? && Time.zone.now >= open_deactivation.deactivation_date + return :deactivating_soon if open_deactivation&.deactivation_date.present? && Time.zone.now < open_deactivation.deactivation_date + return :reactivating_soon if recent_deactivation&.reactivation_date.present? && Time.zone.now < recent_deactivation.reactivation_date + + :active end def active? diff --git a/spec/models/location_spec.rb b/spec/models/location_spec.rb index 01185249a..fc8cb9ac2 100644 --- a/spec/models/location_spec.rb +++ b/spec/models/location_spec.rb @@ -145,11 +145,18 @@ RSpec.describe Location, type: :model do location.save! expect(location.status).to eq(:deactivated) end + + it "returns reactivating soon if the location has a future reactivation date" do + location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 7), reactivation_date: Time.zone.local(2022, 6, 8)) + location.save! + expect(location.status).to eq(:reactivating_soon) + end end context "when there have been previous deactivations" do before do location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 6, 5)) + location.save! end it "returns active if the location has no relevant deactivation records" do @@ -173,6 +180,13 @@ RSpec.describe Location, type: :model do location.save! expect(location.status).to eq(:deactivated) end + + it "returns reactivating soon if the location has a future reactivation date" do + Timecop.freeze(2022, 6, 8) + location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 7), reactivation_date: Time.zone.local(2022, 6, 9)) + location.save! + expect(location.status).to eq(:reactivating_soon) + end end end