Enhance customer search functionality by ordering results and refining search method

This commit is contained in:
2026-02-25 22:05:52 -05:00
parent b38f850df3
commit 5d858ae186
2 changed files with 7 additions and 8 deletions

View File

@@ -51,7 +51,7 @@ class CustomersController < ApplicationController
# display a list of all customers # display a list of all customers
def index def index
if params[:search] if params[:search]
@customers = Customer.search(params[:search]).paginate(page: params[:page]) @customers = Customer.search(params[:search]).order(:name).paginate(page: params[:page])
if only_one_non_zero?(@customers) if only_one_non_zero?(@customers)
redirect_to @customers.first redirect_to @customers.first
end end

View File

@@ -30,6 +30,8 @@ class Customer < ActiveRecord::Base
:type => :to_s, :type => :to_s,
:description => Proc.new {|o| "#{I18n.t :label_primary_phone}: #{o.phone_number} #{I18n.t:label_mobile_phone}: #{o.mobile_phone_number}"}, :description => Proc.new {|o| "#{I18n.t :label_primary_phone}: #{o.phone_number} #{I18n.t:label_mobile_phone}: #{o.mobile_phone_number}"},
:datetime => Proc.new {|o| o.updated_at || o.created_at} :datetime => Proc.new {|o| o.updated_at || o.created_at}
#default_scope { order(name: :asc) }
# Convenience Method # Convenience Method
# returns the customer's email # returns the customer's email
@@ -181,10 +183,10 @@ class Customer < ActiveRecord::Base
end end
end end
# Seach for customers by name or phone number
def self.search(search) def self.search(search)
search = sanitize_sql_like(search) search = sanitize_sql_like(search)
customers = where("name LIKE ? OR phone_number LIKE ? OR mobile_phone_number LIKE ?", "%#{search}%", "%#{search}%", "%#{search}%") where("name LIKE ? OR phone_number LIKE ? OR mobile_phone_number LIKE ?", "%#{search}%", "%#{search}%", "%#{search}%")
return customers.order(:name)
end end
# Override the defult redmine seach method to rank results by id # Override the defult redmine seach method to rank results by id
@@ -194,14 +196,11 @@ class Customer < ActiveRecord::Base
scope = self.all scope = self.all
tokens.each do |token| tokens.each do |token|
q = "%#{sanitize_sql_like(token)}%" scope = scope.search(token)
scope = where("name LIKE ? OR phone_number LIKE ? OR mobile_phone_number LIKE ?", "%#{q}%", "%#{q}%", "%#{q}%")
end end
ids = scope.distinct.limit(options[:limit] || 100).pluck(:id) ids = scope.distinct.limit(options[:limit] || 100).pluck(:id)
ids.index_with { |id| id }
# Assign simple uniform ranking
ids.each_with_object({}) { |id, h| h[id] = id }
end end
# proforms a bruteforce sync operation # proforms a bruteforce sync operation