Skip to content

Commit 5b554b1

Browse files
kdonovanchingor13
authored andcommitted
Fixed: query builder respects custom pagination params (#267)
1 parent fd4c839 commit 5b554b1

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

lib/json_api_client/query/builder.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ def paginate(conditions = {})
4747
end
4848

4949
def page(number)
50-
@pagination_params[:number] = number
50+
@pagination_params[ klass.paginator.page_param ] = number
5151
self
5252
end
5353

5454
def per(size)
55-
@pagination_params[:size] = size
55+
@pagination_params[ klass.paginator.per_page_param ] = size
5656
self
5757
end
5858

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require 'test_helper'
2+
3+
class CustomPaginatorTest < MiniTest::Test
4+
5+
class CustomPaginator < JsonApiClient::Paginating::Paginator
6+
self.page_param = 'pagina'
7+
self.per_page_param = 'limit'
8+
end
9+
10+
class Book < JsonApiClient::Resource
11+
self.site = "http://example.com/"
12+
self.paginator = CustomPaginator
13+
end
14+
15+
def test_can_override_query_param_names
16+
stub_request(:get, "http://example.com/books")
17+
.with(query: {page: {pagina: 3, limit: 6}})
18+
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
19+
data: []
20+
}.to_json)
21+
22+
Book.paginate(page: 3, per_page: 6).to_a
23+
end
24+
25+
end

test/unit/custom_paginator_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ def test_can_override
2323
assert_equal 42, books.total_count
2424
assert_equal 42, books.total_entries
2525
end
26+
2627
end

test/unit/query_builder_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_can_specify_multiple_includes
3131

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

0 commit comments

Comments
 (0)