Skip to content

Commit dcead09

Browse files
authored
Merge pull request #287 from senid231/github-mtsmfm-page-nil
allow nil for page number parameter
2 parents f96c682 + b9b367e commit dcead09

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ articles = Article.page(2).per(30).to_a
246246

247247
# also makes request to /articles?page=2&per_page=30
248248
articles = Article.paginate(page: 2, per_page: 30).to_a
249+
250+
# keep in mind that page number can be nil - in that case default number will be applied
251+
# also makes request to /articles?page=1&per_page=30
252+
articles = Article.paginate(page: nil, per_page: 30).to_a
249253
```
250254

251255
*Note: The mapping of pagination parameters is done by the `query_builder` which is [customizable](#custom-paginator).*

lib/json_api_client/query/builder.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def paginate(conditions = {})
4747
end
4848

4949
def page(number)
50-
@pagination_params[ klass.paginator.page_param ] = number
50+
@pagination_params[ klass.paginator.page_param ] = number || 1
5151
self
5252
end
5353

test/unit/query_builder_test.rb

+26
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,32 @@ def test_can_paginate
3838
Article.paginate(page: 3, per_page: 6).to_a
3939
end
4040

41+
def test_pagination_default_number
42+
JsonApiClient::Paginating::Paginator.page_param = :number
43+
stub_request(:get, "http://example.com/articles?#{{page: {number: 1}}.to_query}")
44+
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
45+
data: [{
46+
type: "articles",
47+
id: "1",
48+
attributes: {
49+
title: "JSON API paints my bikeshed!"
50+
}
51+
}],
52+
links: {
53+
self: "http://example.com/articles?#{{page: {number: 1}}.to_query}",
54+
next: "http://example.com/articles?#{{page: {number: 2}}.to_query}",
55+
prev: nil,
56+
first: "http://example.com/articles?#{{page: {number: 1}}.to_query}",
57+
last: "http://example.com/articles?#{{page: {number: 6}}.to_query}"
58+
}
59+
}.to_json)
60+
61+
articles = Article.page(nil)
62+
assert_equal 1, articles.current_page
63+
ensure
64+
JsonApiClient::Paginating::Paginator.page_param = :page
65+
end
66+
4167
def test_can_sort_asc
4268
stub_request(:get, "http://example.com/articles")
4369
.with(query: {sort: "foo"})

0 commit comments

Comments
 (0)