Class: JsonapiCompliable::Scoping::DefaultFilter

Inherits:
Base
  • Object
show all
Includes:
Filterable
Defined in:
lib/jsonapi_compliable/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

#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 default filtering logic. Loop through each defined default filter, and apply the default proc unless an explicit override is requested

Returns:

  • scope the scope object we are chaining/modifying



38
39
40
41
42
43
44
45
# File 'lib/jsonapi_compliable/scoping/default_filter.rb', line 38

def apply
  resource.default_filters.each_pair do |name, opts|
    next if overridden?(name)
    @scope = opts[:filter].call(@scope, resource.context)
  end

  @scope
end