Browse Source

Use scope to filter location instead

pull/2510/head
Manny Dinssa 2 years ago
parent
commit
c2884ef5d0
  1. 24
      app/models/location.rb
  2. 2
      app/models/scheme.rb

24
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

2
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?

Loading…
Cancel
Save