Browse Source

CLDC-2481: Fix location and scheme filters

pull/2293/head
Robert Sullivan 2 years ago committed by Kat
parent
commit
0c86afb5bc
  1. 18
      app/models/location.rb
  2. 1
      app/models/organisation.rb
  3. 21
      app/models/scheme.rb
  4. 8
      spec/models/location_spec.rb
  5. 5
      spec/models/scheme_spec.rb

18
app/models/location.rb

@ -40,6 +40,7 @@ class Location < ApplicationRecord
if scopes.any?
filtered_records = filtered_records
.left_outer_joins(:location_deactivation_periods)
.joins(scheme: [:owning_organisation])
.order("location_deactivation_periods.created_at DESC")
.merge(scopes.reduce(&:or))
end
@ -53,28 +54,39 @@ class Location < ApplicationRecord
}
scope :deactivated, lambda {
deactivated_by_organisation
.or(deactivated_directly)
}
scope :deactivated_by_organisation, lambda {
merge(Organisation.filter_by_inactive)
}
scope :deactivated_directly, lambda {
merge(LocationDeactivationPeriod.deactivations_without_reactivation)
.where("location_deactivation_periods.deactivation_date <= ?", Time.zone.now)
.or("location.organisation.active", false)
}
scope :deactivating_soon, lambda {
merge(LocationDeactivationPeriod.deactivations_without_reactivation)
.where("location_deactivation_periods.deactivation_date > ?", Time.zone.now)
.where.not(id: joins(scheme: [:owning_organisation]).deactivated_by_organisation.pluck(:id))
}
scope :reactivating_soon, lambda {
where.not("location_deactivation_periods.reactivation_date IS NULL")
.where("location_deactivation_periods.reactivation_date > ?", Time.zone.now)
.where.not(id: joins(scheme: [:owning_organisation]).deactivated_by_organisation.pluck(:id))
}
scope :activating_soon, lambda {
where("startdate > ?", Time.zone.now)
where("locations.startdate > ?", Time.zone.now)
}
scope :active_status, lambda {
where.not(id: joins(:location_deactivation_periods).reactivating_soon.pluck(:id))
.where.not(id: joins(:location_deactivation_periods).deactivated.pluck(:id))
.where.not(id: joins(scheme: [:owning_organisation]).deactivated_by_organisation.pluck(:id))
.where.not(id: joins(:location_deactivation_periods).deactivated_directly.pluck(:id))
.where.not(id: incomplete.pluck(:id))
.where.not(id: joins(:location_deactivation_periods).deactivating_soon.pluck(:id))
.where.not(id: activating_soon.pluck(:id))

1
app/models/organisation.rb

@ -38,6 +38,7 @@ class Organisation < ApplicationRecord
scope :search_by_name, ->(name) { where("name ILIKE ?", "%#{name}%") }
scope :search_by, ->(param) { search_by_name(param) }
scope :filter_by_active, -> { where(active: true) }
scope :filter_by_inactive, -> { where(active: false) }
scope :merged_during_open_collection_period, -> { where("merge_date >= ?", FormHandler.instance.start_date_of_earliest_open_for_editing_collection_period) }
has_paper_trail

21
app/models/scheme.rb

@ -35,6 +35,7 @@ class Scheme < ApplicationRecord
if scopes.any?
filtered_records = filtered_records
.left_outer_joins(:scheme_deactivation_periods)
.joins(:owning_organisation)
.merge(scopes.reduce(&:or))
end
@ -45,36 +46,48 @@ class Scheme < ApplicationRecord
where.not(confirmed: true)
.or(where(confirmed: nil))
.or(where.not(id: Location.select(:scheme_id).where(confirmed: true).distinct))
.where.not(id: joins(:owning_organisation).deactivated_by_organisation.pluck(:id))
.where.not(id: joins(:scheme_deactivation_periods).deactivated_directly.pluck(:id))
.where.not(id: joins(:scheme_deactivation_periods).reactivating_soon.pluck(:id))
.where.not(id: joins(:scheme_deactivation_periods).deactivated.pluck(:id))
.where.not(id: joins(:scheme_deactivation_periods).deactivating_soon.pluck(:id))
}
scope :deactivated, lambda {
deactivated_by_organisation
.or(deactivated_directly)
}
scope :deactivated_by_organisation, lambda {
merge(Organisation.filter_by_inactive)
}
scope :deactivated_directly, lambda {
merge(SchemeDeactivationPeriod.deactivations_without_reactivation)
.where("scheme_deactivation_periods.deactivation_date <= ?", Time.zone.now)
.or("scheme.organisation.active", false)
}
scope :deactivating_soon, lambda {
merge(SchemeDeactivationPeriod.deactivations_without_reactivation)
.where("scheme_deactivation_periods.deactivation_date > ? AND scheme_deactivation_periods.deactivation_date < ? ", Time.zone.now, 6.months.from_now)
.where.not(id: joins(:owning_organisation).deactivated_by_organisation.pluck(:id))
}
scope :reactivating_soon, lambda {
where.not("scheme_deactivation_periods.reactivation_date IS NULL")
.where("scheme_deactivation_periods.reactivation_date > ?", Time.zone.now)
.where.not(id: joins(:owning_organisation).deactivated_by_organisation.pluck(:id))
}
scope :activating_soon, lambda {
where("startdate > ?", Time.zone.now)
where("schemes.startdate > ?", Time.zone.now)
}
scope :active_status, lambda {
where.not(id: joins(:scheme_deactivation_periods).reactivating_soon.pluck(:id))
.where.not(id: joins(:scheme_deactivation_periods).deactivated.pluck(:id))
.where.not(id: incomplete.pluck(:id))
.where.not(id: joins(:scheme_deactivation_periods).deactivating_soon.pluck(:id))
.where.not(id: joins(:owning_organisation).deactivated_by_organisation.pluck(:id))
.where.not(id: joins(:owning_organisation).joins(:scheme_deactivation_periods).deactivated_directly.pluck(:id))
.where.not(id: activating_soon.pluck(:id))
}

