From 5bd939866c017330ca6078f925fa536e32a5215f Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 21 Nov 2022 14:46:58 +0000 Subject: [PATCH] update validations --- app/controllers/locations_controller.rb | 6 +++--- app/models/location_deactivation_period.rb | 18 ++++++------------ spec/requests/locations_controller_spec.rb | 2 +- 3 files changed, 10 insertions(+), 16 deletions(-) diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index 6289d0404..fd9738467 100644 --- a/app/controllers/locations_controller.rb +++ b/app/controllers/locations_controller.rb @@ -43,7 +43,7 @@ class LocationsController < ApplicationController end def deactivate - if LocationDeactivationPeriod.create!(location_id: @location.id, deactivation_date: toggle_date("deactivation_date")) && update_affected_logs + if LocationDeactivationPeriod.create!(location_id: @location.id, deactivation_date: params[:deactivation_date]) && update_affected_logs flash[:notice] = deactivate_success_notice end redirect_to scheme_location_path(@scheme, @location) @@ -200,7 +200,7 @@ private when :deactivated "#{@location.name} has been deactivated" when :deactivating_soon - "#{@location.name} will deactivate on #{toggle_date('deactivation_date').to_time.to_formatted_s(:govuk_date)}" + "#{@location.name} will deactivate on #{params[:deactivation_date].to_time.to_formatted_s(:govuk_date)}" end end @@ -214,7 +214,7 @@ private end def update_affected_logs - @location.lettings_logs.filter_by_before_startdate(toggle_date("deactivation_date").to_time).update!(location: nil, scheme: nil) + @location.lettings_logs.filter_by_before_startdate(params[:deactivation_date].to_time).update!(location: nil, scheme: nil) end def toggle_date(key) diff --git a/app/models/location_deactivation_period.rb b/app/models/location_deactivation_period.rb index 9ba88592c..9cb7e6860 100644 --- a/app/models/location_deactivation_period.rb +++ b/app/models/location_deactivation_period.rb @@ -2,10 +2,11 @@ class LocationDeactivationPeriodValidator < ActiveModel::Validator def validate(record) location = record.location - if record.deactivation_date.blank? - if record.deactivation_date_type.blank? + recent_deactivation = location.location_deactivation_periods.deactivations_without_reactivation.first + if record.deactivation_date.blank? && recent_deactivation.nil? || recent_deactivation && record.reactivation_date.blank? + if record.deactivation_date_type.blank? && record.reactivation_date_type.blank? record.errors.add(:deactivation_date_type, message: I18n.t("validations.location.toggle_date.not_selected")) - elsif record.deactivation_date_type == "other" + elsif record.deactivation_date_type == "other" || record.reactivation_date_type == "other" record.errors.add(:deactivation_date, message: I18n.t("validations.location.toggle_date.invalid")) end elsif location.location_deactivation_periods.any? { |period| period.reactivation_date.present? && record.deactivation_date.between?(period.deactivation_date, period.reactivation_date - 1.day) } @@ -16,15 +17,8 @@ class LocationDeactivationPeriodValidator < ActiveModel::Validator end end - recent_deactivation = location.location_deactivation_periods.deactivations_without_reactivation.first - if recent_deactivation - if record.reactivation_date.blank? - if record.reactivation_date_type.blank? - record.errors.add(:reactivation_date_type, message: I18n.t("validations.location.toggle_date.not_selected")) - elsif record.reactivation_date_type == "other" - record.errors.add(:reactivation_date, message: I18n.t("validations.location.toggle_date.invalid")) - end - elsif !record.reactivation_date.between?(location.available_from, Time.zone.local(2200, 1, 1)) + if recent_deactivation && record.reactivation_date.present? + if !record.reactivation_date.between?(location.available_from, Time.zone.local(2200, 1, 1)) record.errors.add(:reactivation_date, message: I18n.t("validations.location.toggle_date.out_of_range", date: location.available_from.to_formatted_s(:govuk_date))) elsif record.reactivation_date < recent_deactivation.deactivation_date record.errors.add(:reactivation_date, message: I18n.t("validations.location.reactivation.before_deactivation", date: recent_deactivation.deactivation_date.to_formatted_s(:govuk_date))) diff --git a/spec/requests/locations_controller_spec.rb b/spec/requests/locations_controller_spec.rb index c54c53033..237149f7d 100644 --- a/spec/requests/locations_controller_spec.rb +++ b/spec/requests/locations_controller_spec.rb @@ -1277,7 +1277,7 @@ RSpec.describe LocationsController, type: :request do end context "when confirming deactivation" do - let(:params) { { location_deactivation_period: { deactivation_date:, confirm: true, deactivation_date_type: "other" } } } + let(:params) { { deactivation_date:, confirm: true, deactivation_date_type: "other" } } before do Timecop.freeze(Time.utc(2022, 10, 10))