diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index 7d89a1bfa..b25f951ea 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -24,7 +24,7 @@ class LocationsController < ApplicationController if params[:location].blank? render "toggle_active", locals: { action: "deactivate" } else - @location.run_deactivation_validations = true + @location.run_deactivation_validations! @location.deactivation_date = deactivation_date @location.deactivation_date_type = params[:location][:deactivation_date_type] if @location.valid? @@ -36,14 +36,17 @@ class LocationsController < ApplicationController end def deactivate_confirm - render "toggle_active_confirm", locals: { action: "deactivate", deactivation_date: params[:deactivation_date], deactivation_date_type: params[:deactivation_date_type] } + @deactivation_date = params[:deactivation_date] + @deactivation_date_type = params[:deactivation_date_type] end def deactivate - @location.run_deactivation_validations = true - @location.deactivation_date = deactivation_date - @location.deactivation_date_type = params[:location][:deactivation_date_type] - confirm_deactivation + @location.run_deactivation_validations! + + if @location.update!(deactivation_date:) + flash[:notice] = deactivate_success_notice + end + redirect_to scheme_location_path(@scheme, @location) end def reactivate @@ -177,14 +180,7 @@ private location_params["location_admin_district"] != "Select an option" end - def confirm_deactivation - if @location.update!(deactivation_date: @location.deactivation_date) - flash[:notice] = success_text - end - redirect_to scheme_location_path(@scheme, @location) - end - - def success_text + def deactivate_success_notice case @location.status when :deactivated "#{@location.name} has been deactivated" @@ -207,6 +203,6 @@ private year = params[:location]["deactivation_date(1i)"] return nil if [day, month, year].any?(&:blank?) - 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.zone.local(year.to_i, month.to_i, day.to_i) if Date.valid_date?(year.to_i, month.to_i, day.to_i) end end diff --git a/app/controllers/schemes_controller.rb b/app/controllers/schemes_controller.rb index 0f06d86a3..6766ffe85 100644 --- a/app/controllers/schemes_controller.rb +++ b/app/controllers/schemes_controller.rb @@ -37,14 +37,17 @@ class SchemesController < ApplicationController end def deactivate_confirm - render "toggle_active_confirm", locals: { action: "deactivate", deactivation_date: params[:deactivation_date], deactivation_date_type: params[:deactivation_date_type] } + @deactivation_date = params[:deactivation_date] + @deactivation_date_type = params[:deactivation_date_type] end def deactivate - @scheme.run_deactivation_validations = true - @scheme.deactivation_date = deactivation_date - @scheme.deactivation_date_type = params[:scheme][:deactivation_date_type] - confirm_deactivation + @scheme.run_deactivation_validations! + + if @scheme.update!(deactivation_date:) + flash[:notice] = deactivate_success_notice + end + redirect_to scheme_details_path(@scheme) end def reactivate @@ -291,14 +294,7 @@ private redirect_to @scheme if @scheme.confirmed? end - def confirm_deactivation - if @scheme.update!(deactivation_date: @scheme.deactivation_date) - flash[:notice] = success_text - end - redirect_to scheme_details_path(@scheme) - end - - def success_text + def deactivate_success_notice case @scheme.status when :deactivated "#{@scheme.service_name} has been deactivated" @@ -321,6 +317,6 @@ private year = params[:scheme]["deactivation_date(1i)"] return nil if [day, month, year].any?(&:blank?) - 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.zone.local(year.to_i, month.to_i, day.to_i) if Date.valid_date?(year.to_i, month.to_i, day.to_i) end end diff --git a/app/models/form_handler.rb b/app/models/form_handler.rb index c080e6e9b..61b981436 100644 --- a/app/models/form_handler.rb +++ b/app/models/form_handler.rb @@ -50,7 +50,7 @@ class FormHandler end def current_collection_start_date - Time.utc(current_collection_start_year, 4, 1) + Time.zone.local(current_collection_start_year, 4, 1) end def form_name_from_start_year(year, type) diff --git a/app/models/location.rb b/app/models/location.rb index 1647f8c8e..85017afad 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -384,6 +384,10 @@ class Location < ApplicationRecord status == :active end + def run_deactivation_validations! + @run_deactivation_validations = true + end + def implicit_run_deactivation_validations deactivation_date.present? || @run_deactivation_validations end diff --git a/app/models/scheme.rb b/app/models/scheme.rb index 9efc820a9..fe3f40a4a 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -226,6 +226,10 @@ class Scheme < ApplicationRecord status == :active end + def run_deactivation_validations! + @run_deactivation_validations = true + end + def implicit_run_deactivation_validations deactivation_date.present? || @run_deactivation_validations end diff --git a/app/views/locations/toggle_active_confirm.html.erb b/app/views/locations/deactivate_confirm.html.erb similarity index 68% rename from app/views/locations/toggle_active_confirm.html.erb rename to app/views/locations/deactivate_confirm.html.erb index 908edceb7..e3f1ae175 100644 --- a/app/views/locations/toggle_active_confirm.html.erb +++ b/app/views/locations/deactivate_confirm.html.erb @@ -8,10 +8,10 @@ <%= govuk_warning_text text: I18n.t("warnings.location.deactivation.review_logs") %> <%= f.hidden_field :confirm, value: true %> - <%= f.hidden_field :deactivation_date, value: deactivation_date %> - <%= f.hidden_field :deactivation_date_type, value: deactivation_date_type %> + <%= f.hidden_field :deactivation_date, value: @deactivation_date %> + <%= f.hidden_field :deactivation_date_type, value: @deactivation_date_type %>
<% end %> diff --git a/app/views/locations/toggle_active.html.erb b/app/views/locations/toggle_active.html.erb index f5f6e1144..6d2f4a9ba 100644 --- a/app/views/locations/toggle_active.html.erb +++ b/app/views/locations/toggle_active.html.erb @@ -4,7 +4,7 @@ <% content_for :before_content do %> <%= govuk_back_link( text: "Back", - href: scheme_location_path(scheme_id: @location.scheme.id, id: @location.id), + href: scheme_location_path(@location.scheme, @location), ) %> <% end %> diff --git a/app/views/schemes/toggle_active_confirm.html.erb b/app/views/schemes/deactivate_confirm.html.erb similarity index 78% rename from app/views/schemes/toggle_active_confirm.html.erb rename to app/views/schemes/deactivate_confirm.html.erb index bb07bf5b2..95d3896be 100644 --- a/app/views/schemes/toggle_active_confirm.html.erb +++ b/app/views/schemes/deactivate_confirm.html.erb @@ -1,4 +1,4 @@ -<% title = "#{action.humanize} #{@scheme.service_name}" %> +<% title = "Deactivate #{@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 %> @@ -10,8 +10,8 @@ <%= 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.hidden_field :deactivation_date_type, value: deactivation_date_type %> + <%= f.hidden_field :deactivation_date, value: @deactivation_date %> + <%= f.hidden_field :deactivation_date_type, value: @deactivation_date_type %>