Browse Source

Update incomplete, make reactivated scope work in conjunction with other scopes. Lint

pull/1785/head
Kat 3 years ago
parent
commit
74bfd49352
  1. 48
      app/models/location.rb
  2. 30
      spec/models/location_spec.rb

48
app/models/location.rb

@ -27,51 +27,55 @@ class Location < ApplicationRecord
scope :active_in_2_weeks, -> { where(confirmed: true).and(started_in_2_weeks) } scope :active_in_2_weeks, -> { where(confirmed: true).and(started_in_2_weeks) }
scope :confirmed, -> { where(confirmed: true) } scope :confirmed, -> { where(confirmed: true) }
scope :unconfirmed, -> { where.not(confirmed: true) } scope :unconfirmed, -> { where.not(confirmed: true) }
scope :filter_by_status, ->(statuses, _user = nil) { scope :filter_by_status, lambda { |statuses, _user = nil|
filtered_records = all filtered_records = all
scopes = [] scopes = []
statuses.each do |status| statuses.each do |status|
if respond_to?(status, true) if respond_to?(status, true)
status == "active" ? scopes << send("active_status") : scopes << send(status) scopes << (status == "active" ? send("active_status") : send(status))
end end
end end
filtered_records = filtered_records.left_outer_joins(:location_deactivation_periods).merge(scopes.reduce(&:or)) if scopes.any? if scopes.any?
filtered_records = filtered_records
.left_outer_joins(:location_deactivation_periods)
.order("location_deactivation_periods.created_at DESC")
.merge(scopes.reduce(&:or))
end
filtered_records filtered_records
} }
scope :incomplete, -> { scope :incomplete, lambda {
where(confirmed: false) where.not(confirmed: true)
} }
scope :deactivated, -> { scope :deactivated, lambda {
merge(LocationDeactivationPeriod.deactivations_without_reactivation) merge(LocationDeactivationPeriod.deactivations_without_reactivation)
.where("location_deactivation_periods.deactivation_date <= ?", Time.zone.now) .where("location_deactivation_periods.deactivation_date <= ?", Time.zone.now)
} }
scope :deactivating_soon, -> { scope :deactivating_soon, lambda {
merge(LocationDeactivationPeriod.deactivations_without_reactivation) merge(LocationDeactivationPeriod.deactivations_without_reactivation)
.where("location_deactivation_periods.deactivation_date > ?", Time.zone.now) .where("location_deactivation_periods.deactivation_date > ?", Time.zone.now)
} }
scope :reactivating_soon, -> { scope :reactivating_soon, lambda {
where.not("location_deactivation_periods.reactivation_date IS NULL") where.not("location_deactivation_periods.reactivation_date IS NULL")
.order("location_deactivation_periods.created_at DESC") .where("location_deactivation_periods.reactivation_date > ?", Time.zone.now)
.where("location_deactivation_periods.reactivation_date > ?", Time.zone.now)
} }
scope :activating_soon, -> { scope :activating_soon, lambda {
where("startdate > ?", Time.zone.now) where("startdate > ?", Time.zone.now)
} }
scope :active_status, -> { scope :active_status, lambda {
where.not(id: joins(:location_deactivation_periods).reactivating_soon.pluck(:id)) 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(:location_deactivation_periods).deactivated.pluck(:id))
.where.not(id: incomplete.pluck(:id)) .where.not(id: incomplete.pluck(:id))
.where.not(id: joins(:location_deactivation_periods).deactivating_soon.pluck(:id)) .where.not(id: joins(:location_deactivation_periods).deactivating_soon.pluck(:id))
.where.not(id: activating_soon.pluck(:id)) .where.not(id: activating_soon.pluck(:id))
} }
LOCAL_AUTHORITIES = LocalAuthority.all.map { |la| [la.name, la.code] }.to_h LOCAL_AUTHORITIES = LocalAuthority.all.map { |la| [la.name, la.code] }.to_h

30
spec/models/location_spec.rb

