Sha256: 081ade4350ae31b16a3946cf06efb65ca6258859bc27c9b94cadbe270be513a1

Contents?: true

Size: 1.32 KB

Versions: 12

Compression:

Stored size: 1.32 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
          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
      end
      module ClassMethods
        def searchable_columns
          @searchable_columns
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
browsercms-3.5.7 lib/cms/behaviors/searching.rb
browsercms-3.5.6 lib/cms/behaviors/searching.rb
browsercms-3.5.5 lib/cms/behaviors/searching.rb
browsercms-3.5.4 lib/cms/behaviors/searching.rb
browsercms-3.5.3 lib/cms/behaviors/searching.rb
browsercms-3.5.2 lib/cms/behaviors/searching.rb
browsercms-3.5.1 lib/cms/behaviors/searching.rb
browsercms-3.5.0 lib/cms/behaviors/searching.rb
browsercms-3.5.0.rc3 lib/cms/behaviors/searching.rb
browsercms-3.5.0.rc2 lib/cms/behaviors/searching.rb
browsercms-3.5.0.rc1 lib/cms/behaviors/searching.rb
browsercms-3.3.4 lib/cms/behaviors/searching.rb