Browse Source

Update location scopes (#2537)

pull/2536/head^2 v0.4.62
kosiakkatrina 5 months ago committed by GitHub
parent
commit
ba2cf8b4a9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 4
      app/models/location.rb
  2. 7
      spec/models/location_spec.rb

4
app/models/location.rb

@ -18,8 +18,8 @@ class Location < ApplicationRecord
auto_strip_attributes :name, squish: true auto_strip_attributes :name, squish: true
scope :search_by_postcode, ->(postcode) { where("REPLACE(postcode, ' ', '') ILIKE ?", "%#{postcode.delete(' ')}%") } scope :search_by_postcode, ->(postcode) { where("REPLACE(locations.postcode, ' ', '') ILIKE ?", "%#{postcode.delete(' ')}%") }
scope :search_by_name, ->(name) { where("name ILIKE ?", "%#{name}%") } scope :search_by_name, ->(name) { where("locations.name ILIKE ?", "%#{name}%") }
scope :search_by, ->(param) { search_by_name(param).or(search_by_postcode(param)) } scope :search_by, ->(param) { search_by_name(param).or(search_by_postcode(param)) }
scope :started, -> { where("locations.startdate <= ?", Time.zone.today).or(where(startdate: nil)) } scope :started, -> { where("locations.startdate <= ?", Time.zone.today).or(where(startdate: nil)) }
scope :started_in_2_weeks, -> { where("locations.startdate <= ?", Time.zone.today + 2.weeks).or(where(startdate: nil)) } scope :started_in_2_weeks, -> { where("locations.startdate <= ?", Time.zone.today + 2.weeks).or(where(startdate: nil)) }

7
spec/models/location_spec.rb

@ -1019,6 +1019,13 @@ RSpec.describe Location, type: :model do
expect(described_class.filter_by_status(%w[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
context "when filtering by a status and searching" do
it "returns only active locations" do
expect(described_class.filter_by_status(%w[active]).search_by(active_location.name).count).to eq(1)
expect(described_class.filter_by_status(%w[active]).search_by(active_location.name).first).to eq(active_location)
end
end
end end
describe "available_from" do describe "available_from" do

Loading…
Cancel
Save