Class: JsonapiCompliable::Scoping::Filter

Inherits:
Base
  • Object
show all
Includes:
Filterable
Defined in:
lib/jsonapi_compliable/scoping/filter.rb

Overview

Apply filtering logic to the scope

If the user requests to filter a field that has not been whitelisted, a JsonapiCompliable::Errors::BadFilter error will be raised.

allow_filter :title # :title now whitelisted

If the user requests a filter field that has been whitelisted, but does not pass the associated `:if clause, BadFilter will be raised.

allow_filter :title, if: :admin?

This will also honor filter aliases.

# GET /posts?filter[headline]=foo will filter on title
allow_filter :title, aliases: [:headline]

Instance Attribute Summary

Attributes inherited from Base

#query_hash, #resource

Instance Method Summary collapse

Methods included from Filterable

#filter_param, #find_filter, #find_filter!

Methods inherited from Base

#apply?, #apply_custom_scope, #apply_standard_scope, #initialize

Constructor Details

This class inherits a constructor from JsonapiCompliable::Scoping::Base

Instance Method Details

#applyObject

Apply the filtering logic.

Loop and parse all requested filters, taking into account guards and aliases. If valid, call either the default or custom filtering logic.

Returns:

  • the scope we are chaining/modifying



30
31
32
33
34
35
36
# File 'lib/jsonapi_compliable/scoping/filter.rb', line 30

def apply
  each_filter do |filter, value|
    @scope = filter_scope(filter, value)
  end

  @scope
end