diff --git a/app/helpers/locations_helper.rb b/app/helpers/locations_helper.rb index be5d3d4c0..5203c0c06 100644 --- a/app/helpers/locations_helper.rb +++ b/app/helpers/locations_helper.rb @@ -53,6 +53,11 @@ module LocationsHelper availability.strip end + def toggle_location_link(location) + return govuk_button_link_to "Deactivate this location", scheme_location_new_deactivation_path(location.scheme, location), warning: true if location.active? + return govuk_button_link_to "Reactivate this location", scheme_location_new_reactivation_path(location.scheme, location) if location.deactivated? + end + private ActivePeriod = Struct.new(:from, :to) diff --git a/app/helpers/schemes_helper.rb b/app/helpers/schemes_helper.rb index 3d143f8a2..3cbac5789 100644 --- a/app/helpers/schemes_helper.rb +++ b/app/helpers/schemes_helper.rb @@ -42,6 +42,11 @@ module SchemesHelper availability.strip end + def toggle_scheme_link(scheme) + return govuk_button_link_to "Deactivate this scheme", scheme_new_deactivation_path(scheme), warning: true if scheme.active? + return govuk_button_link_to "Reactivate this scheme", scheme_new_reactivation_path(scheme) if scheme.deactivated? + end + private ActivePeriod = Struct.new(:from, :to) diff --git a/app/models/location.rb b/app/models/location.rb index 6eededcc0..850adfb05 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -383,7 +383,11 @@ class Location < ApplicationRecord location_deactivation_periods.order("created_at").last end - def status(date = Time.zone.now) + def status + @status ||= status_at(Time.zone.now) + end + + def status_at(date) return :incomplete unless confirmed return :deactivated if open_deactivation&.deactivation_date.present? && date >= open_deactivation.deactivation_date return :deactivating_soon if open_deactivation&.deactivation_date.present? && date < open_deactivation.deactivation_date @@ -392,12 +396,15 @@ class Location < ApplicationRecord :active end - alias_method :status_at, :status def active? status == :active end + def deactivated? + status == :deactivated + end + def reactivating_soon? status == :reactivating_soon end diff --git a/app/models/scheme.rb b/app/models/scheme.rb index a4dd21418..0f90b57f6 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -231,7 +231,11 @@ class Scheme < ApplicationRecord scheme_deactivation_periods.order("created_at").last end - def status(date = Time.zone.now) + def status + @status ||= status_at(Time.zone.now) + end + + def status_at(date) return :incomplete unless confirmed return :deactivated if open_deactivation&.deactivation_date.present? && date >= open_deactivation.deactivation_date return :deactivating_soon if open_deactivation&.deactivation_date.present? && date < open_deactivation.deactivation_date @@ -239,7 +243,6 @@ class Scheme < ApplicationRecord :active end - alias_method :status_at, :status def active? status == :active @@ -248,4 +251,8 @@ class Scheme < ApplicationRecord def reactivating_soon? status == :reactivating_soon end + + def deactivated? + status == :deactivated + end end diff --git a/app/views/locations/show.html.erb b/app/views/locations/show.html.erb index 5ed049d08..59ece6529 100644 --- a/app/views/locations/show.html.erb +++ b/app/views/locations/show.html.erb @@ -24,9 +24,5 @@ <% if FeatureToggle.location_toggle_enabled? %> - <% if @location.active? || @location.reactivating_soon? %> - <%= govuk_button_link_to "Deactivate this location", scheme_location_new_deactivation_path(@scheme, @location), warning: true %> - <% else %> - <%= govuk_button_link_to "Reactivate this location", scheme_location_new_reactivation_path(@scheme, @location) %> - <% end %> + <%= toggle_location_link(@location) %> <% end %> diff --git a/app/views/schemes/show.html.erb b/app/views/schemes/show.html.erb index 5d93414d0..5ef55f9f4 100644 --- a/app/views/schemes/show.html.erb +++ b/app/views/schemes/show.html.erb @@ -34,9 +34,5 @@ <% end %> <% if FeatureToggle.scheme_toggle_enabled? %> - <% if @scheme.active? || @scheme.reactivating_soon? %> - <%= govuk_button_link_to "Deactivate this scheme", scheme_new_deactivation_path(@scheme), warning: true %> - <% else %> - <%= govuk_button_link_to "Reactivate this scheme", scheme_new_reactivation_path(@scheme) %> - <% end %> + <%= toggle_scheme_link(@scheme) %> <% end %> diff --git a/spec/requests/locations_controller_spec.rb b/spec/requests/locations_controller_spec.rb index 9fc1b0257..f17ef604a 100644 --- a/spec/requests/locations_controller_spec.rb +++ b/spec/requests/locations_controller_spec.rb @@ -1519,18 +1519,20 @@ RSpec.describe LocationsController, type: :request do context "with location that's deactivating soon" do let(:location_deactivation_period) { FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 10, 12), location:) } - it "renders reactivate this location" do + it "does not render toggle location link" do expect(response).to have_http_status(:ok) - expect(page).to have_link("Reactivate this location", href: "/schemes/#{scheme.id}/locations/#{location.id}/new-reactivation") + expect(page).not_to have_link("Reactivate this location") + expect(page).not_to have_link("Deactivate this location") end end context "with location that's reactivating soon" do let(:location_deactivation_period) { FactoryBot.create(:location_deactivation_period, deactivation_date: Time.zone.local(2022, 4, 12), reactivation_date: Time.zone.local(2022, 10, 12), location:) } - it "renders reactivate this location" do + it "does not render toggle location link" do expect(response).to have_http_status(:ok) - expect(page).to have_link("Deactivate this location", href: "/schemes/#{scheme.id}/locations/#{location.id}/new-deactivation") + expect(page).not_to have_link("Reactivate this location") + expect(page).not_to have_link("Deactivate this location") end end end diff --git a/spec/requests/schemes_controller_spec.rb b/spec/requests/schemes_controller_spec.rb index 5530600ff..0a75e4fcd 100644 --- a/spec/requests/schemes_controller_spec.rb +++ b/spec/requests/schemes_controller_spec.rb @@ -292,9 +292,10 @@ RSpec.describe SchemesController, type: :request do 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), scheme:) } - it "renders reactivate this scheme" do + it "does not render toggle scheme link" do expect(response).to have_http_status(:ok) - expect(page).to have_link("Reactivate this scheme", href: "/schemes/#{scheme.id}/new-reactivation") + expect(page).not_to have_link("Reactivate this scheme") + expect(page).not_to have_link("Deactivate this scheme") end end end