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.



36
37
38
39
# File 'lib/jsonapi/paginator.rb', line 36

def initialize(params)
  parse_pagination_params(params)
  verify_pagination_params
end

Instance Attribute Details

#limitObject (readonly)

Returns the value of attribute limit.



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

def limit
  @limit
end

#offsetObject (readonly)

Returns the value of attribute offset.



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

def offset
  @offset
end

Class Method Details

.requires_record_countObject



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

def self.requires_record_count
  true
end

Instance Method Details

#apply(relation, _order_options) ⇒ Object



45
46
47
# File 'lib/jsonapi/paginator.rb', line 45

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


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
89
90
# File 'lib/jsonapi/paginator.rb', line 49

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