From c62ead1edb339756975c0ee58fd0f40cf461b5be Mon Sep 17 00:00:00 2001 From: Arthur Campbell Date: Mon, 13 Mar 2023 14:13:00 +0000 Subject: [PATCH] use the existing method and update validations --- app/models/scheme_deactivation_period.rb | 12 ++++++++---- config/locales/en.yml | 1 + 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/app/models/scheme_deactivation_period.rb b/app/models/scheme_deactivation_period.rb index f716cbc32..e21122943 100644 --- a/app/models/scheme_deactivation_period.rb +++ b/app/models/scheme_deactivation_period.rb @@ -1,4 +1,6 @@ class SchemeDeactivationPeriodValidator < ActiveModel::Validator + include CollectionTimeHelper + def validate(record) scheme = record.scheme recent_deactivation = scheme.scheme_deactivation_periods.deactivations_without_reactivation.first @@ -24,6 +26,8 @@ class SchemeDeactivationPeriodValidator < ActiveModel::Validator end def validate_deactivation(record, scheme) + earliest_possible_deactivation = FormHandler.instance.in_crossover_period? ? previous_collection_start_date : current_collection_start_date + if record.deactivation_date.blank? if record.deactivation_date_type.blank? record.errors.add(:deactivation_date_type, message: I18n.t("validations.scheme.toggle_date.not_selected")) @@ -32,10 +36,10 @@ class SchemeDeactivationPeriodValidator < ActiveModel::Validator end elsif scheme.scheme_deactivation_periods.any? { |period| period.reactivation_date.present? && record.deactivation_date.between?(period.deactivation_date, period.reactivation_date - 1.day) } record.errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation.during_deactivated_period")) - else - unless record.deactivation_date.between?(scheme.available_from, Time.zone.local(2200, 1, 1)) - record.errors.add(:deactivation_date, message: I18n.t("validations.scheme.toggle_date.out_of_range", date: scheme.available_from.to_formatted_s(:govuk_date))) - end + elsif record.deactivation_date.before? earliest_possible_deactivation + record.errors.add(:deactivation_date, message: I18n.t("validations.scheme.toggle_date.out_of_range", date: earliest_possible_deactivation.to_formatted_s(:govuk_date))) + elsif record.deactivation_date.before? scheme.available_from + record.errors.add(:deactivation_date, message: I18n.t("validations.scheme.toggle_date.before_creation", date: scheme.available_from.to_formatted_s(:govuk_date))) end end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 04ff5a2b5..b691d61a7 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -451,6 +451,7 @@ en: toggle_date: not_selected: "Select one of the options" invalid: "Enter a valid day, month and year" + before_creation: "The scheme cannot be deactivated before %{date}, the start of the collection year when it was created" out_of_range: "The date must be on or after the %{date}" reactivation: before_deactivation: "This scheme was deactivated on %{date}. The reactivation date must be on or after deactivation date"