8
spec/models/location_spec.rb

@ -974,6 +974,9 @@ RSpec.describe Location, type: :model do
end
describe "filter by status" do
let!(:deactivated_organisation) { FactoryBot.create(:organisation, active: false) }
let!(:deactivated_by_organisation_scheme) { FactoryBot.create(:scheme, owning_organisation: deactivated_organisation) }
let!(:deactivated_by_organisation_location) { FactoryBot.create(:location, scheme: deactivated_by_organisation_scheme) }
let!(:incomplete_location) { FactoryBot.create(:location, :incomplete, startdate: Time.zone.local(2022, 4, 1)) }
let!(:incomplete_location_with_nil_confirmed) { FactoryBot.create(:location, :incomplete, startdate: Time.zone.local(2022, 4, 1), confirmed: nil) }
let!(:active_location) { FactoryBot.create(:location, startdate: Time.zone.local(2022, 4, 1)) }
@ -1020,8 +1023,9 @@ RSpec.describe Location, type: :model do
context "when filtering by deactivated status" do
it "returns only deactivated locations" do
expect(described_class.filter_by_status(%w[deactivated]).count).to eq(1)
expect(described_class.filter_by_status(%w[deactivated]).first).to eq(deactivated_location)
expect(described_class.filter_by_status(%w[deactivated]).count).to eq(2)
expect(described_class.filter_by_status(%w[deactivated])).to include(deactivated_location)
expect(described_class.filter_by_status(%w[deactivated])).to include(deactivated_by_organisation_location)
end
end

5
spec/models/scheme_spec.rb

@ -112,11 +112,13 @@ RSpec.describe Scheme, type: :model do
end
context "when filtering by status" do
let!(:deactivated_organisation) { FactoryBot.create(:organisation, active: false) }
let!(:incomplete_scheme) { FactoryBot.create(:scheme, :incomplete, service_name: "name") }
let!(:incomplete_scheme_2) { FactoryBot.create(:scheme, :incomplete, service_name: "name") }
let!(:incomplete_scheme_with_nil_confirmed) { FactoryBot.create(:scheme, :incomplete, service_name: "name", confirmed: nil) }
let(:active_scheme) { FactoryBot.create(:scheme) }
let(:active_scheme_2) { FactoryBot.create(:scheme) }
let!(:deactivated_by_organisation_scheme) { FactoryBot.create(:scheme, owning_organisation: deactivated_organisation) }
let(:deactivating_soon_scheme) { FactoryBot.create(:scheme) }
let(:deactivating_soon_scheme_2) { FactoryBot.create(:scheme) }
let(:deactivated_scheme) { FactoryBot.create(:scheme) }
@ -181,9 +183,10 @@ RSpec.describe Scheme, type: :model do
context "when filtering by deactivated status" do
it "returns only deactivated schemes" do
expect(described_class.filter_by_status(%w[deactivated]).count).to eq(2)
expect(described_class.filter_by_status(%w[deactivated]).count).to eq(3)
expect(described_class.filter_by_status(%w[deactivated])).to include(deactivated_scheme)
expect(described_class.filter_by_status(%w[deactivated])).to include(deactivated_scheme_2)
expect(described_class.filter_by_status(%w[deactivated])).to include(deactivated_by_organisation_scheme)
end
end

Loading…
Cancel
Save