@ -954,51 +954,51 @@ RSpec.describe Location, type: :model do
context "when filtering by incomplete status" do context "when filtering by incomplete status" do
it "returns only incomplete locations" do it "returns only incomplete locations" do
expect(described_class.filter_by_status(["incomplete"]).count).to eq(1) expect(described_class.filter_by_status(%w[incomplete]).count).to eq(1)
expect(described_class.filter_by_status(["incomplete"]).first).to eq(incomplete_location) expect(described_class.filter_by_status(%w[incomplete]).first).to eq(incomplete_location)
end end
end end
context "when filtering by active status" do context "when filtering by active status" do
it "returns only active locations" do it "returns only active locations" do
expect(described_class.filter_by_status(["active"]).count).to eq(1) expect(described_class.filter_by_status(%w[active]).count).to eq(1)
expect(described_class.filter_by_status(["active"]).first).to eq(active_location) expect(described_class.filter_by_status(%w[active]).first).to eq(active_location)
end end
end end
context "when filtering by deactivating_soon status" do context "when filtering by deactivating_soon status" do
it "returns only deactivating_soon locations" do it "returns only deactivating_soon locations" do
expect(described_class.filter_by_status(["deactivating_soon"]).count).to eq(1) expect(described_class.filter_by_status(%w[deactivating_soon]).count).to eq(1)
expect(described_class.filter_by_status(["deactivating_soon"]).first).to eq(deactivating_soon_location) expect(described_class.filter_by_status(%w[deactivating_soon]).first).to eq(deactivating_soon_location)
end end
end end
context "when filtering by deactivated status" do context "when filtering by deactivated status" do
it "returns only deactivated locations" do it "returns only deactivated locations" do
expect(described_class.filter_by_status(["deactivated"]).count).to eq(1) expect(described_class.filter_by_status(%w[deactivated]).count).to eq(1)
expect(described_class.filter_by_status(["deactivated"]).first).to eq(deactivated_location) expect(described_class.filter_by_status(%w[deactivated]).first).to eq(deactivated_location)
end end
end end
context "when filtering by reactivating_soon status" do context "when filtering by reactivating_soon status" do
it "returns only reactivating_soon locations" do it "returns only reactivating_soon locations" do
expect(described_class.filter_by_status(["reactivating_soon"]).count).to eq(1) expect(described_class.filter_by_status(%w[reactivating_soon]).count).to eq(1)
expect(described_class.filter_by_status(["reactivating_soon"]).first).to eq(reactivating_soon_location) expect(described_class.filter_by_status(%w[reactivating_soon]).first).to eq(reactivating_soon_location)
end end
end end
context "when filtering by activating_soon status" do context "when filtering by activating_soon status" do
it "returns only activating_soon locations" do it "returns only activating_soon locations" do
expect(described_class.filter_by_status(["activating_soon"]).count).to eq(1) expect(described_class.filter_by_status(%w[activating_soon]).count).to eq(1)
expect(described_class.filter_by_status(["activating_soon"]).first).to eq(activating_soon_location) expect(described_class.filter_by_status(%w[activating_soon]).first).to eq(activating_soon_location)
end end
end end
context "when filtering by multiple statuses" do context "when filtering by multiple statuses" do
it "returns only activating_soon locations" do it "returns only activating_soon locations" do
expect(described_class.filter_by_status(["deactivating_soon", "activating_soon"]).count).to eq(2) expect(described_class.filter_by_status(%w[deactivating_soon activating_soon]).count).to eq(2)
expect(described_class.filter_by_status(["deactivating_soon", "activating_soon"])).to include(activating_soon_location) expect(described_class.filter_by_status(%w[deactivating_soon activating_soon])).to include(activating_soon_location)
expect(described_class.filter_by_status(["deactivating_soon", "activating_soon"])).to include(deactivating_soon_location) expect(described_class.filter_by_status(%w[deactivating_soon activating_soon])).to include(deactivating_soon_location)
end end
end end
end end

Loading…
Cancel
Save