File tree 3 files changed +31
-1
lines changed
lib/json_api_client/query
3 files changed +31
-1
lines changed Original file line number Diff line number Diff line change @@ -246,6 +246,10 @@ articles = Article.page(2).per(30).to_a
246
246
247
247
# also makes request to /articles?page=2&per_page=30
248
248
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
249
253
```
250
254
251
255
* Note: The mapping of pagination parameters is done by the ` query_builder ` which is [ customizable] ( #custom-paginator ) .*
Original file line number Diff line number Diff line change @@ -47,7 +47,7 @@ def paginate(conditions = {})
47
47
end
48
48
49
49
def page ( number )
50
- @pagination_params [ klass . paginator . page_param ] = number
50
+ @pagination_params [ klass . paginator . page_param ] = number || 1
51
51
self
52
52
end
53
53
Original file line number Diff line number Diff line change @@ -38,6 +38,32 @@ def test_can_paginate
38
38
Article . paginate ( page : 3 , per_page : 6 ) . to_a
39
39
end
40
40
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
+
41
67
def test_can_sort_asc
42
68
stub_request ( :get , "http://example.com/articles" )
43
69
. with ( query : { sort : "foo" } )
You can’t perform that action at this time.
0 commit comments