module ActiveRecord::FilterableBy::ClassMethods

Public Instance Methods

filter_by(hash = nil, **opts) click to toggle source

@param [Hash|ActionController::Parameters] the filter params @return [ActiveRecord::Relation] the scoped relation

# File lib/filterable_by.rb, line 83
def filter_by(hash = nil, **opts)
  if hash.nil?
    hash = opts
    opts = {}
  end

  scope = all
  return scope unless hash.respond_to?(:key?) && hash.respond_to?(:[])

  _filterable_by_config.each do |name, block|
    scope = FilterableBy.merge(scope, unscoped, hash, name, **opts, &block)
    break unless scope
  end

  scope || none
end
filterable_by(*names, &block) click to toggle source
# File lib/filterable_by.rb, line 71
def filterable_by(*names, &block)
  if block && block.arity > 1
    ActiveSupport::Deprecation.warn('using scope in filterable_by blocks is deprecated. Please use filterable_by(:x) {|val| where(field: val) } instead.')
  end

  names.each do |name|
    _filterable_by_config[name.to_s] = block || ->(value, **) { where(name.to_sym => value) }
  end
end