Sha256: 45f6b04c0ceacf2e0f6851c66810e3f76cae42186dc45be8dbcc3b0b9c81fc83
Contents?: true
Size: 1.61 KB
Versions: 35
Compression:
Stored size: 1.61 KB
Contents
module Cms module Behaviors module Searching def self.included(model_class) model_class.extend(MacroMethods) end module MacroMethods def searchable? !!@is_searchable end 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 end module ClassMethods def searchable_columns @searchable_columns end def default_order_for_search "#{table_name}.#{searchable_columns.first}" end end end end end
Version data entries
35 entries across 35 versions & 11 rubygems