From 8ac35d0ccb0ffa876b3d010c368a7a35381bf8f0 Mon Sep 17 00:00:00 2001 From: Kat Date: Wed, 16 Nov 2022 14:29:34 +0000 Subject: [PATCH] Rename deactivations table --- app/helpers/locations_helper.rb | 2 +- app/models/location.rb | 4 ++-- ...tivation.rb => location_deactivation_period.rb} | 2 +- .../20221115101732_add_deactivations_table.rb | 2 +- db/schema.rb | 4 ++-- ...tivation.rb => location_deactivation_period.rb} | 2 +- spec/helpers/locations_helper_spec.rb | 4 ++-- spec/models/location_spec.rb | 14 +++++++------- spec/requests/locations_controller_spec.rb | 10 +++++----- 9 files changed, 22 insertions(+), 22 deletions(-) rename app/models/{location_deactivation.rb => location_deactivation_period.rb} (61%) rename spec/factories/{location_deactivation.rb => location_deactivation_period.rb} (58%) diff --git a/app/helpers/locations_helper.rb b/app/helpers/locations_helper.rb index b64f8f1bf..eaf1e07af 100644 --- a/app/helpers/locations_helper.rb +++ b/app/helpers/locations_helper.rb @@ -39,7 +39,7 @@ module LocationsHelper def location_availability(location) availability = "Active from #{location.available_from.to_formatted_s(:govuk_date)}" - location.location_deactivations.each do |deactivation| + location.location_deactivation_periods.each do |deactivation| availability << " to #{(deactivation.deactivation_date - 1.day).to_formatted_s(:govuk_date)}\nDeactivated on #{deactivation.deactivation_date.to_formatted_s(:govuk_date)}" availability << "\nActive from #{deactivation.reactivation_date.to_formatted_s(:govuk_date)}" if deactivation.reactivation_date.present? end diff --git a/app/models/location.rb b/app/models/location.rb index 656145fd9..f73f70549 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -4,7 +4,7 @@ class Location < ApplicationRecord validates :units, :type_of_unit, :mobility_type, presence: true belongs_to :scheme has_many :lettings_logs, class_name: "LettingsLog" - has_many :location_deactivations, class_name: "LocationDeactivation" + has_many :location_deactivation_periods, class_name: "LocationDeactivationPeriod" has_paper_trail @@ -375,7 +375,7 @@ class Location < ApplicationRecord end def status - recent_deactivation = location_deactivations.deactivations_without_reactivation.first + 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 diff --git a/app/models/location_deactivation.rb b/app/models/location_deactivation_period.rb similarity index 61% rename from app/models/location_deactivation.rb rename to app/models/location_deactivation_period.rb index 9b9780b0a..b8579f0bf 100644 --- a/app/models/location_deactivation.rb +++ b/app/models/location_deactivation_period.rb @@ -1,3 +1,3 @@ -class LocationDeactivation < ApplicationRecord +class LocationDeactivationPeriod < ApplicationRecord scope :deactivations_without_reactivation, -> { where(reactivation_date: nil) } end diff --git a/db/migrate/20221115101732_add_deactivations_table.rb b/db/migrate/20221115101732_add_deactivations_table.rb index c961869e0..b8d055a0d 100644 --- a/db/migrate/20221115101732_add_deactivations_table.rb +++ b/db/migrate/20221115101732_add_deactivations_table.rb @@ -1,6 +1,6 @@ class AddDeactivationsTable < ActiveRecord::Migration[7.0] def change - create_table :location_deactivations do |t| + create_table :location_deactivation_periods do |t| t.datetime :deactivation_date t.datetime :reactivation_date t.belongs_to :location diff --git a/db/schema.rb b/db/schema.rb index 17a18e968..2644c8b0b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -245,13 +245,13 @@ ActiveRecord::Schema[7.0].define(version: 2022_11_15_113437) do t.index ["scheme_id"], name: "index_lettings_logs_on_scheme_id" end - create_table "location_deactivations", force: :cascade do |t| + create_table "location_deactivation_periods", force: :cascade do |t| t.datetime "deactivation_date" t.datetime "reactivation_date" t.bigint "location_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.index ["location_id"], name: "index_location_deactivations_on_location_id" + t.index ["location_id"], name: "index_location_deactivation_periods_on_location_id" end create_table "locations", force: :cascade do |t| diff --git a/spec/factories/location_deactivation.rb b/spec/factories/location_deactivation_period.rb similarity index 58% rename from spec/factories/location_deactivation.rb rename to spec/factories/location_deactivation_period.rb index b72551abd..6a3ab70c3 100644 --- a/spec/factories/location_deactivation.rb +++ b/spec/factories/location_deactivation_period.rb @@ -1,5 +1,5 @@ FactoryBot.define do - factory :location_deactivation do + factory :location_deactivation_period do reactivation_date { nil } end end diff --git a/spec/helpers/locations_helper_spec.rb b/spec/helpers/locations_helper_spec.rb index a85ed782d..525043e6d 100644 --- a/spec/helpers/locations_helper_spec.rb +++ b/spec/helpers/locations_helper_spec.rb @@ -78,8 +78,8 @@ RSpec.describe LocationsHelper do context "with previous deactivations" do before do - location.location_deactivations << FactoryBot.create(:location_deactivation, deactivation_date: Time.zone.local(2022, 8, 10), reactivation_date: Time.zone.local(2022, 9, 1)) - location.location_deactivations << FactoryBot.create(:location_deactivation, deactivation_date: Time.zone.local(2022, 9, 15), reactivation_date: nil) + location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 8, 10), reactivation_date: Time.zone.local(2022, 9, 1)) + location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 9, 15), reactivation_date: nil) end it "displays the timeline of availability" do diff --git a/spec/models/location_spec.rb b/spec/models/location_spec.rb index 2a0623ff8..06518136e 100644 --- a/spec/models/location_spec.rb +++ b/spec/models/location_spec.rb @@ -125,19 +125,19 @@ RSpec.describe Location, type: :model do end it "returns deactivating soon if deactivation_date is in the future" do - location.location_deactivations << FactoryBot.create(:location_deactivation, deactivation_date: Time.zone.local(2022, 8, 8)) + location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 8, 8)) location.save! expect(location.status).to eq(:deactivating_soon) end it "returns deactivated if deactivation_date is in the past" do - location.location_deactivations << FactoryBot.create(:location_deactivation, deactivation_date: Time.zone.local(2022, 6, 6)) + location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 6)) location.save! expect(location.status).to eq(:deactivated) end it "returns deactivated if deactivation_date is today" do - location.location_deactivations << FactoryBot.create(:location_deactivation, deactivation_date: Time.zone.local(2022, 6, 7)) + location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 7)) location.save! expect(location.status).to eq(:deactivated) end @@ -145,7 +145,7 @@ RSpec.describe Location, type: :model do context "when there have been previous deactivations" do before do - location.location_deactivations << FactoryBot.create(:location_deactivation, 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)) end it "returns active if the location has no relevant deactivation records" do @@ -153,19 +153,19 @@ RSpec.describe Location, type: :model do end it "returns deactivating soon if deactivation_date is in the future" do - location.location_deactivations << FactoryBot.create(:location_deactivation, deactivation_date: Time.zone.local(2022, 8, 8)) + location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 8, 8)) location.save! expect(location.status).to eq(:deactivating_soon) end it "returns deactivated if deactivation_date is in the past" do - location.location_deactivations << FactoryBot.create(:location_deactivation, deactivation_date: Time.zone.local(2022, 6, 6)) + location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 6)) location.save! expect(location.status).to eq(:deactivated) end it "returns deactivated if deactivation_date is today" do - location.location_deactivations << FactoryBot.create(:location_deactivation, deactivation_date: Time.zone.local(2022, 6, 7)) + location.location_deactivation_periods << FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 6, 7)) location.save! expect(location.status).to eq(:deactivated) end diff --git a/spec/requests/locations_controller_spec.rb b/spec/requests/locations_controller_spec.rb index 323d7c13a..ff3d77ab0 100644 --- a/spec/requests/locations_controller_spec.rb +++ b/spec/requests/locations_controller_spec.rb @@ -1283,8 +1283,8 @@ RSpec.describe LocationsController, type: :request do expect(response).to have_http_status(:ok) expect(page).to have_css(".govuk-notification-banner.govuk-notification-banner--success") location.reload - expect(location.location_deactivations.count).to eq(1) - expect(location.location_deactivations.first.deactivation_date).to eq(deactivation_date) + expect(location.location_deactivation_periods.count).to eq(1) + expect(location.location_deactivation_periods.first.deactivation_date).to eq(deactivation_date) end context "and a log startdate is after location deactivation date" do @@ -1392,7 +1392,7 @@ RSpec.describe LocationsController, type: :request do let(:user) { FactoryBot.create(:user, :data_coordinator) } let!(:scheme) { FactoryBot.create(:scheme, owning_organisation: user.organisation) } let!(:location) { FactoryBot.create(:location, scheme:) } - let(:add_deactivations) { location.location_deactivations << location_deactivation } + let(:add_deactivations) { location.location_deactivation_periods << location_deactivation_period } before do Timecop.freeze(Time.utc(2022, 10, 10)) @@ -1412,7 +1412,7 @@ RSpec.describe LocationsController, type: :request do end context "with deactivated location" do - let(:location_deactivation) { FactoryBot.create(:location_deactivation, deactivation_date: Time.zone.local(2022, 10, 9)) } + let(:location_deactivation_period) { FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 10, 9)) } it "renders reactivate this location" do expect(response).to have_http_status(:ok) @@ -1421,7 +1421,7 @@ RSpec.describe LocationsController, type: :request do end context "with location that's deactivating soon" do - let(:location_deactivation) { FactoryBot.create(:location_deactivation, deactivation_date: Time.zone.local(2022, 10, 12)) } + let(:location_deactivation_period) { FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 10, 12)) } it "renders reactivate this location" do expect(response).to have_http_status(:ok)