diff --git a/app/models/location.rb b/app/models/location.rb index e75b81423..9d6bb2f0d 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -62,25 +62,25 @@ class Location < ApplicationRecord merge(Organisation.filter_by_inactive) } - scope :deactivated_directly, lambda { + scope :deactivated_directly, lambda { |date = Time.zone.now| merge(LocationDeactivationPeriod.deactivations_without_reactivation) - .where("location_deactivation_periods.deactivation_date <= ?", Time.zone.now) + .where("location_deactivation_periods.deactivation_date <= ?", date) } - scope :deactivating_soon, lambda { + scope :deactivating_soon, lambda { |date = Time.zone.now| merge(LocationDeactivationPeriod.deactivations_without_reactivation) - .where("location_deactivation_periods.deactivation_date > ?", Time.zone.now) + .where("location_deactivation_periods.deactivation_date > ?", date) .where.not(id: joins(scheme: [:owning_organisation]).deactivated_by_organisation.pluck(:id)) } - scope :reactivating_soon, lambda { + scope :reactivating_soon, lambda { |date = Time.zone.now| where.not("location_deactivation_periods.reactivation_date IS NULL") - .where("location_deactivation_periods.reactivation_date > ?", Time.zone.now) + .where("location_deactivation_periods.reactivation_date > ?", date) .where.not(id: joins(scheme: [:owning_organisation]).deactivated_by_organisation.pluck(:id)) } - scope :activating_soon, lambda { - where("locations.startdate > ?", Time.zone.now) + scope :activating_soon, lambda { |date = Time.zone.now| + where("locations.startdate > ?", date) } scope :active_status, lambda { @@ -92,6 +92,14 @@ class Location < ApplicationRecord .where.not(id: activating_soon.pluck(:id)) } + scope :active_on_date, lambda { |date = Time.zone.now| + where.not(id: joins(:location_deactivation_periods).reactivating_soon(date).pluck(:id)) + .where.not(id: joins(scheme: [:owning_organisation]).deactivated_by_organisation.pluck(:id)) + .where.not(id: joins(:location_deactivation_periods).deactivated_directly(date).pluck(:id)) + .where.not(id: incomplete.pluck(:id)) + .where.not(id: activating_soon(date).pluck(:id)) + } + scope :visible, -> { where(discarded_at: nil) } LOCAL_AUTHORITIES = LocalAuthority.all.map { |la| [la.name, la.code] }.to_h diff --git a/app/models/scheme.rb b/app/models/scheme.rb index 2224ef0e2..ff10c33c2 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -302,7 +302,7 @@ class Scheme < ApplicationRecord def has_active_locations_on_date?(date) return false unless date - locations.any? { |location| location.active_on_date?(date) } + locations.active_on_date(date).exists? end def reactivating_soon?