Browse Source

feat: further separate scheme deactivation behaviour

pull/980/head
natdeanlewissoftwire 2 years ago
parent
commit
5c035e0c71
  1. 27
      app/controllers/schemes_controller.rb
  2. 6
      app/models/scheme.rb
  3. 2
      app/views/schemes/show.html.erb
  4. 2
      app/views/schemes/toggle_active.html.erb
  5. 4
      config/routes.rb

27
app/controllers/schemes_controller.rb

@ -21,23 +21,28 @@ class SchemesController < ApplicationController
render_not_found and return unless @scheme
end
def deactivate
def new_deactivation
if params[:scheme].blank?
render "toggle_active", locals: { action: "deactivate" }
else
@scheme.run_deactivation_validations = true
@scheme.deactivation_date = deactivation_date
@scheme.deactivation_date_type = params[:scheme][:deactivation_date_type]
if params[:scheme][:confirm].present?
confirm_deactivation
elsif @scheme.valid?
render "toggle_active_confirm", locals: { action: "deactivate", deactivation_date: @scheme.deactivation_date, deactivation_date_type: @scheme.deactivation_date_type }
prepare_for_validation
if @scheme.valid?
redirect_to scheme_deactivate_confirm_path(@scheme, deactivation_date: @scheme.deactivation_date, deactivation_date_type: @scheme.deactivation_date_type)
else
render "toggle_active", locals: { action: "deactivate" }, status: :unprocessable_entity
end
end
end
def deactivate_confirm
render "toggle_active_confirm", locals: { action: "deactivate", deactivation_date: params[:deactivation_date], deactivation_date_type: params[:deactivation_date_type] }
end
def deactivate
prepare_for_validation
confirm_deactivation
end
def reactivate
render "toggle_active", locals: { action: "reactivate" }
end
@ -303,4 +308,10 @@ private
Time.utc(year.to_i, month.to_i, day.to_i) if Date.valid_date?(year.to_i, month.to_i, day.to_i)
end
def prepare_for_validation
@scheme.run_deactivation_validations = true
@scheme.deactivation_date = deactivation_date
@scheme.deactivation_date_type = params[:scheme][:deactivation_date_type]
end
end

6
app/models/scheme.rb

@ -225,8 +225,12 @@ class Scheme < ApplicationRecord
status == :active
end
def run_deactivation_validations
deactivation_date.present? || @run_deactivation_validations
end
def deactivation_date_errors
return unless run_deactivation_validations == true
return unless run_deactivation_validations
collection_start_date = FormHandler.instance.current_collection_start_date
if deactivation_date_type.blank?

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

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

2
app/views/schemes/toggle_active.html.erb

@ -6,7 +6,7 @@
href: scheme_details_path(@scheme),
) %>
<% end %>
<%= form_with model: @scheme, url: scheme_deactivate_path(@scheme), method: "patch", local: true do |f| %>
<%= form_with model: @scheme, url: scheme_new_deactivation_path(@scheme), method: "patch", local: true do |f| %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<% collection_start_date = FormHandler.instance.current_collection_start_date %>

4
config/routes.rb

@ -49,8 +49,10 @@ Rails.application.routes.draw do
get "check-answers", to: "schemes#check_answers"
get "edit-name", to: "schemes#edit_name"
get "support-services-provider", to: "schemes#support_services_provider"
get "deactivate", to: "schemes#deactivate"
get "new-deactivation", to: "schemes#new_deactivation"
get "deactivate-confirm", to: "schemes#deactivate_confirm"
get "reactivate", to: "schemes#reactivate"
patch "new-deactivation", to: "schemes#new_deactivation"
patch "deactivate", to: "schemes#deactivate"
resources :locations do

Loading…
Cancel
Save