Skip to content

Commit

Permalink
Fixed: query builder respects custom pagination params (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdonovan authored and chingor13 committed Nov 22, 2017
1 parent fd4c839 commit 5b554b1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 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
25 changes: 25 additions & 0 deletions test/unit/custom_paginator_params_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'test_helper'

class CustomPaginatorTest < MiniTest::Test

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

class Book < JsonApiClient::Resource
self.site = "http://example.com/"
self.paginator = CustomPaginator
end

def test_can_override_query_param_names
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)

Book.paginate(page: 3, per_page: 6).to_a
end

end
1 change: 1 addition & 0 deletions test/unit/custom_paginator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ def test_can_override
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 5b554b1

Please sign in to comment.