diff --git a/app/models/scheme.rb b/app/models/scheme.rb index accc4f17c..5ac48b310 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -6,7 +6,7 @@ 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(:locations).where("locations.postcode ILIKE ?", "%#{postcode.delete(' ')}%") } + 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 } validate :validate_confirmed diff --git a/spec/features/schemes_spec.rb b/spec/features/schemes_spec.rb index f1b8aafde..63e530ec9 100644 --- a/spec/features/schemes_spec.rb +++ b/spec/features/schemes_spec.rb @@ -119,6 +119,13 @@ RSpec.describe "Schemes scheme Features" do expect(page).to have_content(scheme_to_search.id_to_display) end + it "returns results with no location" do + scheme_to_search.locations.each { |location| location.destroy } + scheme_to_search.reload + click_button("Search") + expect(page).to have_content(scheme_to_search.id_to_display) + end + it "allows clearing the search results" do fill_in("search", with: scheme_to_search.id_to_display) click_button("Search") diff --git a/spec/requests/schemes_controller_spec.rb b/spec/requests/schemes_controller_spec.rb index 26394886c..b082d6683 100644 --- a/spec/requests/schemes_controller_spec.rb +++ b/spec/requests/schemes_controller_spec.rb @@ -179,6 +179,16 @@ RSpec.describe SchemesController, type: :request do end end + it "returns results with no location" do + searched_scheme.locations.each { |location| location.destroy } + searched_scheme.reload + get "/schemes?search=#{search_param}" + expect(page).to have_content(searched_scheme.id_to_display) + schemes.each do |scheme| + expect(page).not_to have_content(scheme.id_to_display) + end + end + it "updates the table caption" do expect(page).to have_content("1 scheme found matching ‘#{search_param}’") end