Sha256: aeddda5a72832240bbe8b6de9040534d0c0d18b6dae9c94f2c4741f7ed7ded56

Contents?: true

Size: 1.52 KB

Versions: 32

Compression:

Stored size: 1.52 KB

Contents

module JsonapiCompliable
  # 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.
  #
  # @see Resource.default_filter
  # @see Resource.allow_filter
  class Scoping::DefaultFilter < Scoping::Base
    include Scoping::Filterable

    # Apply the default filtering logic.
    # Loop through each defined default filter, and apply the default
    # proc unless an explicit override is requested
    #
    # @return scope the scope object we are chaining/modifying
    def apply
      resource.default_filters.each_pair do |name, opts|
        next if overridden?(name)
        @scope = opts[:filter].call(@scope)
      end

      @scope
    end

    private

    def overridden?(name)
      if found = find_filter(name)
        found_aliases = found[name][:aliases]
        filter_param.keys.any? { |k| found_aliases.include?(k.to_sym) }
      else
        false
      end
    end
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
jsonapi_compliable-0.11.1 lib/jsonapi_compliable/scoping/default_filter.rb
jsonapi_compliable-0.11.0 lib/jsonapi_compliable/scoping/default_filter.rb
jsonapi_compliable-0.10.9 lib/jsonapi_compliable/scoping/default_filter.rb
jsonapi_compliable-0.10.7 lib/jsonapi_compliable/scoping/default_filter.rb
jsonapi_compliable-0.10.6 lib/jsonapi_compliable/scoping/default_filter.rb
jsonapi_compliable-0.10.5 lib/jsonapi_compliable/scoping/default_filter.rb
jsonapi_compliable-0.10.4 lib/jsonapi_compliable/scoping/default_filter.rb
jsonapi_compliable-0.10.3 lib/jsonapi_compliable/scoping/default_filter.rb
jsonapi_compliable-0.10.2 lib/jsonapi_compliable/scoping/default_filter.rb
jsonapi_compliable-0.10.1 lib/jsonapi_compliable/scoping/default_filter.rb
jsonapi_compliable-0.10.0 lib/jsonapi_compliable/scoping/default_filter.rb
jsonapi_compliable-0.9.2 lib/jsonapi_compliable/scoping/default_filter.rb
jsonapi_compliable-0.9.1 lib/jsonapi_compliable/scoping/default_filter.rb
jsonapi_compliable-0.9.0 lib/jsonapi_compliable/scoping/default_filter.rb
jsonapi_compliable-0.8.0 lib/jsonapi_compliable/scoping/default_filter.rb
jsonapi_compliable-0.7.9 lib/jsonapi_compliable/scoping/default_filter.rb
jsonapi_compliable-0.7.8 lib/jsonapi_compliable/scoping/default_filter.rb
jsonapi_compliable-0.7.7 lib/jsonapi_compliable/scoping/default_filter.rb
jsonapi_compliable-0.7.6 lib/jsonapi_compliable/scoping/default_filter.rb
jsonapi_compliable-0.7.5 lib/jsonapi_compliable/scoping/default_filter.rb