module ActiveRecord::HashOptions

Constants

VERSION

Public Class Methods

compare_array_column(actual_val, value) click to toggle source
# File lib/active_record/hash_options.rb, line 66
def self.compare_array_column(actual_val, value)
  case value
  when Regexp
    actual_val =~ value
  when Array
    value.include?(actual_val)
  when Range
    value.cover?(actual_val)
  when ActiveRecord::HashOptions::GenericOp
    # NOTE: The `nil?` check in `filter_array` may skip this comparison and short circuit to a false
    value.call(actual_val)
  else # NilClass, String, Integer
    # NOTE: this treats `x == nil` the same as `x IS NULL`
    actual_val == value
  end
end
extended(mod) click to toggle source
# File lib/active_record/hash_options.rb, line 9
def self.extended(mod)
  ActiveRecord::HashOptions.register_my_handler(mod)
end
filter(scope_or_array, conditions, negate = false) click to toggle source
# File lib/active_record/hash_options.rb, line 35
def self.filter(scope_or_array, conditions, negate = false)
  if scope_or_array.kind_of?(Array)
    filter_array(scope_or_array, conditions, negate)
  else
    filter_scope(scope_or_array, conditions, negate)
  end
end
filter_array(array, conditions, negate) click to toggle source
# File lib/active_record/hash_options.rb, line 51
def self.filter_array(array, conditions, negate)
  array.select do |rec|
    conditions.all? do |name, value|
      actual_val = rec.send(name)
      # not thrilled about special cases, but `x in [..., nil]` and `x == nil` are the only cases
      # that handle negation for a `nil` / `null` value correctly
      if actual_val.nil? && (value != nil || value.kind_of?(Array) && !value.include?(nil))
        false
      else
        compare_array_column(actual_val, value) ? !negate : negate
      end
    end
  end
end
filter_scope(scope, conditions, negate) click to toggle source
# File lib/active_record/hash_options.rb, line 43
def self.filter_scope(scope, conditions, negate)
  if negate
    scope.where.not(conditions)
  else
    scope.where(conditions)
  end
end
inherited(mod) click to toggle source
# File lib/active_record/hash_options.rb, line 13
def self.inherited(mod)
  ActiveRecord::HashOptions.register_my_handler(mod)
end
register_my_handler(mod) click to toggle source
# File lib/active_record/hash_options.rb, line 17
def self.register_my_handler(mod)
  if mod < ActiveRecord::Base && mod.kind_of?(Class)
    mod.predicate_builder.register_handler(GT, GT.arel_proc)
    mod.predicate_builder.register_handler(LT, LT.arel_proc)
    mod.predicate_builder.register_handler(GTE, GTE.arel_proc)
    mod.predicate_builder.register_handler(LTE, LTE.arel_proc)
    mod.predicate_builder.register_handler(INSENSITIVE, INSENSITIVE.arel_proc)
    mod.predicate_builder.register_handler(LIKE, LIKE.arel_proc)
    mod.predicate_builder.register_handler(NOT_LIKE, NOT_LIKE.arel_proc)
    # for postgres:
    mod.predicate_builder.register_handler(ILIKE, ILIKE.arel_proc)

    # NOTE: Probably want Regexp over REGEXP (e.g.: where(:name => /value/i))
    mod.predicate_builder.register_handler(Regexp, REGEXP.arel_proc)
    mod.predicate_builder.register_handler(REGEXP, REGEXP.arel_proc)
  end
end