Skip to content

Commit 4bc58b1

Browse files
mtsmfmsenid231
authored andcommitted
Consider Model.page(nil)
It is very happy if we can write `Model.page(params[:page])` like Kaminari.
1 parent f96c682 commit 4bc58b1

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lib/json_api_client/query/builder.rb

Lines changed: 1 addition & 1 deletion
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

Lines changed: 26 additions & 0 deletions
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)