From a3f7808b0548ccba806b547d1ecb5c7e73da9ad4 Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire Date: Fri, 11 Nov 2022 16:03:43 +0000 Subject: [PATCH] refactor: use same structure as locations deactivation --- app/controllers/schemes_controller.rb | 21 ++++++++--------- app/models/scheme.rb | 7 +++++- app/views/schemes/show.html.erb | 7 ++++-- app/views/schemes/toggle_active.html.erb | 23 ++++++++++--------- .../schemes/toggle_active_confirm.html.erb | 20 ++++++++-------- config/locales/en.yml | 4 +++- config/routes.rb | 2 ++ 7 files changed, 49 insertions(+), 35 deletions(-) diff --git a/app/controllers/schemes_controller.rb b/app/controllers/schemes_controller.rb index 7e0eb1a58..0b4e57113 100644 --- a/app/controllers/schemes_controller.rb +++ b/app/controllers/schemes_controller.rb @@ -22,7 +22,9 @@ class SchemesController < ApplicationController end def deactivate - if params[:scheme].present? && params[:scheme][:confirm].present? && params[:scheme][:deactivation_date].present? + if params[:scheme].blank? + render "toggle_active", locals: { action: "deactivate" } + elsif params[:scheme][:confirm].present? && params[:scheme][:deactivation_date].present? confirm_deactivation else deactivation_date_errors @@ -31,16 +33,15 @@ class SchemesController < ApplicationController # @scheme.deactivation_date = deactivation_date_errors render "toggle_active", locals: { action: "deactivate" }, status: :unprocessable_entity else - deactivation_date_value = deactivation_date - if deactivation_date_value.blank? - render "toggle_active", locals: { action: "deactivate" } - else - render "toggle_active_confirm", locals: { action: "deactivate", deactivation_date: deactivation_date_value } - end + render "toggle_active_confirm", locals: { action: "deactivate", deactivation_date: } end end end + def reactivate + render "toggle_active", locals: { action: "reactivate" } + end + def new @scheme = Scheme.new end @@ -283,19 +284,17 @@ private def confirm_deactivation if @scheme.update(deactivation_date: params[:scheme][:deactivation_date]) - @scheme.lettings_logs.filter_by_before_startdate(params[:scheme][:deactivation_date]).update!(scheme: nil) flash[:notice] = "#{@scheme.service_name} has been deactivated" end redirect_to scheme_details_path(@scheme) - nil end def deactivation_date_errors - if params[:scheme].present? && params[:scheme][:deactivation_date].blank? && params[:scheme][:deactivation_date_type].blank? + if params[:scheme][:deactivation_date].blank? && params[:scheme][:deactivation_date_type].blank? @scheme.errors.add(:deactivation_date_type, message: I18n.t("validations.scheme.deactivation_date.not_selected")) end - if params[:scheme].present? && params[:scheme][:deactivation_date_type] == "other" + if params[:scheme][:deactivation_date_type] == "other" day = params[:scheme]["deactivation_date(3i)"] month = params[:scheme]["deactivation_date(2i)"] year = params[:scheme]["deactivation_date(1i)"] diff --git a/app/models/scheme.rb b/app/models/scheme.rb index a4ae000fb..70777c562 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -22,7 +22,6 @@ class Scheme < ApplicationRecord auto_strip_attributes :service_name attr_accessor :deactivation_date_type - attr_accessor :deactivation_date SENSITIVE = { No: 0, @@ -214,4 +213,10 @@ class Scheme < ApplicationRecord def available_from created_at end + + def status + return :active if deactivation_date.blank? + return :deactivating_soon if Time.zone.now < deactivation_date + return :deactivated if Time.zone.now >= deactivation_date + end end diff --git a/app/views/schemes/show.html.erb b/app/views/schemes/show.html.erb index 7bf7d84a2..bd4ef5e86 100644 --- a/app/views/schemes/show.html.erb +++ b/app/views/schemes/show.html.erb @@ -26,5 +26,8 @@ <% end %> <% if FeatureToggle.scheme_toggle_enabled? %> - <%= govuk_button_link_to "Deactivate this scheme", scheme_deactivate_path(@scheme), warning: true %> -<% end %> + <% if @scheme.status == :active %> + <%= govuk_button_link_to "Deactivate this scheme", scheme_deactivate_path(@scheme), warning: true %> + <% else %> + <%= govuk_button_link_to "Reactivate this scheme", scheme_reactivate_path(@scheme) %> + <% end %><% end %> diff --git a/app/views/schemes/toggle_active.html.erb b/app/views/schemes/toggle_active.html.erb index d6897571d..3eddeb771 100644 --- a/app/views/schemes/toggle_active.html.erb +++ b/app/views/schemes/toggle_active.html.erb @@ -1,27 +1,28 @@ +<% title = "#{action.humanize} #{@scheme.service_name}" %> +<% content_for :title, title %> <% content_for :before_content do %> <%= govuk_back_link( text: "Back", - href: :back, + href: scheme_details_path(@scheme), ) %> <% end %> - <%= form_with model: @scheme, url: scheme_deactivate_path(@scheme), method: "patch", local: true do |f| %>
- <%= f.govuk_error_summary %> - <%= f.govuk_radio_buttons_fieldset :deactivation_date_type, - legend: { text: "When should this change apply?" }, - caption: { text: "Deactivate #{@scheme.service_name}" }, - hint: { text: "If the date is before 1 April 2022, select ‘From the start of the current collection period’ because the previous period has now closed."} do %> - <%= govuk_warning_text text: "It will not be possible to add logs with this scheme if their tenancy start date is on or after the date you enter. Any existing logs may be affected." %> + <% collection_start_date = FormHandler.instance.current_collection_start_date %> + <%= f.govuk_error_summary %> + <%= f.govuk_radio_buttons_fieldset :deactivation_date_type, + legend: { text: I18n.t("questions.scheme.deactivation.apply_from") }, + caption: { text: title }, + hint: { text: I18n.t("hints.scheme.deactivation", date: collection_start_date.to_formatted_s(:govuk_date)) } do %> + <%= govuk_warning_text text: I18n.t("warnings.scheme.deactivation.existing_logs") %> <%= f.govuk_radio_button :deactivation_date_type, "default", - label: { text: "From the start of the current collection period (1 April 2022)" } %> - + label: { text: "From the start of the current collection period (#{collection_start_date.to_formatted_s(:govuk_date)})" } %> <%= f.govuk_radio_button :deactivation_date_type, "other", label: { text: "For tenancies starting after a certain date" }, - **basic_conditional_html_attributes({"deactivation_date" => ["other"]}, "scheme") do %> + **basic_conditional_html_attributes({ "deactivation_date" => %w[other] }, "scheme") do %> <%= f.govuk_date_field :deactivation_date, legend: { text: "Date", size: "m" }, hint: { text: "For example, 27 3 2008" }, diff --git a/app/views/schemes/toggle_active_confirm.html.erb b/app/views/schemes/toggle_active_confirm.html.erb index ed4d6203f..bc88dc93e 100644 --- a/app/views/schemes/toggle_active_confirm.html.erb +++ b/app/views/schemes/toggle_active_confirm.html.erb @@ -1,16 +1,18 @@ +<% title = "#{action.humanize} #{@scheme.service_name}" %> +<% content_for :title, title %> <%= form_with model: @scheme, url: scheme_deactivate_path(@scheme), method: "patch", local: true do |f| %> - <% content_for :before_content do %> - <%= govuk_back_link(href: :back) %> - <% end %> + <% content_for :before_content do %> + <%= govuk_back_link(href: :back) %> + <% end %>

