From 981a81631ba33ac608c44421c9f82460eedfcd0b Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Fri, 5 Aug 2022 16:24:19 +0100 Subject: [PATCH] allow searching by a Property Postcode, when case log is supported housing (#818) --- app/models/case_log.rb | 10 ++++++---- spec/models/case_log_spec.rb | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 9b703a510..be31c9200 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -57,11 +57,13 @@ class CaseLog < ApplicationRecord 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("REPLACE(postcode_full, ' ', '') ILIKE ?", "%#{postcode_full.delete(' ')}%") } + scope :filter_by_location_postcode, ->(postcode_full) { left_joins(:location).where("REPLACE(locations.postcode, ' ', '') ILIKE ?", "%#{postcode_full.delete(' ')}%") } scope :search_by, lambda { |param| - filter_by_id(param) - .or(filter_by_tenant_code(param)) - .or(filter_by_propcode(param)) - .or(filter_by_postcode(param)) + filter_by_location_postcode(param) + .or(filter_by_tenant_code(param)) + .or(filter_by_propcode(param)) + .or(filter_by_postcode(param)) + .or(filter_by_id(param)) } AUTOGENERATED_FIELDS = %w[id status created_at updated_at discarded_at].freeze diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 9520feffe..704879df5 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -2073,6 +2073,20 @@ RSpec.describe CaseLog do expect(result.count).to eq(1) expect(result.first.id).to eq case_log_to_search.id end + + context "when case log is supported housing" do + let(:location) { FactoryBot.create(:location, postcode: "W6 0ST") } + + before do + case_log_to_search.update!(needstype: 2, location:) + end + + it "allows searching by a Property Postcode" do + result = described_class.filter_by_location_postcode("W6 0ST") + expect(result.count).to eq(1) + expect(result.first.id).to eq case_log_to_search.id + end + end end describe "#search_by" do @@ -2100,6 +2114,20 @@ RSpec.describe CaseLog do expect(result.first.id).to eq case_log_to_search.id end + context "when case log is supported housing" do + let(:location) { FactoryBot.create(:location, postcode: "W6 0ST") } + + before do + case_log_to_search.update!(needstype: 2, location:) + end + + it "allows searching by a Property Postcode" do + result = described_class.search_by("W6 0ST") + expect(result.count).to eq(1) + expect(result.first.id).to eq case_log_to_search.id + end + end + context "when postcode has spaces and lower case letters" do let(:matching_postcode_lower_case_with_spaces) { case_log_to_search.postcode_full.downcase.chars.insert(3, " ").join }