diff --git a/app/models/location.rb b/app/models/location.rb index 0f5616c16..0e576afe7 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -220,8 +220,8 @@ class Location < ApplicationRecord location_deactivation_periods.deactivations_without_reactivation.first end - def last_deactivation_before(date) - location_deactivation_periods.where("deactivation_date <= ?", date).order("created_at").last + def soonest_reactivation(date) + location_deactivation_periods.deactivations_with_reactivation.where(date < reactivation_date).order(reactivation_date).first end def status diff --git a/app/models/scheme.rb b/app/models/scheme.rb index ae772b30b..f1a2b6907 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -315,8 +315,8 @@ class Scheme < ApplicationRecord scheme_deactivation_periods.deactivations_without_reactivation.first end - def last_deactivation_before(date) - scheme_deactivation_periods.where("deactivation_date <= ?", date).order("created_at").last + def soonest_reactivation(date) + scheme_deactivation_periods.deactivations_with_reactivation.where(date < reactivation_date).order(reactivation_date).first end def last_deactivation_date diff --git a/app/models/validations/shared_validations.rb b/app/models/validations/shared_validations.rb index 6f68f788c..f4201ebcb 100644 --- a/app/models/validations/shared_validations.rb +++ b/app/models/validations/shared_validations.rb @@ -73,7 +73,7 @@ module Validations::SharedValidations status = resource.status_at(date) return unless %i[reactivating_soon activating_soon deactivated].include?(status) - closest_reactivation = resource.last_deactivation_before(date) + closest_reactivation = resource.soonest_reactivation(date) open_deactivation = if resource.is_a?(Location) resource.open_deactivation || resource.scheme.open_deactivation else