Browse Source

refactor: use same structure as locations deactivation

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

19
app/controllers/schemes_controller.rb

@ -22,7 +22,9 @@ class SchemesController < ApplicationController
end end
def deactivate 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 confirm_deactivation
else else
deactivation_date_errors deactivation_date_errors
@ -31,14 +33,13 @@ class SchemesController < ApplicationController
# @scheme.deactivation_date = deactivation_date_errors # @scheme.deactivation_date = deactivation_date_errors
render "toggle_active", locals: { action: "deactivate" }, status: :unprocessable_entity render "toggle_active", locals: { action: "deactivate" }, status: :unprocessable_entity
else else
deactivation_date_value = deactivation_date render "toggle_active_confirm", locals: { action: "deactivate", 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 end
end end
end end
def reactivate
render "toggle_active", locals: { action: "reactivate" }
end end
def new def new
@ -283,19 +284,17 @@ private
def confirm_deactivation def confirm_deactivation
if @scheme.update(deactivation_date: params[:scheme][:deactivation_date]) 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" flash[:notice] = "#{@scheme.service_name} has been deactivated"
end end
redirect_to scheme_details_path(@scheme) redirect_to scheme_details_path(@scheme)
nil
end end
def deactivation_date_errors 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")) @scheme.errors.add(:deactivation_date_type, message: I18n.t("validations.scheme.deactivation_date.not_selected"))
end end
if params[:scheme].present? && params[:scheme][:deactivation_date_type] == "other" if params[:scheme][:deactivation_date_type] == "other"
day = params[:scheme]["deactivation_date(3i)"] day = params[:scheme]["deactivation_date(3i)"]
month = params[:scheme]["deactivation_date(2i)"] month = params[:scheme]["deactivation_date(2i)"]
year = params[:scheme]["deactivation_date(1i)"] year = params[:scheme]["deactivation_date(1i)"]

7
app/models/scheme.rb

@ -22,7 +22,6 @@ class Scheme < ApplicationRecord
auto_strip_attributes :service_name auto_strip_attributes :service_name
attr_accessor :deactivation_date_type attr_accessor :deactivation_date_type
attr_accessor :deactivation_date
SENSITIVE = { SENSITIVE = {
No: 0, No: 0,
@ -214,4 +213,10 @@ class Scheme < ApplicationRecord
def available_from def available_from
created_at created_at
end 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 end

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

@ -26,5 +26,8 @@
<% end %> <% end %>
<% if FeatureToggle.scheme_toggle_enabled? %> <% if FeatureToggle.scheme_toggle_enabled? %>
<% if @scheme.status == :active %>
<%= govuk_button_link_to "Deactivate this scheme", scheme_deactivate_path(@scheme), warning: true %> <%= govuk_button_link_to "Deactivate this scheme", scheme_deactivate_path(@scheme), warning: true %>
<% end %> <% else %>
<%= govuk_button_link_to "Reactivate this scheme", scheme_reactivate_path(@scheme) %>
<% end %><% end %>

19
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 %> <% content_for :before_content do %>
<%= govuk_back_link( <%= govuk_back_link(
text: "Back", text: "Back",
href: :back, href: scheme_details_path(@scheme),
) %> ) %>
<% end %> <% end %>
<%= form_with model: @scheme, url: scheme_deactivate_path(@scheme), method: "patch", local: true do |f| %> <%= form_with model: @scheme, url: scheme_deactivate_path(@scheme), method: "patch", local: true do |f| %>
<div class="govuk-grid-row"> <div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds"> <div class="govuk-grid-column-two-thirds">
<% collection_start_date = FormHandler.instance.current_collection_start_date %>
<%= f.govuk_error_summary %> <%= f.govuk_error_summary %>
<%= f.govuk_radio_buttons_fieldset :deactivation_date_type, <%= f.govuk_radio_buttons_fieldset :deactivation_date_type,
legend: { text: "When should this change apply?" }, legend: { text: I18n.t("questions.scheme.deactivation.apply_from") },
caption: { text: "Deactivate #{@scheme.service_name}" }, caption: { text: title },
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 %> hint: { text: I18n.t("hints.scheme.deactivation", date: collection_start_date.to_formatted_s(:govuk_date)) } 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." %> <%= govuk_warning_text text: I18n.t("warnings.scheme.deactivation.existing_logs") %>
<%= f.govuk_radio_button :deactivation_date_type, <%= f.govuk_radio_button :deactivation_date_type,
"default", "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, <%= f.govuk_radio_button :deactivation_date_type,
"other", "other",
label: { text: "For tenancies starting after a certain date" }, 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, <%= f.govuk_date_field :deactivation_date,
legend: { text: "Date", size: "m" }, legend: { text: "Date", size: "m" },
hint: { text: "For example, 27 3 2008" }, hint: { text: "For example, 27 3 2008" },

14
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| %> <%= form_with model: @scheme, url: scheme_deactivate_path(@scheme), method: "patch", local: true do |f| %>
<% content_for :before_content do %> <% content_for :before_content do %>
<%= govuk_back_link(href: :back) %> <%= govuk_back_link(href: :back) %>
<% end %> <% end %>
<h1 class="govuk-heading-l"> <h1 class="govuk-heading-l">
<span class="govuk-caption-l"><%= @scheme.service_name %></span> <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> </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." %> <%= govuk_warning_text text: I18n.t("warnings.scheme.deactivation.review_logs") %>
<%= f.hidden_field :confirm, :value => true %> <%= f.hidden_field :confirm, value: true %>
<%= f.hidden_field :deactivation_date, :value => deactivation_date %> <%= f.hidden_field :deactivation_date, value: deactivation_date %>
<div class="govuk-button-group"> <div class="govuk-button-group">
<%= f.govuk_submit "Deactivate this scheme" %> <%= f.govuk_submit "#{action.humanize} this scheme" %>
<%= govuk_button_link_to "Cancel", scheme_details_path, html: { method: :get }, secondary: true %> <%= govuk_button_link_to "Cancel", scheme_details_path(@scheme), html: { method: :get }, secondary: true %>
</div> </div>
<% end %> <% 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)" 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?" 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?" mobility_type: "What are the mobility standards for the majority of units in this location?"
scheme:
deactivation: deactivation:
apply_from: "When should this change apply?" apply_from: "When should this change apply?"
descriptions: descriptions:
@ -382,10 +383,11 @@ en:
postcode: "For example, SW1P 4DF." postcode: "For example, SW1P 4DF."
name: "This is how you refer to this location within your organisation" 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." 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." deactivation: "If the date is before %{date}, select ‘From the start of the current collection period’ because the previous period has now closed."
warnings: warnings:
location: scheme:
deactivation: 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." 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." 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 "edit-name", to: "schemes#edit_name"
get "support-services-provider", to: "schemes#support_services_provider" get "support-services-provider", to: "schemes#support_services_provider"
get "deactivate", to: "schemes#deactivate" get "deactivate", to: "schemes#deactivate"
get "reactivate", to: "schemes#reactivate"
patch "deactivate", to: "schemes#deactivate" patch "deactivate", to: "schemes#deactivate"
patch "reactivate", to: "schemes#reactivate"
member do member do
resources :locations do resources :locations do

Loading…
Cancel
Save