Browse Source

Update status method

pull/1004/head
Kat 4 years ago
parent
commit
acf6676947
  1. 5
      app/models/scheme.rb
  2. 5
      spec/factories/scheme_deactivation_period.rb
  3. 40
      spec/models/scheme_spec.rb
  4. 25
      spec/requests/schemes_controller_spec.rb

5
app/models/scheme.rb

@ -217,8 +217,9 @@ class Scheme < ApplicationRecord
end end
def status def status
return :active if deactivation_date.blank? recent_deactivation = scheme_deactivation_periods.deactivations_without_reactivation.first
return :deactivating_soon if Time.zone.now < deactivation_date return :active if recent_deactivation.blank?
return :deactivating_soon if Time.zone.now < recent_deactivation.deactivation_date
:deactivated :deactivated
end end

5
spec/factories/scheme_deactivation_period.rb

@ -0,0 +1,5 @@
FactoryBot.define do
factory :scheme_deactivation_period do
reactivation_date { nil }
end
end

40
spec/models/scheme_spec.rb

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

25
spec/requests/schemes_controller_spec.rb

@ -246,19 +246,42 @@ RSpec.describe SchemesController, type: :request do
context "when looking at scheme details" do context "when looking at scheme details" do
let(:user) { FactoryBot.create(:user, :data_coordinator) } let(:user) { FactoryBot.create(:user, :data_coordinator) }
let!(:scheme) { FactoryBot.create(:scheme, owning_organisation: user.organisation) } let!(:scheme) { FactoryBot.create(:scheme, owning_organisation: user.organisation) }
let(:add_deactivations) { scheme.scheme_deactivation_periods << scheme_deactivation_period }
before do before do
Timecop.freeze(Time.utc(2022, 10, 10)) Timecop.freeze(Time.utc(2022, 10, 10))
sign_in user sign_in user
add_deactivations
scheme.save!
get "/schemes/#{scheme.id}" get "/schemes/#{scheme.id}"
end end
context "with active scheme" do context "with active scheme" do
let(:add_deactivations) {}
it "renders deactivate this scheme" do it "renders deactivate this scheme" do
expect(response).to have_http_status(:ok) expect(response).to have_http_status(:ok)
expect(page).to have_link("Deactivate this scheme", href: "/schemes/#{scheme.id}/new-deactivation") expect(page).to have_link("Deactivate this scheme", href: "/schemes/#{scheme.id}/new-deactivation")
end end
end end
context "with deactivated scheme" do
let(:scheme_deactivation_period) { FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 10, 9)) }
it "renders reactivate this scheme" do
expect(response).to have_http_status(:ok)
expect(page).to have_link("Reactivate this scheme", href: "/schemes/#{scheme.id}/reactivate")
end
end
context "with scheme that's deactivating soon" do
let(:scheme_deactivation_period) { FactoryBot.create(:scheme_deactivation_period, deactivation_date: Time.zone.local(2022, 10, 12)) }
it "renders reactivate this scheme" do
expect(response).to have_http_status(:ok)
expect(page).to have_link("Reactivate this scheme", href: "/schemes/#{scheme.id}/reactivate")
end
end
end end
end end
@ -1743,7 +1766,6 @@ RSpec.describe SchemesController, type: :request do
let(:user) { FactoryBot.create(:user, :data_coordinator) } let(:user) { FactoryBot.create(:user, :data_coordinator) }
let!(:scheme) { FactoryBot.create(:scheme, owning_organisation: user.organisation) } let!(:scheme) { FactoryBot.create(:scheme, owning_organisation: user.organisation) }
let!(:location) { FactoryBot.create(:location, scheme:) } let!(:location) { FactoryBot.create(:location, scheme:) }
let(:startdate) { Time.utc(2021, 1, 2) }
let(:deactivation_date) { Time.utc(2022, 10, 10) } let(:deactivation_date) { Time.utc(2022, 10, 10) }
let!(:lettings_log) { FactoryBot.create(:lettings_log, :sh, location:, scheme:, startdate:, owning_organisation: user.organisation) } let!(:lettings_log) { FactoryBot.create(:lettings_log, :sh, location:, scheme:, startdate:, owning_organisation: user.organisation) }
let(:startdate) { Time.utc(2022, 10, 11) } let(:startdate) { Time.utc(2022, 10, 11) }
@ -1787,6 +1809,7 @@ RSpec.describe SchemesController, type: :request do
follow_redirect! follow_redirect!
follow_redirect! follow_redirect!
expect(response).to have_http_status(:ok) expect(response).to have_http_status(:ok)
expect(page).to have_css(".govuk-notification-banner.govuk-notification-banner--success")
scheme.reload scheme.reload
expect(scheme.scheme_deactivation_periods.count).to eq(1) expect(scheme.scheme_deactivation_periods.count).to eq(1)
expect(scheme.scheme_deactivation_periods.first.deactivation_date).to eq(deactivation_date) expect(scheme.scheme_deactivation_periods.first.deactivation_date).to eq(deactivation_date)

Loading…
Cancel
Save