From 20e0715db98b05101e2706b0e828bf5cb28a62b2 Mon Sep 17 00:00:00 2001 From: Manny Dinssa <44172848+Dinssa@users.noreply.github.com> Date: Wed, 17 Jul 2024 11:59:40 +0100 Subject: [PATCH] Consolidate tenancy validation functionality into scheme validation --- app/models/scheme.rb | 2 +- app/models/validations/shared_validations.rb | 11 ++--------- config/locales/en.yml | 1 + 3 files changed, 4 insertions(+), 10 deletions(-) diff --git a/app/models/scheme.rb b/app/models/scheme.rb index 342327636..7ae492c9f 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -299,7 +299,7 @@ class Scheme < ApplicationRecord active? && locations.active.exists? end - def any_location_active_on_date?(date) + def has_active_locations_on_date?(date) return false unless date locations.any? { |location| date && location.active_on_date?(date) } diff --git a/app/models/validations/shared_validations.rb b/app/models/validations/shared_validations.rb index fb9a85d5f..98fce62da 100644 --- a/app/models/validations/shared_validations.rb +++ b/app/models/validations/shared_validations.rb @@ -82,16 +82,9 @@ module Validations::SharedValidations record.errors.add :startdate, I18n.t("validations.setup.startdate.scheme.#{scope}.startdate", name: record.scheme.service_name, date:, deactivation_date:) record.errors.add :scheme_id, I18n.t("validations.setup.startdate.scheme.#{scope}.scheme_id", name: record.scheme.service_name, date:, deactivation_date:) end - end - - def tenancy_during_startdate_validation(record) - return if record.startdate.blank? - - if record.scheme.present? && !record.scheme.any_location_active_on_date?(record.startdate) + if record.scheme.present? && !record.scheme.has_active_locations_on_date?(record.startdate) record.errors.add :startdate, I18n.t("validations.setup.startdate.scheme.locations_inactive.startdate", name: record.scheme.service_name) - end - if record.location.present? && !record.location.active_on_date?(record.startdate) - record.errors.add :startdate, I18n.t("validations.setup.startdate.location.deactivated.startdate", postcode: record.location.postcode) + record.errors.add :scheme_id, I18n.t("validations.setup.startdate.scheme.locations_inactive.scheme_id", name: record.scheme.service_name) end end diff --git a/config/locales/en.yml b/config/locales/en.yml index b807dea96..8ece9f7ab 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -302,6 +302,7 @@ en: scheme_id: "The scheme %{name} is not available until %{date}. Select another scheme or edit the tenancy start date" locations_inactive: startdate: "The scheme %{name} has no locations that are active on this date. Enter another date or choose another scheme." + scheme_id: "The scheme %{name} has no locations that are active on this date. Enter another date or choose another scheme." owning_organisation: invalid: "Please select the owning organisation or managing organisation that you belong to" data_sharing_agreement_not_signed: "The organisation must accept the Data Sharing Agreement before it can be selected as the owning organisation."