Browse Source

Search schemes by location name (#791)

* Search schemes by location name

* Update search hint
pull/795/head
baarkerlounger 2 years ago committed by GitHub
parent
commit
7e3a1fb483
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      app/models/scheme.rb
  2. 2
      app/views/organisations/schemes.html.erb
  3. 2
      app/views/schemes/index.html.erb
  4. 4
      spec/features/schemes_spec.rb
  5. 14
      spec/models/scheme_spec.rb

8
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

2
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) %>
<hr class="govuk-section-break govuk-section-break--visible govuk-section-break--m">

2
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) %>
<hr class="govuk-section-break govuk-section-break--visible govuk-section-break--m">

4
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

14
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

Loading…
Cancel
Save