Browse Source

Update toggle scheme/location button (#1060)

* only display toggle scheme/location for active and deactivted schemes

* Memoize statuses
pull/1076/head
kosiakkatrina 2 years ago committed by GitHub
parent
commit
4b9c5c464a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      app/helpers/locations_helper.rb
  2. 5
      app/helpers/schemes_helper.rb
  3. 11
      app/models/location.rb
  4. 11
      app/models/scheme.rb
  5. 6
      app/views/locations/show.html.erb
  6. 6
      app/views/schemes/show.html.erb
  7. 10
      spec/requests/locations_controller_spec.rb
  8. 5
      spec/requests/schemes_controller_spec.rb

5
app/helpers/locations_helper.rb

@ -53,6 +53,11 @@ module LocationsHelper
availability.strip availability.strip
end 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 private
ActivePeriod = Struct.new(:from, :to) ActivePeriod = Struct.new(:from, :to)

5
app/helpers/schemes_helper.rb

@ -42,6 +42,11 @@ module SchemesHelper
availability.strip availability.strip
end 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 private
ActivePeriod = Struct.new(:from, :to) ActivePeriod = Struct.new(:from, :to)

11
app/models/location.rb

@ -383,7 +383,11 @@ class Location < ApplicationRecord
location_deactivation_periods.order("created_at").last location_deactivation_periods.order("created_at").last
end 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 :incomplete unless confirmed
return :deactivated if open_deactivation&.deactivation_date.present? && date >= open_deactivation.deactivation_date 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 return :deactivating_soon if open_deactivation&.deactivation_date.present? && date < open_deactivation.deactivation_date
@ -392,12 +396,15 @@ class Location < ApplicationRecord
:active :active
end end
alias_method :status_at, :status
def active? def active?
status == :active status == :active
end end
def deactivated?
status == :deactivated
end
def reactivating_soon? def reactivating_soon?
status == :reactivating_soon status == :reactivating_soon
end end

11
app/models/scheme.rb

@ -231,7 +231,11 @@ class Scheme < ApplicationRecord
scheme_deactivation_periods.order("created_at").last scheme_deactivation_periods.order("created_at").last
end 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 :incomplete unless confirmed
return :deactivated if open_deactivation&.deactivation_date.present? && date >= open_deactivation.deactivation_date 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 return :deactivating_soon if open_deactivation&.deactivation_date.present? && date < open_deactivation.deactivation_date
@ -239,7 +243,6 @@ class Scheme < ApplicationRecord
:active :active
end end
alias_method :status_at, :status
def active? def active?
status == :active status == :active
@ -248,4 +251,8 @@ class Scheme < ApplicationRecord
def reactivating_soon? def reactivating_soon?
status == :reactivating_soon status == :reactivating_soon
end end
def deactivated?
status == :deactivated
end
end end

6
app/views/locations/show.html.erb

@ -24,9 +24,5 @@
</div> </div>
</div> </div>
<% if FeatureToggle.location_toggle_enabled? %> <% if FeatureToggle.location_toggle_enabled? %>
<% if @location.active? || @location.reactivating_soon? %> <%= toggle_location_link(@location) %>
<%= 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 %>
<% end %> <% end %>

6
app/views/schemes/show.html.erb

@ -34,9 +34,5 @@
<% end %> <% end %>
<% if FeatureToggle.scheme_toggle_enabled? %> <% if FeatureToggle.scheme_toggle_enabled? %>
<% if @scheme.active? || @scheme.reactivating_soon? %> <%= toggle_scheme_link(@scheme) %>
<%= 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 %>
<% end %> <% end %>

10
spec/requests/locations_controller_spec.rb

@ -1519,18 +1519,20 @@ RSpec.describe LocationsController, type: :request do
context "with location that's deactivating soon" 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:) } 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(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
end end
context "with location that's reactivating soon" do 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:) } 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(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 end
end end

5
spec/requests/schemes_controller_spec.rb

@ -292,9 +292,10 @@ RSpec.describe SchemesController, type: :request do
context "with scheme that's deactivating soon" 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:) } 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(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 end
end end

Loading…
Cancel
Save