Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ articles = Article.page(2).per(30).to_a

# also makes request to /articles?page=2&per_page=30
articles = Article.paginate(page: 2, per_page: 30).to_a

# keep in mind that page number can be nil - in that case default number will be applied
# also makes request to /articles?page=1&per_page=30
articles = Article.paginate(page: nil, per_page: 30).to_a
```

*Note: The mapping of pagination parameters is done by the `query_builder` which is [customizable](#custom-paginator).*
Expand Down
2 changes: 1 addition & 1 deletion lib/json_api_client/query/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def paginate(conditions = {})
end

def page(number)
@pagination_params[ klass.paginator.page_param ] = number
@pagination_params[ klass.paginator.page_param ] = number || 1
self
end

Expand Down
26 changes: 26 additions & 0 deletions test/unit/query_builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,32 @@ def test_can_paginate
Article.paginate(page: 3, per_page: 6).to_a
end

def test_pagination_default_number
JsonApiClient::Paginating::Paginator.page_param = :number
stub_request(:get, "http://example.com/articles?#{{page: {number: 1}}.to_query}")
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
data: [{
type: "articles",
id: "1",
attributes: {
title: "JSON API paints my bikeshed!"
}
}],
links: {
self: "http://example.com/articles?#{{page: {number: 1}}.to_query}",
next: "http://example.com/articles?#{{page: {number: 2}}.to_query}",
prev: nil,
first: "http://example.com/articles?#{{page: {number: 1}}.to_query}",
last: "http://example.com/articles?#{{page: {number: 6}}.to_query}"
}
}.to_json)

articles = Article.page(nil)
assert_equal 1, articles.current_page
ensure
JsonApiClient::Paginating::Paginator.page_param = :page
end

def test_can_sort_asc
stub_request(:get, "http://example.com/articles")
.with(query: {sort: "foo"})
Expand Down