Class: JsonapiCompliable::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapi_compliable/scope.rb

Overview

A Scope wraps an underlying object. It modifies that object using the corresponding Resource and Query, and how to resolve that underlying object scope.

Examples:

Basic Controller usage

def index
  base  = Post.all
  scope = jsonapi_scope(base)
  scope.object == base # => true
  scope.object = scope.object.where(active: true)

  # actually fires sql
  scope.resolve #=> [#<Post ...>, #<Post ...>, etc]
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, resource, query, opts = {}) ⇒ Scope

Returns a new instance of Scope

Parameters:

  • object
    • The underlying, chainable base scope object

  • resource
    • The Resource that will process the object

  • query
    • The Query object for the current request

  • opts (Hash) (defaults to: {})

    Options to configure the Scope

Options Hash (opts):

  • :namespace (String)

    The nested relationship name

  • :filter (Boolean)

    Opt-out of filter scoping

  • :extra_fields (Boolean)

    Opt-out of extra_fields scoping

  • :sort (Boolean)

    Opt-out of sort scoping

  • :pagination (Boolean)

    Opt-out of pagination scoping

  • :default_paginate (Boolean)

    Opt-out of default pagination when not specified in request



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/jsonapi_compliable/scope.rb', line 29

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

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object



17
18
19
# File 'lib/jsonapi_compliable/scope.rb', line 17

def object
  @object
end

#unpaginated_objectObject (readonly)

Returns the value of attribute unpaginated_object



17
18
19
# File 'lib/jsonapi_compliable/scope.rb', line 17

def unpaginated_object
  @unpaginated_object
end

Instance Method Details

#query_hashObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The slice of Query#to_hash for the current namespace

See Also:



75
76
77
# File 'lib/jsonapi_compliable/scope.rb', line 75

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

#resolveArray

Resolve the scope. This is where SQL is actually fired, an HTTP request is actually made, etc.

Does nothing if the user requested zero results, ie /posts?page=0

First resolves the top-level resource, then processes each relevant sideload

Returns:

  • (Array)

    an array of resolved model instances

See Also:



62
63
64
65
66
67
68
69
70
# File 'lib/jsonapi_compliable/scope.rb', line 62

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

#resolve_statsHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Resolve the requested stats. Returns hash like:

{ rating: { average: 5.5, maximum: 9 } }

Returns:

  • (Hash)

    the resolved stat info



49
50
51
# File 'lib/jsonapi_compliable/scope.rb', line 49

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