Sha256: c7fcabf1bcca6eaca259a1b4ebc6032517760d58278c8c46e6193c5a631d6e65

Contents?: true

Size: 1.61 KB

Versions: 9

Compression:

Stored size: 1.61 KB

Contents

module Searchlogic
  class Search
    module Conditions
      # Returns a hash of the current conditions set.
      def conditions
        mass_conditions.clone.merge(@conditions)
      end

      def compact_conditions
        conditions.select { |k,v| !v.blank? }
      end

      # Accepts a hash of conditions.
      def conditions=(values)
        values.each do |condition, value|
          mass_conditions[condition.to_sym] = value
          value.delete_if { |v| ignore_value?(v) } if value.is_a?(Array)
          next if ignore_value?(value)
          send("#{condition}=", value)
        end
      end

      # Delete a condition from the search. Since conditions map to named scopes,
      # if a named scope accepts a parameter there is no way to actually delete
      # the scope if you do not want it anymore. A nil value might be meaningful
      # to that scope.
      def delete(*names)
        names.each do |name|
          @conditions.delete(name.to_sym)
          mass_conditions.delete(name)
        end
        self
      end

      private
        # This is here as a hook to allow people to modify the order in which the conditions are called, for whatever reason.
        def conditions_array
          @conditions.to_a
        end

        def write_condition(name, value)
          @conditions[name] = value
        end

        def read_condition(name)
          @conditions[name]
        end

        def mass_conditions
          @mass_conditions ||= {}
        end

        def ignore_value?(value)
          (value.is_a?(String) && value.blank?) || (value.is_a?(Array) && value.empty?)
        end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
searchlogic-2.5.8 lib/searchlogic/search/conditions.rb
searchlogic-2.5.7 lib/searchlogic/search/conditions.rb
searchlogic-2.5.6 lib/searchlogic/search/conditions.rb
searchlogic-2.5.5 lib/searchlogic/search/conditions.rb
searchlogic-2.5.4 lib/searchlogic/search/conditions.rb
searchlogic-2.5.3 lib/searchlogic/search/conditions.rb
searchlogic-2.5.2 lib/searchlogic/search/conditions.rb
searchlogic-2.5.1 lib/searchlogic/search/conditions.rb
searchlogic-2.5.0 lib/searchlogic/search/conditions.rb