Browse Source

feat: respond to pr comments and add dynamic success text

pull/980/head
natdeanlewissoftwire 2 years ago
parent
commit
537a72c55a
  1. 37
      app/controllers/schemes_controller.rb

37
app/controllers/schemes_controller.rb

@ -25,7 +25,9 @@ class SchemesController < ApplicationController
if params[:scheme].blank? if params[:scheme].blank?
render "toggle_active", locals: { action: "deactivate" } render "toggle_active", locals: { action: "deactivate" }
else else
prepare_for_validation @scheme.run_deactivation_validations = true
@scheme.deactivation_date = deactivation_date
@scheme.deactivation_date_type = params[:scheme][:deactivation_date_type]
if @scheme.valid? if @scheme.valid?
redirect_to scheme_deactivate_confirm_path(@scheme, deactivation_date: @scheme.deactivation_date, deactivation_date_type: @scheme.deactivation_date_type) redirect_to scheme_deactivate_confirm_path(@scheme, deactivation_date: @scheme.deactivation_date, deactivation_date_type: @scheme.deactivation_date_type)
else else
@ -39,7 +41,9 @@ class SchemesController < ApplicationController
end end
def deactivate def deactivate
prepare_for_validation @scheme.run_deactivation_validations = true
@scheme.deactivation_date = deactivation_date
@scheme.deactivation_date_type = params[:scheme][:deactivation_date_type]
confirm_deactivation confirm_deactivation
end end
@ -289,17 +293,28 @@ private
def confirm_deactivation def confirm_deactivation
if @scheme.update!(deactivation_date: @scheme.deactivation_date) if @scheme.update!(deactivation_date: @scheme.deactivation_date)
flash[:notice] = "#{@scheme.service_name} has been deactivated" flash[:notice] = success_text
end end
redirect_to scheme_details_path(@scheme) redirect_to scheme_details_path(@scheme)
end end
def deactivation_date def success_text
return if params[:scheme].blank? case @scheme.status
when :deactivated
"#{@scheme.service_name} has been deactivated"
when :deactivating_soon
"#{@scheme.service_name} will deactivate on #{@scheme.deactivation_date.to_formatted_s(:govuk_date)}"
end
end
collection_start_date = FormHandler.instance.current_collection_start_date def deactivation_date
return collection_start_date if params[:scheme][:deactivation_date_type] == "default" if params[:scheme].blank?
return params[:scheme][:deactivation_date] if params[:scheme][:deactivation_date].present? return
elsif params[:scheme][:deactivation_date_type] == "default"
return FormHandler.instance.current_collection_start_date
elsif params[:scheme][:deactivation_date].present?
return params[:scheme][:deactivation_date]
end
day = params[:scheme]["deactivation_date(3i)"] day = params[:scheme]["deactivation_date(3i)"]
month = params[:scheme]["deactivation_date(2i)"] month = params[:scheme]["deactivation_date(2i)"]
@ -308,10 +323,4 @@ 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) 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 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 end

Loading…
Cancel
Save