Class: Graphiti::Scoping::DefaultFilter
- Includes:
- Filterable
- Defined in:
- lib/graphiti/scoping/default_filter.rb
Overview
Default filters apply to every request, unless specifically overridden in the request.
Maybe we only want to show active posts:
class PostResource < ApplicationResource
# ... code ...
default_filter :active do |scope|
scope.where(active: true)
end
end
But if the user is an admin and specifically requests inactive posts:
class PostResource < ApplicationResource
# ... code ...
allow_filter :active, if: admin?
default_filter :active do |scope|
scope.where(active: true)
end
end
# Now a GET /posts?filter[active]=false will return inactive posts
# if the user is an admin.
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#apply ⇒ Object
Apply the default filtering logic.
Methods included from Filterable
#dependent_filters, #filter_param, #find_filter, #find_filter!, #missing_dependent_filters, #missing_required_filters, #required_filters
Methods inherited from Base
#apply?, #apply_custom_scope, #apply_standard_scope, #initialize
Constructor Details
This class inherits a constructor from Graphiti::Scoping::Base
Instance Method Details
#apply ⇒ Object
Apply the default filtering logic. Loop through each defined default filter, and apply the default proc unless an explicit override is requested
38 39 40 41 42 43 44 45 |
# File 'lib/graphiti/scoping/default_filter.rb', line 38 def apply resource.default_filters.each_pair do |name, opts| next if overridden?(name) @scope = resource.instance_exec(@scope, resource.context, &opts[:filter]) end @scope end |