Created
January 25, 2013 09:57
-
-
Save kohgpat/4633208 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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