Class: OffsetPaginator

Inherits:
JSONAPI::Paginator show all
Defined in:
lib/jsonapi/paginator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from JSONAPI::Paginator

paginator_for

Constructor Details

#initialize(params) ⇒ OffsetPaginator

Returns a new instance of OffsetPaginator.


34
35
36
37
# File 'lib/jsonapi/paginator.rb', line 34

def initialize(params)
  parse_pagination_params(params)
  verify_pagination_params
end

Instance Attribute Details

#limitObject (readonly)

Returns the value of attribute limit


32
33
34
# File 'lib/jsonapi/paginator.rb', line 32

def limit
  @limit
end

#offsetObject (readonly)

Returns the value of attribute offset


32
33
34
# File 'lib/jsonapi/paginator.rb', line 32

def offset
  @offset
end

Class Method Details

.requires_record_countObject


39
40
41
# File 'lib/jsonapi/paginator.rb', line 39

def self.requires_record_count
  true
end

Instance Method Details

#apply(relation, _order_options) ⇒ Object


43
44
45
# File 'lib/jsonapi/paginator.rb', line 43

def apply(relation, _order_options)
  relation.offset(@offset).limit(@limit)
end

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/jsonapi/paginator.rb', line 47

def links_page_params(options = {})
  record_count = options[:record_count]
  links_page_params = {}

  links_page_params['first'] = {
    'offset' => 0,
    'limit' => @limit
  }

  if @offset > 0
    previous_offset = @offset - @limit

    previous_offset = 0 if previous_offset < 0

    links_page_params['prev'] = {
      'offset' => previous_offset,
      'limit' => @limit
    }
  end

  next_offset = @offset + @limit

  unless next_offset >= record_count
    links_page_params['next'] = {
      'offset' => next_offset,
      'limit' => @limit
    }
  end

  if record_count
    last_offset = record_count - @limit

    last_offset = 0 if last_offset < 0

    links_page_params['last'] = {
      'offset' => last_offset,
      'limit' => @limit
    }
  end

  links_page_params
end