Skip to content

Commit 3a685a7

Browse files
authored
Total count and configurable default per_page (#31)
* Remove hard coded maximum per-page limit of 30. * Don't include total in pagination links. * Wrap default per_page in a method. * Update readme with jsonapi_page_size example. * Skip :records in pagination with next.
1 parent 3a31499 commit 3a685a7

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ class MyController < ActionController::Base
290290
render jsonapi: paginated
291291
end
292292
end
293+
293294
end
294295
```
295296

@@ -306,6 +307,15 @@ use the `jsonapi_pagination_meta` method:
306307
end
307308

308309
```
310+
311+
If you want to change the default number of items per page, use the
312+
`jsonapi_page_size` method:
313+
314+
```ruby
315+
def jsonapi_page_size
316+
30
317+
end
318+
```
309319
### Deserialization
310320

311321
`JSONAPI::Deserialization` provides a helper to transform a `JSONAPI` document

lib/jsonapi/pagination.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ def jsonapi_pagination(resources)
4343
original_url = request.base_url + request.path + '?'
4444

4545
pagination.each do |page_name, number|
46+
next if page_name == :records
47+
4648
original_params[:page][:number] = number
4749
links[page_name] = original_url + CGI.unescape(
4850
original_params.to_query
@@ -93,16 +95,21 @@ def jsonapi_pagination_meta(resources)
9395
#
9496
# @return [Array] with the offset, limit and the current page number
9597
def jsonapi_pagination_params
96-
def_per_page = self.class.const_get(:JSONAPI_PAGE_SIZE).to_i
97-
9898
pagination = params[:page].try(:slice, :number, :size) || {}
9999
per_page = pagination[:size].to_f.to_i
100-
per_page = def_per_page if per_page > def_per_page || per_page < 1
100+
per_page = jsonapi_page_size if per_page < 1
101101
num = [1, pagination[:number].to_f.to_i].max
102102

103103
[(num - 1) * per_page, per_page, num]
104104
end
105105

106+
# Retrieves the default page size
107+
#
108+
# @return [Integer]
109+
def jsonapi_page_size
110+
self.class.const_get(:JSONAPI_PAGE_SIZE).to_i
111+
end
112+
106113
# Fallback to Rack's parsed query string when Rails is not available
107114
#
108115
# @return [Hash]

0 commit comments

Comments
 (0)