Browse Source

Update logs status determination for location

pull/996/head
Kat 4 years ago
parent
commit
1289f20ccf
  1. 6
      app/models/location.rb
  2. 1
      app/models/location_deactivation.rb
  3. 42
      spec/models/location_spec.rb

6
app/models/location.rb

@ -375,9 +375,9 @@ class Location < ApplicationRecord
end end
def status def status
return :active if location_deactivations.count == 0 recent_deactivation = location_deactivations.deactivations_without_reactivation.first
return :deactivating_soon if Time.zone.now < location_deactivations.first.deactivation_date return :active unless recent_deactivation.present?
return :deactivated return :deactivating_soon if Time.zone.now < recent_deactivation.deactivation_date
:deactivated :deactivated
end end

1
app/models/location_deactivation.rb

@ -1,2 +1,3 @@
class LocationDeactivation < ApplicationRecord class LocationDeactivation < ApplicationRecord
scope :deactivations_without_reactivation, ->() { where(reactivation_date: nil) }
end end

42
spec/models/location_spec.rb

@ -119,34 +119,58 @@ RSpec.describe Location, type: :model do
Timecop.freeze(2022, 6, 7) Timecop.freeze(2022, 6, 7)
end end
it "returns active if the location is not deactivated" do context "when there have not been any previous deactivations" do
location.deactivation_date = nil it "returns active if the location has no deactivation records" do
location.deactivation_date_type = nil expect(location.status).to eq(:active)
end
it "returns deactivating soon if deactivation_date is in the future" do
location.location_deactivations << LocationDeactivation.create(deactivation_date: Time.zone.local(2022, 8, 8), reactivation_date: nil)
location.save!
expect(location.status).to eq(:deactivating_soon)
end
it "returns deactivated if deactivation_date is in the past" do
location.location_deactivations << LocationDeactivation.create(deactivation_date: Time.zone.local(2022, 6, 6), reactivation_date: nil)
location.save!
expect(location.status).to eq(:deactivated)
end
it "returns deactivated if deactivation_date is today" do
location.location_deactivations << LocationDeactivation.create(deactivation_date: Time.zone.local(2022, 6, 7), reactivation_date: nil)
location.save! location.save!
expect(location.status).to eq(:deactivated)
end
end
context "when there have been previous deactivations" do
before do
location.location_deactivations << LocationDeactivation.create(deactivation_date: Time.zone.local(2022, 6, 4), reactivation_date: Time.zone.local(2022, 6, 5))
end
it "returns active if the location has no relevant deactivation records" do
expect(location.status).to eq(:active) expect(location.status).to eq(:active)
end end
it "returns deactivating soon if deactivation_date is in the future" do it "returns deactivating soon if deactivation_date is in the future" do
location.deactivation_date = Time.zone.local(2022, 8, 8) location.location_deactivations << LocationDeactivation.create(deactivation_date: Time.zone.local(2022, 8, 8), reactivation_date: nil)
location.deactivation_date_type = "other"
location.save! location.save!
expect(location.status).to eq(:deactivating_soon) expect(location.status).to eq(:deactivating_soon)
end end
it "returns deactivated if deactivation_date is in the past" do it "returns deactivated if deactivation_date is in the past" do
location.deactivation_date = Time.zone.local(2022, 4, 8) location.location_deactivations << LocationDeactivation.create(deactivation_date: Time.zone.local(2022, 6, 6), reactivation_date: nil)
location.deactivation_date_type = "other"
location.save! location.save!
expect(location.status).to eq(:deactivated) expect(location.status).to eq(:deactivated)
end end
it "returns deactivated if deactivation_date is today" do it "returns deactivated if deactivation_date is today" do
location.deactivation_date = Time.zone.local(2022, 6, 7) location.location_deactivations << LocationDeactivation.create(deactivation_date: Time.zone.local(2022, 6, 7), reactivation_date: nil)
location.deactivation_date_type = "other"
location.save! location.save!
expect(location.status).to eq(:deactivated) expect(location.status).to eq(:deactivated)
end end
end end
end
describe "with deactivation_date (but no deactivation_date_type)" do describe "with deactivation_date (but no deactivation_date_type)" do
let(:location) { FactoryBot.create(:location, deactivation_date: Date.new(2022, 4, 1)) } let(:location) { FactoryBot.create(:location, deactivation_date: Date.new(2022, 4, 1)) }

Loading…
Cancel
Save