From 46c879a42fe97934c5456afb38293e070b679a35 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Tue, 28 Jun 2022 15:22:25 +0100 Subject: [PATCH] Search schemes by postcode (#691) * Search by postcode * remove the space removal for postcodes as we expect them to be saved without spaces * lint --- app/models/scheme.rb | 3 ++- spec/factories/location.rb | 2 +- spec/models/scheme_spec.rb | 15 ++++++++++++++- spec/requests/organisations_controller_spec.rb | 2 ++ spec/requests/schemes_controller_spec.rb | 1 + 5 files changed, 20 insertions(+), 3 deletions(-) diff --git a/app/models/scheme.rb b/app/models/scheme.rb index 625ea28f1..90f027f2f 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -5,7 +5,8 @@ class Scheme < ApplicationRecord scope :search_by_code, ->(code) { where("code ILIKE ?", "%#{code}%") } scope :search_by_service_name, ->(name) { where("service_name ILIKE ?", "%#{name}%") } - scope :search_by, ->(param) { search_by_code(param).or(search_by_service_name(param)) } + scope :search_by_postcode, ->(postcode) { joins(:locations).where("locations.postcode ILIKE ?", "%#{postcode.delete(' ')}%") } + scope :search_by, ->(param) { search_by_postcode(param).or(search_by_service_name(param)).or(search_by_code(param)).distinct } SCHEME_TYPE = { 0 => "Missings", diff --git a/spec/factories/location.rb b/spec/factories/location.rb index 7afaeb11c..e4eaf43a0 100644 --- a/spec/factories/location.rb +++ b/spec/factories/location.rb @@ -1,7 +1,7 @@ FactoryBot.define do factory :location do location_code { Faker::Name.initials(number: 10) } - postcode { Faker::Address.postcode } + postcode { Faker::Address.postcode.delete(" ") } address_line1 { Faker::Address.street_name } address_line2 { Faker::Address.city } type_of_unit { Faker::Lorem.word } diff --git a/spec/models/scheme_spec.rb b/spec/models/scheme_spec.rb index 27221838d..d6e39ae2e 100644 --- a/spec/models/scheme_spec.rb +++ b/spec/models/scheme_spec.rb @@ -11,6 +11,8 @@ RSpec.describe Scheme, type: :model do describe "scopes" do let!(:scheme_1) { FactoryBot.create(:scheme) } let!(:scheme_2) { FactoryBot.create(:scheme) } + let!(:location) { FactoryBot.create(:location, scheme: scheme_1) } + let!(:location_2) { FactoryBot.create(:location, scheme: scheme_2) } context "when searching by code" do it "returns case insensitive matching records" do @@ -34,7 +36,18 @@ RSpec.describe Scheme, type: :model do end end - context "when searching by all searchable field" do + context "when searching by postcode" do + it "returns case insensitive matching records" do + expect(described_class.search_by_postcode(location.postcode.upcase).count).to eq(1) + expect(described_class.search_by_postcode(location.postcode.downcase).count).to eq(1) + expect(described_class.search_by_postcode(location.postcode.downcase).first.locations.first.postcode).to eq(location.postcode) + expect(described_class.search_by_postcode(location_2.postcode.upcase).count).to eq(1) + expect(described_class.search_by_postcode(location_2.postcode.downcase).count).to eq(1) + expect(described_class.search_by_postcode(location_2.postcode.downcase).first.locations.first.postcode).to eq(location_2.postcode) + end + end + + context "when searching by all searchable fields" do it "returns case insensitive matching records" do expect(described_class.search_by(scheme_1.code.upcase).count).to eq(1) expect(described_class.search_by(scheme_1.code.downcase).count).to eq(1) diff --git a/spec/requests/organisations_controller_spec.rb b/spec/requests/organisations_controller_spec.rb index 35fc98d35..a929f9304 100644 --- a/spec/requests/organisations_controller_spec.rb +++ b/spec/requests/organisations_controller_spec.rb @@ -76,6 +76,7 @@ RSpec.describe OrganisationsController, type: :request do let(:search_param) { "CODE321" } before do + FactoryBot.create(:location, scheme: searched_scheme) allow(user).to receive(:need_two_factor_authentication?).and_return(false) get "/organisations/#{organisation.id}/schemes?search=#{search_param}" end @@ -144,6 +145,7 @@ RSpec.describe OrganisationsController, type: :request do let(:search_param) { "CODE321" } before do + FactoryBot.create(:location, scheme: searched_scheme) get "/organisations/#{organisation.id}/schemes?search=#{search_param}" end diff --git a/spec/requests/schemes_controller_spec.rb b/spec/requests/schemes_controller_spec.rb index 912913b1f..be17ec13e 100644 --- a/spec/requests/schemes_controller_spec.rb +++ b/spec/requests/schemes_controller_spec.rb @@ -140,6 +140,7 @@ RSpec.describe SchemesController, type: :request do let(:search_param) { "CODE321" } before do + FactoryBot.create(:location, scheme: searched_scheme) get "/schemes?search=#{search_param}" end