Module: Cms::Behaviors::Searching::MacroMethods

Defined in:
lib/cms/behaviors/searching.rb

Instance Method Summary collapse

Instance Method Details

#is_searchable(options = {}) ⇒ Object



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
36
37
# 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

  #This is in a method to allow classes to override it
  named_scope :search, lambda{|search_params| 
    term = search_params.is_a?(Hash) ? search_params[:term] : search_params  
    order = search_params.is_a?(Hash) && search_params[:order] ? search_params[:order] : default_order_for_search
    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[:order] = order if order
    scope                      
  }
end

#searchable?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/cms/behaviors/searching.rb', line 8

def searchable?
  !!@is_searchable
end