Browse Source

refactor: use same structure as locations deactivation

pull/980/head
natdeanlewissoftwire 2 years ago
parent
commit
a3f7808b05
  1. 21
      app/controllers/schemes_controller.rb
  2. 7
      app/models/scheme.rb
  3. 7
      app/views/schemes/show.html.erb
  4. 23
      app/views/schemes/toggle_active.html.erb
  5. 20
      app/views/schemes/toggle_active_confirm.html.erb
  6. 4
      config/locales/en.yml
  7. 2
      config/routes.rb

21
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)"]

7
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

7
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 %>

23
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| %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= 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" },

20
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 %>
<h1 class="govuk-heading-l">
<span class="govuk-caption-l"><%= @scheme.service_name %></span>
<%= "This change will affect SOME logs" %>
<%= "This change will affect #{@scheme.lettings_logs.count} logs" %>
</h1>
<%= 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 %>
<div class="govuk-button-group">
<%= 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 %>
</div>
<% end %>

4
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."

2
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

Loading…
Cancel
Save