From 066e58fdd16e5942327d5d9c55abe7287c7d48a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Meny?= Date: Tue, 2 Aug 2022 15:29:29 +0100 Subject: [PATCH] Remove spaces in postcode searches Co-Authored-By: Katrina --- app/models/case_log.rb | 2 +- app/models/location.rb | 2 +- app/models/scheme.rb | 4 ++-- spec/features/schemes_helpers.rb | 4 ++-- spec/requests/case_logs_controller_spec.rb | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index a37836116..0e9d550e0 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -56,7 +56,7 @@ class CaseLog < ApplicationRecord scope :filter_by_id, ->(id) { where(id:) } scope :filter_by_tenant_code, ->(tenant_code) { where("tenancycode ILIKE ?", "%#{tenant_code}%") } scope :filter_by_propcode, ->(propcode) { where("propcode ILIKE ?", "%#{propcode}%") } - scope :filter_by_postcode, ->(postcode_full) { where("postcode_full ILIKE ?", "%#{UKPostcode.parse(postcode_full)}%") } + scope :filter_by_postcode, ->(postcode_full) { where("REPLACE(postcode_full, ' ', '') ILIKE ?", "%#{postcode_full.delete(' ')}%") } scope :search_by, lambda { |param| filter_by_id(param) .or(filter_by_tenant_code(param)) diff --git a/app/models/location.rb b/app/models/location.rb index b0670d589..38ebeec74 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -7,7 +7,7 @@ class Location < ApplicationRecord attr_accessor :add_another_location - scope :search_by_postcode, ->(postcode) { where("postcode ILIKE ?", "%#{UKPostcode.parse(postcode)}%") } + scope :search_by_postcode, ->(postcode) { where("REPLACE(postcode, ' ', '') ILIKE ?", "%#{postcode.delete(' ')}%") } scope :search_by_name, ->(name) { where("name ILIKE ?", "%#{name}%") } scope :search_by, ->(param) { search_by_name(param).or(search_by_postcode(param)) } diff --git a/app/models/scheme.rb b/app/models/scheme.rb index 7b18d8b0e..ea2754206 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -6,8 +6,8 @@ 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 ?", "%#{UKPostcode.parse(postcode)}%") } - scope :search_by_location_name, ->(name) { joins("LEFT JOIN locations ON locations.scheme_id = schemes.id").where("locations.name ILIKE ?", "%#{name}%") } + scope :search_by_postcode, ->(postcode) { left_joins(:locations).where("REPLACE(locations.postcode, ' ', '') ILIKE ?", "%#{postcode.delete(' ')}%") } + scope :search_by_location_name, ->(name) { left_joins(:locations).where("locations.name ILIKE ?", "%#{name}%") } scope :search_by, lambda { |param| search_by_postcode(param) .or(search_by_service_name(param)) diff --git a/spec/features/schemes_helpers.rb b/spec/features/schemes_helpers.rb index f903a3de0..d1a4db2dc 100644 --- a/spec/features/schemes_helpers.rb +++ b/spec/features/schemes_helpers.rb @@ -55,7 +55,7 @@ module SchemesHelpers end def fill_in_and_save_location - fill_in "Postcode", with: "AA1 1AA" + fill_in "Postcode", with: "AA11AA" fill_in "Location name (optional)", with: "Some name" fill_in "Total number of units at this location", with: 5 fill_in "Day", with: 2 @@ -68,7 +68,7 @@ module SchemesHelpers end def fill_in_and_save_second_location - fill_in "Postcode", with: "AA1 2AA" + fill_in "Postcode", with: "AA12AA" fill_in "Location name (optional)", with: "Other name" fill_in "Total number of units at this location", with: 2 choose "Self-contained house" diff --git a/spec/requests/case_logs_controller_spec.rb b/spec/requests/case_logs_controller_spec.rb index db60b2a65..4ca219c67 100644 --- a/spec/requests/case_logs_controller_spec.rb +++ b/spec/requests/case_logs_controller_spec.rb @@ -792,7 +792,7 @@ RSpec.describe CaseLogsController, type: :request do it "downloads logs matching both csv and filter logs" do get "/logs?status[]=completed&search=#{postcode}", headers:, params: {} csv = CSV.parse(response.body) - expect(csv.count).to eq(1) + expect(csv.count).to eq(2) end end end