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

Version Path
SFEley-browsercms-3.0.2 lib/cms/behaviors/searching.rb
buzzware-browsercms-3.0.2 lib/cms/behaviors/searching.rb
coredumplings-browsercms-3.0.0 lib/cms/behaviors/searching.rb
nate-browsercms-3.0.210 lib/cms/behaviors/searching.rb
nate-browsercms-3.0.211 lib/cms/behaviors/searching.rb
we5-browsercms-3.0.1.1 lib/cms/behaviors/searching.rb
webficient-browsercms-3.0.1 lib/cms/behaviors/searching.rb
webficient-browsercms-3.0.2 lib/cms/behaviors/searching.rb
webficient-browsercms-3.0.3 lib/cms/behaviors/searching.rb
webficient-browsercms-3.0.4 lib/cms/behaviors/searching.rb
browsercms-3.1.5 lib/cms/behaviors/searching.rb
browsercms-3.1.4 lib/cms/behaviors/searching.rb
browsercms-3.1.3 lib/cms/behaviors/searching.rb
bf4-browsercms-3.1.0 lib/cms/behaviors/searching.rb
drujensen-browsercms-3.2.0 lib/cms/behaviors/searching.rb
browsercmsi-3.1.2 lib/cms/behaviors/searching.rb
browsercms-3.1.2 lib/cms/behaviors/searching.rb
browsercms-3.1.1 lib/cms/behaviors/searching.rb
browsercmsi-3.1.1 lib/cms/behaviors/searching.rb
browsercmsi-3.1.0 lib/cms/behaviors/searching.rb