<%= @scheme.service_name %> - <%= "This change will affect SOME logs" %> + <%= "This change will affect #{@scheme.lettings_logs.count} logs" %>

- <%= govuk_warning_text text: "Your data providers will need to review these logs and answer a few questions again. We’ll email each log creator with a list of logs that need updating." %> - <%= f.hidden_field :confirm, :value => true %> - <%= f.hidden_field :deactivation_date, :value => deactivation_date %> + <%= govuk_warning_text text: I18n.t("warnings.scheme.deactivation.review_logs") %> + <%= f.hidden_field :confirm, value: true %> + <%= f.hidden_field :deactivation_date, value: deactivation_date %>
- <%= f.govuk_submit "Deactivate this scheme" %> - <%= govuk_button_link_to "Cancel", scheme_details_path, html: { method: :get }, secondary: true %> + <%= f.govuk_submit "#{action.humanize} this scheme" %> + <%= govuk_button_link_to "Cancel", scheme_details_path(@scheme), html: { method: :get }, secondary: true %>
<% end %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 5f51593dd..1d0a82ca9 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -368,6 +368,7 @@ en: startdate: "When did the first property in this location become available under this scheme? (optional)" add_another_location: "Do you want to add another location?" mobility_type: "What are the mobility standards for the majority of units in this location?" + scheme: deactivation: apply_from: "When should this change apply?" descriptions: @@ -382,10 +383,11 @@ en: postcode: "For example, SW1P 4DF." name: "This is how you refer to this location within your organisation" units: "A unit can be a bedroom in a shared house or flat, or a house with 4 bedrooms. Do not include bedrooms used for wardens, managers, volunteers or sleep-in staff." + scheme: deactivation: "If the date is before %{date}, select ‘From the start of the current collection period’ because the previous period has now closed." warnings: - location: + scheme: deactivation: existing_logs: "It will not be possible to add logs with this scheme if their tenancy start date is on or after the date you enter. Any existing logs may be affected." review_logs: "Your data providers will need to review these logs and answer a few questions again. We’ll email each log creator with a list of logs that need updating." diff --git a/config/routes.rb b/config/routes.rb index 653d352eb..d06a358eb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -50,7 +50,9 @@ Rails.application.routes.draw do get "edit-name", to: "schemes#edit_name" get "support-services-provider", to: "schemes#support_services_provider" get "deactivate", to: "schemes#deactivate" + get "reactivate", to: "schemes#reactivate" patch "deactivate", to: "schemes#deactivate" + patch "reactivate", to: "schemes#reactivate" member do resources :locations do