Browse Source

use the existing method and update validations

pull/1396/head
Arthur Campbell 3 years ago
parent
commit
c62ead1edb
  1. 12
      app/models/scheme_deactivation_period.rb
  2. 1
      config/locales/en.yml

12
app/models/scheme_deactivation_period.rb

@ -1,4 +1,6 @@
class SchemeDeactivationPeriodValidator < ActiveModel::Validator class SchemeDeactivationPeriodValidator < ActiveModel::Validator
include CollectionTimeHelper
def validate(record) def validate(record)
scheme = record.scheme scheme = record.scheme
recent_deactivation = scheme.scheme_deactivation_periods.deactivations_without_reactivation.first recent_deactivation = scheme.scheme_deactivation_periods.deactivations_without_reactivation.first
@ -24,6 +26,8 @@ class SchemeDeactivationPeriodValidator < ActiveModel::Validator
end end
def validate_deactivation(record, scheme) 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.blank?
if record.deactivation_date_type.blank? if record.deactivation_date_type.blank?
record.errors.add(:deactivation_date_type, message: I18n.t("validations.scheme.toggle_date.not_selected")) record.errors.add(:deactivation_date_type, message: I18n.t("validations.scheme.toggle_date.not_selected"))
@ -32,10 +36,10 @@ class SchemeDeactivationPeriodValidator < ActiveModel::Validator
end end
elsif scheme.scheme_deactivation_periods.any? { |period| period.reactivation_date.present? && record.deactivation_date.between?(period.deactivation_date, period.reactivation_date - 1.day) } 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")) record.errors.add(:deactivation_date, message: I18n.t("validations.scheme.deactivation.during_deactivated_period"))
else elsif record.deactivation_date.before? earliest_possible_deactivation
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: earliest_possible_deactivation.to_formatted_s(:govuk_date)))
record.errors.add(:deactivation_date, message: I18n.t("validations.scheme.toggle_date.out_of_range", date: scheme.available_from.to_formatted_s(:govuk_date))) elsif record.deactivation_date.before? scheme.available_from
end 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 end
end end

1
config/locales/en.yml

@ -451,6 +451,7 @@ en:
toggle_date: toggle_date:
not_selected: "Select one of the options" not_selected: "Select one of the options"
invalid: "Enter a valid day, month and year" 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}" out_of_range: "The date must be on or after the %{date}"
reactivation: reactivation:
before_deactivation: "This scheme was deactivated on %{date}. The reactivation date must be on or after deactivation date" before_deactivation: "This scheme was deactivated on %{date}. The reactivation date must be on or after deactivation date"

Loading…
Cancel
Save