Sha256: 9996f7af49ff6024d0b82659c31f70a19eb592f47aadeae46168e234cd491e7c

Contents?: true

Size: 1.91 KB

Versions: 6

Compression:

Stored size: 1.91 KB

Contents

module Searchgasm
  module Search
    # = Searchgasm Conditions
    #
    # Implements all of the conditions functionality into a searchgasm search. All of this functonality is extracted out into its own class Searchgasm::Conditions::Base. This is a separate module to help keep the code
    # clean and organized.
    module Conditions
      def self.included(klass)
        klass.class_eval do
          alias_method_chain :initialize, :conditions
          alias_method_chain :conditions=, :conditions
          alias_method_chain :sanitize, :conditions
        end
      end
      
      def initialize_with_conditions(init_options = {})
        self.conditions = Searchgasm::Conditions::Base.create_virtual_class(klass).new
        initialize_without_conditions(init_options)
      end
      
      # Sets conditions on the search. Accepts a hash or a Searchgasm::Conditions::Base object.
      #
      # === Examples
      #
      #   search.conditions = {:first_name_like => "Ben"}
      #   search.conditions = User.new_conditions
      #
      # or to set a scope
      #
      #   search.conditions = "user_group_id = 6"
      #
      # now you can create the rest of your search and your "scope" will get merged into your final SQL.
      # What this does is determine if the value a hash or a conditions object, if not it sets it up as a scope.
      def conditions_with_conditions=(values)
        case values
        when Searchgasm::Conditions::Base
          @conditions = values
        else
          @conditions.conditions = values
        end
      end
      
      def sanitize_with_conditions(searching = true) # :nodoc:
        find_options = sanitize_without_conditions(searching)
        if conditions_obj = find_options.delete(:conditions)
          new_conditions = conditions_obj.sanitize
          find_options[:conditions] = new_conditions unless new_conditions.blank?
        end
        find_options
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
searchgasm-1.5.1 lib/searchgasm/search/conditions.rb
searchgasm-1.5.2 lib/searchgasm/search/conditions.rb
searchgasm-1.4.0 lib/searchgasm/search/conditions.rb
searchgasm-1.4.1 lib/searchgasm/search/conditions.rb
searchgasm-1.5.0 lib/searchgasm/search/conditions.rb
searchgasm-1.5.3 lib/searchgasm/search/conditions.rb