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