Class: Graphiti::Scoping::Paginate

Inherits:
Base show all
Defined in:
lib/graphiti/scoping/paginate.rb

Constant Summary collapse

DEFAULT_PAGE_SIZE =
20
PARAMS =
[:number, :size, :offset, :before, :after]

Instance Attribute Summary

Attributes inherited from Base

#query_hash, #resource

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Graphiti::Scoping::Base

Instance Method Details

#applyObject



6
7
8
9
10
11
12
13
14
15
# File 'lib/graphiti/scoping/paginate.rb', line 6

def apply
  if size > resource.max_page_size
    raise Graphiti::Errors::UnsupportedPageSize
      .new(size, resource.max_page_size)
  elsif requested? && @opts[:sideload_parent_length].to_i > 1
    raise Graphiti::Errors::UnsupportedPagination
  else
    super
  end
end

#apply?Boolean

We want to apply this logic unless we’ve explicitly received the default: false option. In that case, only apply if pagination was explicitly specified in the request.

Returns:

  • (Boolean)

    should we apply this logic?



22
23
24
25
26
27
28
# File 'lib/graphiti/scoping/paginate.rb', line 22

def apply?
  if @opts[:default_paginate] == false
    requested?
  else
    true
  end
end

#apply_custom_scopeObject

Apply the custom pagination proc



47
48
49
50
51
52
53
54
55
# File 'lib/graphiti/scoping/paginate.rb', line 47

def apply_custom_scope
  resource.instance_exec \
    @scope,
    number,
    size,
    resource.context,
    offset,
    &custom_scope
end

#apply_standard_scopeObject

Apply default pagination proc via the Resource adapter



36
37
38
39
40
41
42
43
44
# File 'lib/graphiti/scoping/paginate.rb', line 36

def apply_standard_scope
  meth = resource.adapter.method(:paginate)

  if meth.arity == 4 # backwards-compat
    resource.adapter.paginate(@scope, number, size, offset)
  else
    resource.adapter.paginate(@scope, number, size)
  end
end

#custom_scopeProc, Nil

Returns the custom pagination proc.

Returns:

  • (Proc, Nil)

    the custom pagination proc



31
32
33
# File 'lib/graphiti/scoping/paginate.rb', line 31

def custom_scope
  resource.pagination
end