Skip to content

Instantly share code, notes, and snippets.

@kohgpat
Created January 25, 2013 09:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kohgpat/4633208 to your computer and use it in GitHub Desktop.
Save kohgpat/4633208 to your computer and use it in GitHub Desktop.
class Finder
def self.find(query)
people = []
conditions = []
words = query.split(' ')
case words.count
when 1
conditions << ["id = ?", query]
conditions << ["enp = ?", query]
conditions << ["n_pol = ?", query]
conditions << ["fam LIKE %?%", Unicode::upcase(query)]
when 2
first, second = words
conditions << ["fam LIKE %?% AND im LIKE %?%", Unicode::upcase(first), Unicode::upcase(second)]
conditions << ["s_pol LIKE ? AND n_pol LIKE ?", first, second]
when 3
first, second, third = words
conditions << ["fam LIKE %?% AND im LIKE %?% AND ot LIKE %?%", Unicode::upcase(first), Unicode::upcase(second), Unicode::upcase(third)]
conditions << ["sn_rasp = ?", query]
end
sql = []
bindings = []
conditions.each do |condition|
sql = "(#{condition.first})"
bindings.concat condition.drop(1)
end
Person.where(sql.join(' OR '), *bindings)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment