Class: PagedPaginator

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) ⇒ PagedPaginator

Returns a new instance of PagedPaginator.



122
123
124
125
# File 'lib/jsonapi/paginator.rb', line 122

def initialize(params)
  parse_pagination_params(params)
  verify_pagination_params
end

Instance Attribute Details

#numberObject (readonly)

Returns the value of attribute number.



120
121
122
# File 'lib/jsonapi/paginator.rb', line 120

def number
  @number
end

#sizeObject (readonly)

Returns the value of attribute size.



120
121
122
# File 'lib/jsonapi/paginator.rb', line 120

def size
  @size
end

Class Method Details

.requires_record_countObject



127
128
129
# File 'lib/jsonapi/paginator.rb', line 127

def self.requires_record_count
  true
end

Instance Method Details

#apply(relation, order_options) ⇒ Object



131
132
133
134
# File 'lib/jsonapi/paginator.rb', line 131

def apply(relation, order_options)
  offset = (@number - 1) * @size
  relation.offset(offset).limit(@size)
end


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/jsonapi/paginator.rb', line 136

def links_page_params(options = {})
  record_count = options[:record_count]
  page_count = (record_count / @size.to_f).ceil

  links_page_params = {}

  links_page_params['first'] = {
    'number' => 1,
    'size' => @size
  }

  if @number > 1
    links_page_params['previous'] = {
      'number' => @number - 1,
      'size' => @size
    }
  end

  unless @number >= page_count
    links_page_params['next'] = {
      'number' => @number + 1,
      'size'=> @size
    }
  end

  if record_count
    links_page_params['last'] = {
      'number' => page_count,
      'size' => @size
    }
  end

  links_page_params
end