Browse Source

Make scheme search by code case insensitive (#2037)

pull/2045/head
kosiakkatrina 1 year ago committed by GitHub
parent
commit
190675749c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      app/models/scheme.rb
  2. 2
      spec/models/scheme_spec.rb

2
app/models/scheme.rb

@ -6,7 +6,7 @@ class Scheme < ApplicationRecord
has_paper_trail has_paper_trail
scope :filter_by_id, ->(id) { where(id: (id.start_with?("S") ? id[1..] : id)) } scope :filter_by_id, ->(id) { where(id: (id.start_with?("S", "s") ? id[1..] : id)) }
scope :search_by_service_name, ->(name) { where("service_name ILIKE ?", "%#{name}%") } scope :search_by_service_name, ->(name) { where("service_name ILIKE ?", "%#{name}%") }
scope :search_by_postcode, ->(postcode) { where("schemes.id IN (SELECT DISTINCT scheme_id FROM locations WHERE REPLACE(locations.postcode, ' ', '') ILIKE ?)", "%#{postcode.delete(' ')}%") } scope :search_by_postcode, ->(postcode) { where("schemes.id IN (SELECT DISTINCT scheme_id FROM locations WHERE REPLACE(locations.postcode, ' ', '') ILIKE ?)", "%#{postcode.delete(' ')}%") }
scope :search_by_location_name, ->(name) { where("schemes.id IN (SELECT DISTINCT scheme_id FROM locations WHERE locations.name ILIKE ?)", "%#{name}%") } scope :search_by_location_name, ->(name) { where("schemes.id IN (SELECT DISTINCT scheme_id FROM locations WHERE locations.name ILIKE ?)", "%#{name}%") }

2
spec/models/scheme_spec.rb

@ -77,6 +77,8 @@ RSpec.describe Scheme, type: :model do
it "returns case insensitive matching records" do it "returns case insensitive matching records" do
expect(described_class.search_by(scheme_1.id.to_s).count).to eq(1) expect(described_class.search_by(scheme_1.id.to_s).count).to eq(1)
expect(described_class.search_by("S#{scheme_1.id}").count).to eq(1)
expect(described_class.search_by("s#{scheme_1.id}").count).to eq(1)
expect(described_class.search_by(scheme_1.id.to_s).first.id).to eq(scheme_1.id) expect(described_class.search_by(scheme_1.id.to_s).first.id).to eq(scheme_1.id)
expect(described_class.search_by(scheme_2.service_name.upcase).count).to eq(1) expect(described_class.search_by(scheme_2.service_name.upcase).count).to eq(1)
expect(described_class.search_by(scheme_2.service_name.downcase).count).to eq(1) expect(described_class.search_by(scheme_2.service_name.downcase).count).to eq(1)

Loading…
Cancel
Save