Sha256: ba7ad763a4f19880e59d96146906b3557cced9fa104a441cd72a648a86c9cf8e

Contents?: true

Size: 1.18 KB

Versions: 4

Compression:

Stored size: 1.18 KB

Contents

module Searchgasm
  module Search
    # = Searchgasm Searching
    #
    # Implements searching functionality for searchgasm. Searchgasm::Search::Base and Searchgasm::Conditions::Base can both search and include
    # this module.
    module Searching
      # Use these methods just like you would in ActiveRecord
      SEARCH_METHODS = [:all, :find, :first]
      CALCULATION_METHODS = [:average, :calculate, :count, :maximum, :minimum, :sum]
      
      (SEARCH_METHODS + CALCULATION_METHODS).each do |method|
        class_eval <<-"end_eval", __FILE__, __LINE__
          def #{method}(*args)
            find_options = {}
            options = args.extract_options! # can't pass options, your options are in the search
            klass.send(:with_scope, :find => acting_as_filter? ? {} : scope) do
              options = sanitize(#{SEARCH_METHODS.include?(method)})
              if #{CALCULATION_METHODS.include?(method)}
                options[:distinct] = true
                args[0] = klass.primary_key if [nil, :all].include?(args[0])
              end
              args << options
              klass.#{method}(*args)
            end
          end
        end_eval
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
searchgasm-1.5.0 lib/searchgasm/search/searching.rb
searchgasm-1.5.2 lib/searchgasm/search/searching.rb
searchgasm-1.4.1 lib/searchgasm/search/searching.rb
searchgasm-1.5.1 lib/searchgasm/search/searching.rb