module JSONAPI::Utils::Support::Filter::Default

Public Instance Methods

apply_filter(records, options = {}) click to toggle source

Apply default equality filters.

e.g.: User.where(name: 'Foobar')

@param records [ActiveRecord::Relation, Array] collection of records

e.g.: User.all or [{ id: 1, name: 'Tiago' }, { id: 2, name: 'Doug' }]

@param options [Hash] JU's options

e.g.: { filter: false, paginate: false }

@return [ActiveRecord::Relation, Array]

@api public

# File lib/jsonapi/utils/support/filter/default.rb, line 15
def apply_filter(records, options = {})
  if apply_filter?(records, options)
    records.where(filter_params)
  else
    records
  end
end
apply_filter?(records, options = {}) click to toggle source

Check whether default filters should be applied.

@param records [ActiveRecord::Relation, Array] collection of records

e.g.: User.all or [{ id: 1, name: 'Tiago' }, { id: 2, name: 'Doug' }]

@param options [Hash] JU's options

e.g.: { filter: false, paginate: false }

@return [Boolean]

@api public

# File lib/jsonapi/utils/support/filter/default.rb, line 34
def apply_filter?(records, options = {})
  params[:filter].present? && records.respond_to?(:where) &&
    (options[:filter].nil? || options[:filter])
end
filter_params() click to toggle source

Build a Hash with the default filters.

@return [Hash, NilClass]

@api public

# File lib/jsonapi/utils/support/filter/default.rb, line 44
def filter_params
  @_filter_params ||=
    case params[:filter]
    when Hash, ActionController::Parameters
      default_filters.each_with_object({}) do |field, hash|
        unformatted_field = @request.unformat_key(field)
        hash[unformatted_field] = params[:filter][field]
      end
    end
end

Private Instance Methods

default_filters() click to toggle source

Take all allowed filters and remove the custom ones.

@return [Array]

@api private

# File lib/jsonapi/utils/support/filter/default.rb, line 62
def default_filters
  params[:filter].keys.map(&:to_sym) - @request.resource_klass._custom_filters
end