24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/sitemap_search/model.rb', line 24
def search(query)
words = query.strip.downcase.split(" ").reject(&:blank?)
return [] if words.empty?
wrapped_words = words.map {|word| ["%#{word}%"] * self.search_fields.size }
field_conditions = self.search_fields.map {|field| "LOWER(#{field}) LIKE ?"}
fields_per_word = field_conditions.join(" OR ")
all_conditions = ([fields_per_word] * words.size).map {|stmt| "(#{stmt})"}.join(" AND ")
query_conditions = [all_conditions, wrapped_words].flatten
find(:all, :conditions => query_conditions, :joins => includes)
end
|