mirror of
https://github.com/rickbarrette/redmine_qbo_vehicles.git
synced 2026-04-02 07:01:59 -04:00
Enhance search_result_ranks_and_ids method to rank results by ID and streamline query handling
This commit is contained in:
@@ -85,6 +85,7 @@ class Vehicle < ActiveRecord::Base
|
|||||||
where("vin LIKE ? OR make LIKE ? OR model LIKE ? OR year LIKE ?", "%#{q}%", "%#{q}%", "%#{q}%", "%#{q}%")
|
where("vin LIKE ? OR make LIKE ? OR model LIKE ? OR year LIKE ?", "%#{q}%", "%#{q}%", "%#{q}%", "%#{q}%")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Override the defult redmine seach method to rank results by id
|
||||||
# Override the defult redmine seach method to rank results by id
|
# Override the defult redmine seach method to rank results by id
|
||||||
def self.search_result_ranks_and_ids(tokens, user, project = nil, options = {})
|
def self.search_result_ranks_and_ids(tokens, user, project = nil, options = {})
|
||||||
return {} if tokens.blank?
|
return {} if tokens.blank?
|
||||||
@@ -92,14 +93,11 @@ class Vehicle < 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("vin LIKE ? OR make LIKE ? OR model LIKE ? OR year LIKE ?", "%#{q}%", "%#{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 }
|
||||||
# rank by id
|
|
||||||
ids.each_with_object({}) { |id, h| h[id] = id }
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# returns a human readable string
|
# returns a human readable string
|
||||||
|
|||||||
Reference in New Issue
Block a user