Browse Source

update location in same way as scheme

pull/1396/head
Arthur Campbell 3 years ago
parent
commit
5a525d9373
  1. 12
      app/models/location_deactivation_period.rb
  2. 2
      app/views/locations/toggle_active.html.erb
  3. 1
      config/locales/en.yml

12
app/models/location_deactivation_period.rb

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

2
app/views/locations/toggle_active.html.erb

@ -20,7 +20,7 @@
<%= govuk_warning_text text: I18n.t("warnings.location.#{action}.existing_logs") %> <%= govuk_warning_text text: I18n.t("warnings.location.#{action}.existing_logs") %>
<%= f.govuk_radio_button date_type_question(action), <%= f.govuk_radio_button date_type_question(action),
"default", "default",
label: { text: "From the start of the current collection period (#{collection_start_date.to_formatted_s(:govuk_date)})" } %> label: { text: "From the start of the open collection period (#{collection_start_date.to_formatted_s(:govuk_date)})" } %>
<%= f.govuk_radio_button date_type_question(action), <%= f.govuk_radio_button date_type_question(action),
"other", "other",

1
config/locales/en.yml

@ -470,6 +470,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 location 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 location was deactivated on %{date}. The reactivation date must be on or after deactivation date" before_deactivation: "This location was deactivated on %{date}. The reactivation date must be on or after deactivation date"

Loading…
Cancel
Save