Sha256: ff053b2d2da036d64c71b28550b1b5078c50209a3169ac1048fdbdd2ca07a75c

Contents?: true

Size: 1.89 KB

Versions: 6

Compression:

Stored size: 1.89 KB

Contents

module JsonapiCompliable
  class Scope
    def initialize(object, resource, query, opts = {})
      @object    = object
      @resource  = resource
      @query     = query

      # Namespace for the 'outer' or 'main' resource is its type
      # For its relationships, its the relationship name
      # IOW when hitting /states, it's resource type 'states
      # when hitting /authors?include=state its 'state'
      @namespace = opts.delete(:namespace) || resource.type

      apply_scoping(opts)
    end

    def resolve_stats
      Stats::Payload.new(@resource, query_hash, @unpaginated_object).generate
    end

    def resolve
      if @query.zero_results?
        []
      else
        resolved = @resource.resolve(@object)
        sideload(resolved, query_hash[:include]) if query_hash[:include]
        resolved
      end
    end

    def query_hash
      @query_hash ||= @query.to_hash[@namespace]
    end

    private

    def sideload(results, includes)
      return if results == []

      includes.each_pair do |name, nested|
        if @resource.allowed_sideloads.has_key?(name)
          sideload = @resource.sideload(name)
          sideload.resolve(results, @query)
        end
      end
    end

    def apply_scoping(opts)
      @object = JsonapiCompliable::Scoping::DefaultFilter.new(@resource, query_hash, @object).apply
      @object = JsonapiCompliable::Scoping::Filter.new(@resource, query_hash, @object).apply unless opts[:filter] == false
      @object = JsonapiCompliable::Scoping::ExtraFields.new(@resource, query_hash, @object).apply unless opts[:extra_fields] == false
      @object = JsonapiCompliable::Scoping::Sort.new(@resource, query_hash, @object).apply unless opts[:sort] == false
      @unpaginated_object = @object
      @object = JsonapiCompliable::Scoping::Paginate.new(@resource, query_hash, @object, default: opts[:default_paginate]).apply unless opts[:paginate] == false
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
jsonapi_compliable-0.6.4 lib/jsonapi_compliable/scope.rb
jsonapi_compliable-0.6.3 lib/jsonapi_compliable/scope.rb
jsonapi_compliable-0.6.2 lib/jsonapi_compliable/scope.rb
jsonapi_compliable-0.6.1 lib/jsonapi_compliable/scope.rb
jsonapi_compliable-0.6.0 lib/jsonapi_compliable/scope.rb
jsonapi_compliable-0.5.7 lib/jsonapi_compliable/scope.rb