11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/cms/behaviors/searching.rb', line 11
def is_searchable(options={})
@is_searchable = true
@searchable_columns = options[:searchable_columns] ? options[:searchable_columns].map(&:to_sym) : [:name]
extend ClassMethods
scope :search, lambda{|search_params|
term = search_params.is_a?(Hash) ? search_params[:term] : search_params
conditions = []
unless term.blank?
searchable_columns.each do |c|
if conditions.empty?
conditions = ["#{table_name}.#{c} like ?"]
else
conditions.first << "or #{table_name}.#{c} like ?"
end
conditions << "%#{term}%"
end
conditions[0] = "(#{conditions[0]})"
end
scope = {}
scope[:conditions] = conditions if conditions
scope
}
end
|