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.



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

def initialize(params)
  parse_pagination_params(params)
  verify_pagination_params
end

Instance Attribute Details

#limitObject (readonly)

Returns the value of attribute limit.



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

def limit
  @limit
end

#offsetObject (readonly)

Returns the value of attribute offset.



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

def offset
  @offset
end

Class Method Details

.requires_record_countObject

Deprecated.


47
48
49
# File 'lib/jsonapi/paginator.rb', line 47

def self.requires_record_count
  true
end

Instance Method Details

#apply(relation, _order_options) ⇒ Object



55
56
57
# File 'lib/jsonapi/paginator.rb', line 55

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


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
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/jsonapi/paginator.rb', line 59

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

#requires_record_countObject



51
52
53
# File 'lib/jsonapi/paginator.rb', line 51

def requires_record_count
  true
end