From 8011301583b6235affee4529991bb4eaf4772bf5 Mon Sep 17 00:00:00 2001 From: Arthur Campbell Date: Mon, 13 Mar 2023 17:22:31 +0000 Subject: [PATCH] create method on FormHandler that finds the start date of the earliest collection period --- app/models/form_handler.rb | 4 ++++ app/models/location_deactivation_period.rb | 6 ++---- app/models/scheme_deactivation_period.rb | 6 ++---- app/views/locations/toggle_active.html.erb | 4 ++-- app/views/schemes/toggle_active.html.erb | 6 +++--- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/app/models/form_handler.rb b/app/models/form_handler.rb index ab22e6bc5..ee84c3e32 100644 --- a/app/models/form_handler.rb +++ b/app/models/form_handler.rb @@ -77,6 +77,10 @@ class FormHandler form_mappings[current_collection_start_year - year] end + def start_date_of_earliest_open_collection_period + in_crossover_period? ? previous_collection_start_date : current_collection_start_date + end + def in_crossover_period?(now: Time.zone.now) lettings_in_crossover_period?(now:) || sales_in_crossover_period?(now:) end diff --git a/app/models/location_deactivation_period.rb b/app/models/location_deactivation_period.rb index 58b694d72..5764829ed 100644 --- a/app/models/location_deactivation_period.rb +++ b/app/models/location_deactivation_period.rb @@ -26,8 +26,6 @@ class LocationDeactivationPeriodValidator < ActiveModel::Validator end 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_type.blank? record.errors.add(:deactivation_date_type, message: I18n.t("validations.location.toggle_date.not_selected")) @@ -36,8 +34,8 @@ class LocationDeactivationPeriodValidator < ActiveModel::Validator end 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")) - elsif record.deactivation_date.before? earliest_possible_deactivation - record.errors.add(:deactivation_date, message: I18n.t("validations.location.toggle_date.out_of_range", date: earliest_possible_deactivation.to_formatted_s(:govuk_date))) + elsif record.deactivation_date.before? FormHandler.instance.start_date_of_earliest_open_collection_period + record.errors.add(:deactivation_date, message: I18n.t("validations.location.toggle_date.out_of_range", date: FormHandler.instance.start_date_of_earliest_open_collection_period.to_formatted_s(:govuk_date))) elsif record.deactivation_date.before? location.available_from record.errors.add(:deactivation_date, message: I18n.t("validations.location.toggle_date.before_creation", date: location.available_from.to_formatted_s(:govuk_date))) end diff --git a/app/models/scheme_deactivation_period.rb b/app/models/scheme_deactivation_period.rb index e21122943..01aafbcb4 100644 --- a/app/models/scheme_deactivation_period.rb +++ b/app/models/scheme_deactivation_period.rb @@ -26,8 +26,6 @@ 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")) @@ -36,8 +34,8 @@ 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")) - 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? FormHandler.instance.start_date_of_earliest_open_collection_period + record.errors.add(:deactivation_date, message: I18n.t("validations.scheme.toggle_date.out_of_range", date: FormHandler.instance.start_date_of_earliest_open_collection_period.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 diff --git a/app/views/locations/toggle_active.html.erb b/app/views/locations/toggle_active.html.erb index 53cc0d1d8..9ef36cf81 100644 --- a/app/views/locations/toggle_active.html.erb +++ b/app/views/locations/toggle_active.html.erb @@ -16,11 +16,11 @@ <%= f.govuk_radio_buttons_fieldset date_type_question(action), legend: { text: I18n.t("questions.location.toggle_active.apply_from") }, caption: { text: title }, - hint: { text: I18n.t("hints.location.toggle_active", date: collection_start_date.to_formatted_s(:govuk_date)) } do %> + hint: { text: I18n.t("hints.location.toggle_active", date: start_date.to_formatted_s(:govuk_date)) } do %> <%= govuk_warning_text text: I18n.t("warnings.location.#{action}.existing_logs") %> <%= f.govuk_radio_button date_type_question(action), "default", - label: { text: "From the start of the open collection period (#{collection_start_date.to_formatted_s(:govuk_date)})" } %> + label: { text: "From the start of the open collection period (#{start_date.to_formatted_s(:govuk_date)})" } %> <%= f.govuk_radio_button date_type_question(action), "other", diff --git a/app/views/schemes/toggle_active.html.erb b/app/views/schemes/toggle_active.html.erb index 8ce3df8ae..f2ecc4a1a 100644 --- a/app/views/schemes/toggle_active.html.erb +++ b/app/views/schemes/toggle_active.html.erb @@ -11,16 +11,16 @@ <%= form_with model: @scheme_deactivation_period, url: toggle_scheme_form_path(action, @scheme), method: "patch", local: true do |f| %>
- <% collection_start_date = FormHandler.instance.in_crossover_period? ? previous_collection_start_date : current_collection_start_date %> + <% start_date = FormHandler.instance.start_date_of_earliest_open_collection_period %> <%= f.govuk_error_summary %> <%= f.govuk_radio_buttons_fieldset date_type_question(action), legend: { text: I18n.t("questions.scheme.toggle_active.apply_from") }, caption: { text: title }, - hint: { text: I18n.t("hints.scheme.toggle_active", date: collection_start_date.to_formatted_s(:govuk_date)) } do %> + hint: { text: I18n.t("hints.scheme.toggle_active", date: start_date.to_formatted_s(:govuk_date)) } do %> <%= govuk_warning_text text: I18n.t("warnings.scheme.#{action}.existing_logs") %> <%= f.govuk_radio_button date_type_question(action), "default", - label: { text: "From the start of the open collection period (#{collection_start_date.to_formatted_s(:govuk_date)})" } %> + label: { text: "From the start of the open collection period (#{start_date.to_formatted_s(:govuk_date)})" } %> <%= f.govuk_radio_button date_type_question(action), "other", label: { text: "For tenancies starting after a certain date" },