From 7e3a1fb48344173e873bbf43fc8a2ff3688cce30 Mon Sep 17 00:00:00 2001 From: baarkerlounger <5101747+baarkerlounger@users.noreply.github.com> Date: Fri, 29 Jul 2022 08:21:46 +0100 Subject: [PATCH] Search schemes by location name (#791) * Search schemes by location name * Update search hint --- app/models/scheme.rb | 8 +++++++- app/views/organisations/schemes.html.erb | 2 +- app/views/schemes/index.html.erb | 2 +- spec/features/schemes_spec.rb | 4 ++-- spec/models/scheme_spec.rb | 14 ++++++++++++++ 5 files changed, 25 insertions(+), 5 deletions(-) diff --git a/app/models/scheme.rb b/app/models/scheme.rb index 41f757d15..5052b171c 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -7,7 +7,13 @@ class Scheme < ApplicationRecord scope :filter_by_id, ->(id) { where(id: (id.start_with?("S") ? id[1..] : id)) } scope :search_by_service_name, ->(name) { where("service_name ILIKE ?", "%#{name}%") } scope :search_by_postcode, ->(postcode) { joins("LEFT JOIN locations ON locations.scheme_id = schemes.id").where("locations.postcode ILIKE ?", "%#{postcode.delete(' ')}%") } - scope :search_by, ->(param) { search_by_postcode(param).or(search_by_service_name(param)).or(filter_by_id(param)).distinct } + scope :search_by_location_name, ->(name) { joins("LEFT JOIN locations ON locations.scheme_id = schemes.id").where("locations.name ILIKE ?", "%#{name}%") } + scope :search_by, lambda { |param| + search_by_postcode(param) + .or(search_by_service_name(param)) + .or(search_by_location_name(param)) + .or(filter_by_id(param)).distinct + } validate :validate_confirmed diff --git a/app/views/organisations/schemes.html.erb b/app/views/organisations/schemes.html.erb index 06d65477a..8e9690fe5 100644 --- a/app/views/organisations/schemes.html.erb +++ b/app/views/organisations/schemes.html.erb @@ -20,7 +20,7 @@ text: "A supported housing scheme (also known as a ‘supported housing service’) provides shared or self-contained housing for a particular client group, for example younger or vulnerable people. A single scheme can contain multiple units, for example bedrooms in shared houses or a bungalow with 3 bedrooms.", ) %> -<%= render SearchComponent.new(current_user:, search_label: "Search by scheme name, code or postcode", value: @searched) %> +<%= render SearchComponent.new(current_user:, search_label: "Search by scheme name, code, postcode or location name", value: @searched) %>
diff --git a/app/views/schemes/index.html.erb b/app/views/schemes/index.html.erb index ca00be067..8e787a33d 100644 --- a/app/views/schemes/index.html.erb +++ b/app/views/schemes/index.html.erb @@ -7,7 +7,7 @@ <%= govuk_button_link_to "Create a new supported housing scheme", new_scheme_path, html: { method: :post } %> -<%= render SearchComponent.new(current_user:, search_label: "Search by scheme name, code or postcode", value: @searched) %> +<%= render SearchComponent.new(current_user:, search_label: "Search by scheme name, code, postcode or location name", value: @searched) %>
diff --git a/spec/features/schemes_spec.rb b/spec/features/schemes_spec.rb index 2cece714f..b76ecb891 100644 --- a/spec/features/schemes_spec.rb +++ b/spec/features/schemes_spec.rb @@ -34,7 +34,7 @@ RSpec.describe "Schemes scheme Features" do context "when I search for a specific scheme" do it "there is a search bar with a message and search button for schemes" do expect(page).to have_field("search") - expect(page).to have_content("Search by scheme name, code or postcode") + expect(page).to have_content("Search by scheme name, code, postcode or location name") expect(page).to have_button("Search") end @@ -109,7 +109,7 @@ RSpec.describe "Schemes scheme Features" do it "displays a search bar" do expect(page).to have_field("search") - expect(page).to have_content("Search by scheme name, code or postcode") + expect(page).to have_content("Search by scheme name, code, postcode or location name") expect(page).to have_button("Search") end diff --git a/spec/models/scheme_spec.rb b/spec/models/scheme_spec.rb index d81d93bd6..dddf8edb4 100644 --- a/spec/models/scheme_spec.rb +++ b/spec/models/scheme_spec.rb @@ -45,6 +45,17 @@ RSpec.describe Scheme, type: :model do end end + context "when searching by location name" do + it "returns case insensitive matching records" do + expect(described_class.search_by_location_name(location.name.upcase).count).to eq(1) + expect(described_class.search_by_location_name(location.name.downcase).count).to eq(1) + expect(described_class.search_by_location_name(location.name.downcase).first.locations.first.name).to eq(location.name) + expect(described_class.search_by_location_name(location_2.name.upcase).count).to eq(1) + expect(described_class.search_by_location_name(location_2.name.downcase).count).to eq(1) + expect(described_class.search_by_location_name(location_2.name.downcase).first.locations.first.name).to eq(location_2.name) + end + end + context "when searching by all searchable fields" do before do location_2.update!(postcode: location_2.postcode.gsub(scheme_1.id.to_s, "0")) @@ -59,6 +70,9 @@ RSpec.describe Scheme, type: :model do expect(described_class.search_by(location.postcode.upcase).count).to eq(1) expect(described_class.search_by(location.postcode.downcase).count).to eq(1) expect(described_class.search_by(location.postcode.downcase).first.locations.first.postcode).to eq(location.postcode) + expect(described_class.search_by(location.name.upcase).count).to eq(1) + expect(described_class.search_by(location.name.downcase).count).to eq(1) + expect(described_class.search_by(location.name.downcase).first.locations.first.name).to eq(location.name) end end end