Skip to content

Commit

Permalink
Fixed: query builder respects custom pagination params
Browse files Browse the repository at this point in the history
  • Loading branch information
kdonovan committed Aug 8, 2017
1 parent 2c7493c commit afbcded
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lib/json_api_client/query/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ def paginate(conditions = {})
end

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

def per(size)
@pagination_params[:size] = size
@pagination_params[ klass.paginator.per_page_param ] = size
self
end

Expand Down
7 changes: 6 additions & 1 deletion test/unit/custom_paginator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
class CustomPaginatorTest < MiniTest::Test

class CustomPaginator < JsonApiClient::Paginating::Paginator
self.page_param = 'pagina'
self.per_page_param = 'limit'

def total_entries
42
end
Expand All @@ -15,12 +18,14 @@ class Book < JsonApiClient::Resource

def test_can_override
stub_request(:get, "http://example.com/books")
.with(query: {page: {pagina: 3, limit: 6}})
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
data: []
}.to_json)

books = Book.all
books = Book.paginate(page: 3, per_page: 6).to_a
assert_equal 42, books.total_count
assert_equal 42, books.total_entries
end

end
2 changes: 1 addition & 1 deletion test/unit/query_builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_can_specify_multiple_includes

def test_can_paginate
stub_request(:get, "http://example.com/articles")
.with(query: {page: {number: 3, size: 6}})
.with(query: {page: {page: 3, per_page: 6}})
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
data: []
}.to_json)
Expand Down

0 comments on commit afbcded

Please sign in to comment.