Browse Source

Order lettings logs search results

pull/2299/head
Kat 2 years ago committed by kosiakkatrina
parent
commit
2457d3c27e
  1. 18
      app/models/lettings_log.rb
  2. 24
      spec/models/lettings_log_spec.rb

18
app/models/lettings_log.rb

@ -59,12 +59,18 @@ class LettingsLog < Log
query.all
}
scope :search_by, lambda { |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.gsub(/log/i, "")))
}
by_id = Arel.sql("case when lettings_logs.id = #{param.to_i} then 0 else 1 end")
by_tenant_code = Arel.sql("case when tenancycode = '#{param}' then 0 when tenancycode ILIKE '%#{param}%' then 1 else 2 end")
by_propcode = Arel.sql("case when propcode = '#{param}' then 0 when propcode ILIKE '%#{param}%' then 1 else 2 end")
by_postcode = Arel.sql("case when REPLACE(postcode_full, ' ', '') = '#{param.delete(' ')}' then 0 when REPLACE(postcode_full, ' ', '') ILIKE '%#{param.delete(' ')}%' then 1 else 2 end")
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.gsub(/log/i, "")))
.order(by_id).order(by_tenant_code, by_propcode, by_postcode)
}
scope :after_date, ->(date) { where("lettings_logs.startdate >= ?", date) }
scope :before_date, ->(date) { where("lettings_logs.startdate < ?", date) }
scope :unresolved, -> { where(unresolved: true) }

24
spec/models/lettings_log_spec.rb

@ -2885,6 +2885,30 @@ RSpec.describe LettingsLog do
expect(result.first.id).to eq lettings_log_to_search.id
end
end
context "when matching multiple records on different fields" do
let!(:lettings_log_with_propcode) { create(:lettings_log, propcode: lettings_log_to_search.id) }
let!(:lettings_log_with_tenancycode) { create(:lettings_log, tenancycode: lettings_log_to_search.id) }
let!(:lettings_log_with_postcode) { create(:lettings_log, postcode_full: "C1 1AC") }
let!(:lettings_log_with_postcode_tenancycode) { create(:lettings_log, tenancycode: "C1 1AC") }
let!(:lettings_log_with_postcode_propcode) { create(:lettings_log, propcode: "C1 1AC") }
it "returns all matching records in correct order with matching IDs" do
result = described_class.search_by(lettings_log_to_search.id.to_s)
expect(result.count).to eq(3)
expect(result.first.id).to eq lettings_log_to_search.id
expect(result.second.id).to eq lettings_log_with_tenancycode.id
expect(result.third.id).to eq lettings_log_with_propcode.id
end
it "returns all matching records in correct order with matching postcode" do
result = described_class.search_by("C1 1AC")
expect(result.count).to eq(3)
expect(result.first.id).to eq lettings_log_with_postcode_tenancycode.id
expect(result.second.id).to eq lettings_log_with_postcode_propcode.id
expect(result.third.id).to eq lettings_log_with_postcode.id
end
end
end
end

Loading…
Cancel
Save