downcase query string for search

This commit is contained in:
danielgrippi 2011-07-29 14:05:59 -07:00
parent 16bd3e4c2f
commit 2b550906fc

View file

@ -45,6 +45,8 @@ class Person < ActiveRecord::Base
scope :searchable, joins(:profile).where(:profiles => {:searchable => true}) scope :searchable, joins(:profile).where(:profiles => {:searchable => true})
def self.search_query_string(query) def self.search_query_string(query)
query = query.downcase
if postgres? if postgres?
where_clause = <<-SQL where_clause = <<-SQL
profiles.full_name ILIKE ? OR profiles.full_name ILIKE ? OR
@ -57,7 +59,7 @@ class Person < ActiveRecord::Base
SQL SQL
end end
q_tokens = query.to_s.strip.gsub(/(\s|$|^)/) { "%#{$1}" } q_tokens = query.to_s.strip.gsub(/(\s|$)/) { "%#{$1}" }
[where_clause, [q_tokens, q_tokens]] [where_clause, [q_tokens, q_tokens]]
end end
@ -66,6 +68,8 @@ class Person < ActiveRecord::Base
sql, tokens = self.search_query_string(query) sql, tokens = self.search_query_string(query)
pp tokens
Person.searchable.where(sql, *tokens).joins( Person.searchable.where(sql, *tokens).joins(
"LEFT OUTER JOIN contacts ON contacts.user_id = #{user.id} AND contacts.person_id = people.id" "LEFT OUTER JOIN contacts ON contacts.user_id = #{user.id} AND contacts.person_id = people.id"
).includes(:profile ).includes(:profile