Browse Source

CLDC-2035 Fix search bug (#1327)

* feat: fix search bug

* feat: add correct sales log search filter behaviour

* refactor: removal of logic from erbs

* refactor: linting

* test: add tests to check log filtering works
pull/1394/head
natdeanlewissoftwire 2 years ago committed by GitHub
parent
commit
03b21e4a2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      app/components/search_component.rb
  2. 16
      app/helpers/logs_helper.rb
  3. 1
      app/models/lettings_log.rb
  4. 1
      app/models/log.rb
  5. 7
      app/models/sales_log.rb
  6. 4
      app/views/logs/index.html.erb
  7. 80
      spec/services/filter_service_spec.rb

2
app/components/search_component.rb

@ -23,6 +23,8 @@ class SearchComponent < ViewComponent::Base
user_path(current_user) user_path(current_user)
elsif request.path.include?("organisations") elsif request.path.include?("organisations")
organisations_path organisations_path
elsif request.path.include?("sales-logs")
sales_logs_path
elsif request.path.include?("logs") elsif request.path.include?("logs")
lettings_logs_path lettings_logs_path
end end

16
app/helpers/logs_helper.rb

@ -23,4 +23,20 @@ module LogsHelper
array = bulk_upload ? [bulk_upload.id] : [] array = bulk_upload ? [bulk_upload.id] : []
array.index_with { |_bulk_upload_id| "With logs from bulk upload" } array.index_with { |_bulk_upload_id| "With logs from bulk upload" }
end end
def search_label_for_controller(controller)
case log_type_for_controller(controller)
when "lettings"
"Search by log ID, tenant code, property reference or postcode"
when "sales"
"Search by log ID, purchaser code or postcode"
end
end
def csv_download_url_for_controller(controller)
case log_type_for_controller(controller)
when "lettings"
csv_download_lettings_logs_path(search: params["search"])
end
end
end end

1
app/models/lettings_log.rb

@ -40,7 +40,6 @@ class LettingsLog < Log
scope :filter_by_year, ->(year) { where(startdate: Time.zone.local(year.to_i, 4, 1)...Time.zone.local(year.to_i + 1, 4, 1)) } scope :filter_by_year, ->(year) { where(startdate: Time.zone.local(year.to_i, 4, 1)...Time.zone.local(year.to_i + 1, 4, 1)) }
scope :filter_by_tenant_code, ->(tenant_code) { where("tenancycode ILIKE ?", "%#{tenant_code}%") } scope :filter_by_tenant_code, ->(tenant_code) { where("tenancycode ILIKE ?", "%#{tenant_code}%") }
scope :filter_by_propcode, ->(propcode) { where("propcode ILIKE ?", "%#{propcode}%") } 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 :filter_by_location_postcode, ->(postcode_full) { left_joins(:location).where("REPLACE(locations.postcode, ' ', '') ILIKE ?", "%#{postcode_full.delete(' ')}%") }
scope :search_by, lambda { |param| scope :search_by, lambda { |param|
filter_by_location_postcode(param) filter_by_location_postcode(param)

1
app/models/log.rb

@ -18,6 +18,7 @@ class Log < ApplicationRecord
years.each { |year| query = query.or(filter_by_year(year)) } years.each { |year| query = query.or(filter_by_year(year)) }
query.all query.all
} }
scope :filter_by_postcode, ->(postcode_full) { where("REPLACE(postcode_full, ' ', '') ILIKE ?", "%#{postcode_full.delete(' ')}%") }
scope :filter_by_id, ->(id) { where(id:) } scope :filter_by_id, ->(id) { where(id:) }
scope :filter_by_user, lambda { |selected_user, user| scope :filter_by_user, lambda { |selected_user, user|
if !selected_user.include?("all") && user.present? if !selected_user.include?("all") && user.present?

7
app/models/sales_log.rb

@ -33,7 +33,12 @@ class SalesLog < Log
before_validation :set_derived_fields! before_validation :set_derived_fields!
scope :filter_by_year, ->(year) { where(saledate: Time.zone.local(year.to_i, 4, 1)...Time.zone.local(year.to_i + 1, 4, 1)) } scope :filter_by_year, ->(year) { where(saledate: Time.zone.local(year.to_i, 4, 1)...Time.zone.local(year.to_i + 1, 4, 1)) }
scope :search_by, ->(param) { filter_by_id(param) } scope :filter_by_purchaser_code, ->(purchid) { where("purchid ILIKE ?", "%#{purchid}%") }
scope :search_by, lambda { |param|
filter_by_purchaser_code(param)
.or(filter_by_postcode(param))
.or(filter_by_id(param))
}
scope :filter_by_organisation, ->(org, _user = nil) { where(owning_organisation: org) } scope :filter_by_organisation, ->(org, _user = nil) { where(owning_organisation: org) }
OPTIONAL_FIELDS = %w[saledate_check purchid monthly_charges_value_check old_persons_shared_ownership_value_check].freeze OPTIONAL_FIELDS = %w[saledate_check purchid monthly_charges_value_check old_persons_shared_ownership_value_check].freeze

4
app/views/logs/index.html.erb

@ -64,9 +64,9 @@
<%= render partial: "log_filters" %> <%= render partial: "log_filters" %>
<div class="app-filter-layout__content"> <div class="app-filter-layout__content">
<%= render SearchComponent.new(current_user:, search_label: "Search by log ID, tenant code, property reference or postcode", value: @searched) %> <%= render SearchComponent.new(current_user:, search_label: search_label_for_controller(controller), value: @searched) %>
<%= govuk_section_break(visible: true, size: "m") %> <%= govuk_section_break(visible: true, size: "m") %>
<%= render partial: "log_list", locals: { logs: @logs, title: "Logs", pagy: @pagy, searched: @searched, item_label:, total_count: @total_count, csv_download_url: csv_download_lettings_logs_path(search: @search_term) } %> <%= render partial: "log_list", locals: { logs: @logs, title: "Logs", pagy: @pagy, searched: @searched, item_label:, total_count: @total_count, csv_download_url: csv_download_url_for_controller(controller) } %>
<%== render partial: "pagy/nav", locals: { pagy: @pagy, item_name: "logs" } %> <%== render partial: "pagy/nav", locals: { pagy: @pagy, item_name: "logs" } %>
</div> </div>
</div> </div>

80
spec/services/filter_service_spec.rb

@ -2,26 +2,80 @@ require "rails_helper"
describe FilterService do describe FilterService do
describe "filter_by_search" do describe "filter_by_search" do
before do context "when filtering organisations" do
FactoryBot.create_list(:organisation, 5) before do
FactoryBot.create(:organisation, name: "Acme LTD") FactoryBot.create_list(:organisation, 5)
end FactoryBot.create(:organisation, name: "Acme LTD")
end
let(:organisation_list) { Organisation.all }
context "when given a search term" do
let(:search_term) { "Acme" }
let(:organisation_list) { Organisation.all } it "filters the collection on search term" do
expect(described_class.filter_by_search(organisation_list, search_term).count).to eq(1)
end
end
context "when given a search term" do context "when not given a search term" do
let(:search_term) { "Acme" } let(:search_term) { nil }
it "filters the collection on search term" do it "does not filter the given collection" do
expect(described_class.filter_by_search(organisation_list, search_term).count).to eq(1) expect(described_class.filter_by_search(organisation_list, search_term).count).to eq(6)
end
end end
end end
context "when not given a search term" do context "when filtering logs" do
let(:search_term) { nil } context "when filtering lettings logs" do
before do
FactoryBot.create_list(:lettings_log, 5)
FactoryBot.create(:lettings_log, postcode_full: "SW1 1AA")
end
let(:lettings_log_list) { LettingsLog.all }
context "when given a postcode" do
let(:search_term) { "SW1 1AA" }
it "filters the collection on search term" do
expect(described_class.filter_by_search(lettings_log_list, search_term).count).to eq(1)
end
end
context "when not given a search term" do
let(:search_term) { nil }
it "does not filter the given collection" do
expect(described_class.filter_by_search(lettings_log_list, search_term).count).to eq(6)
end
end
end
context "when filtering sales logs" do
before do
FactoryBot.create_list(:sales_log, 5)
FactoryBot.create(:sales_log, purchid: "2")
end
let(:sales_log_list) { SalesLog.all }
context "when given a purchid" do
let(:search_term) { "2" }
it "filters the collection on search term" do
expect(described_class.filter_by_search(sales_log_list, search_term).count).to eq(1)
end
end
context "when not given a search term" do
let(:search_term) { nil }
it "does not filter the given collection" do it "does not filter the given collection" do
expect(described_class.filter_by_search(organisation_list, search_term).count).to eq(6) expect(described_class.filter_by_search(sales_log_list, search_term).count).to eq(6)
end
end
end end
end end
end end

Loading…
Cancel
Save