Browse Source

Add reactivation status

pull/1007/head
Kat 4 years ago
parent
commit
9870530220
  1. 11
      app/models/location.rb
  2. 14
      spec/models/location_spec.rb

11
app/models/location.rb

@ -376,11 +376,14 @@ class Location < ApplicationRecord
end end
def status def status
recent_deactivation = location_deactivation_periods.deactivations_without_reactivation.first open_deactivation = location_deactivation_periods.deactivations_without_reactivation.first
return :active if recent_deactivation.blank? recent_deactivation = location_deactivation_periods.order("created_at").last
return :deactivating_soon if Time.zone.now < recent_deactivation.deactivation_date
: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 end
def active? def active?

14
spec/models/location_spec.rb

@ -145,11 +145,18 @@ RSpec.describe Location, type: :model do
location.save! location.save!
expect(location.status).to eq(:deactivated) expect(location.status).to eq(:deactivated)
end 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 end
context "when there have been previous deactivations" do context "when there have been previous deactivations" do
before 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.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 end
it "returns active if the location has no relevant deactivation records" do it "returns active if the location has no relevant deactivation records" do
@ -173,6 +180,13 @@ RSpec.describe Location, type: :model do
location.save! location.save!
expect(location.status).to eq(:deactivated) expect(location.status).to eq(:deactivated)
end 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
end end

Loading…
Cancel
Save