Skip to content

Commit c8a45db

Browse files
committed
Merge pull request #1570 from bmorrall/fix-last-pagination-page-size
Fixed pagination issue with last page size
2 parents 31a30c8 + b5dd90c commit c8a45db

File tree

3 files changed

+51
-14
lines changed

3 files changed

+51
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Features:
2121
- [#1340](https://github.com/rails-api/active_model_serializers/pull/1340) Add support for resource-level meta. (@beauby)
2222

2323
Fixes:
24+
- [#1570](https://github.com/rails-api/active_model_serializers/pull/1570) Fixed pagination issue with last page size. (@bmorrall)
2425
- [#1516](https://github.com/rails-api/active_model_serializers/pull/1516) No longer return a nil href when only
2526
adding meta to a relationship link. (@groyoh)
2627
- [#1458](https://github.com/rails-api/active_model_serializers/pull/1458) Preserve the serializer

lib/active_model_serializers/adapter/json_api/pagination_links.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ def initialize(collection, context)
1212
end
1313

1414
def serializable_hash(options = {})
15+
per_page = collection.try(:per_page) || collection.try(:limit_value) || collection.size
1516
pages_from.each_with_object({}) do |(key, value), hash|
16-
params = query_parameters.merge(page: { number: value, size: collection.size }).to_query
17+
params = query_parameters.merge(page: { number: value, size: per_page }).to_query
1718

1819
hash[key] = "#{url(options)}?#{params}"
1920
end

test/adapter/json_api/pagination_links_test.rb

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ def setup
1515
@array = [
1616
Profile.new({ id: 1, name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
1717
Profile.new({ id: 2, name: 'Name 2', description: 'Description 2', comments: 'Comments 2' }),
18-
Profile.new({ id: 3, name: 'Name 3', description: 'Description 3', comments: 'Comments 3' })
18+
Profile.new({ id: 3, name: 'Name 3', description: 'Description 3', comments: 'Comments 3' }),
19+
Profile.new({ id: 4, name: 'Name 4', description: 'Description 4', comments: 'Comments 4' }),
20+
Profile.new({ id: 5, name: 'Name 5', description: 'Description 5', comments: 'Comments 5' })
1921
]
2022
end
2123

@@ -32,31 +34,43 @@ def load_adapter(paginated_collection, options = {})
3234
ActiveModel::SerializableResource.new(paginated_collection, options)
3335
end
3436

35-
def using_kaminari
36-
Kaminari.paginate_array(@array).page(2).per(1)
37+
def using_kaminari(page = 2)
38+
Kaminari.paginate_array(@array).page(page).per(2)
3739
end
3840

39-
def using_will_paginate
40-
@array.paginate(page: 2, per_page: 1)
41+
def using_will_paginate(page = 2)
42+
@array.paginate(page: page, per_page: 2)
4143
end
4244

4345
def data
4446
{ data: [
4547
{ id: '1', type: 'profiles', attributes: { name: 'Name 1', description: 'Description 1' } },
4648
{ id: '2', type: 'profiles', attributes: { name: 'Name 2', description: 'Description 2' } },
47-
{ id: '3', type: 'profiles', attributes: { name: 'Name 3', description: 'Description 3' } }
49+
{ id: '3', type: 'profiles', attributes: { name: 'Name 3', description: 'Description 3' } },
50+
{ id: '4', type: 'profiles', attributes: { name: 'Name 4', description: 'Description 4' } },
51+
{ id: '5', type: 'profiles', attributes: { name: 'Name 5', description: 'Description 5' } }
4852
]
4953
}
5054
end
5155

5256
def links
5357
{
5458
links: {
55-
self: "#{URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
56-
first: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
57-
prev: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
58-
next: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
59-
last: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1"
59+
self: "#{URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2",
60+
first: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
61+
prev: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
62+
next: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=2",
63+
last: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=2"
64+
}
65+
}
66+
end
67+
68+
def last_page_links
69+
{
70+
links: {
71+
self: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=2",
72+
first: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
73+
prev: "#{URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2"
6074
}
6175
}
6276
end
@@ -67,19 +81,26 @@ def expected_response_without_pagination_links
6781

6882
def expected_response_with_pagination_links
6983
{}.tap do |hash|
70-
hash[:data] = [data.values.flatten.second]
84+
hash[:data] = data.values.flatten[2..3]
7185
hash.merge! links
7286
end
7387
end
7488

7589
def expected_response_with_pagination_links_and_additional_params
7690
new_links = links[:links].each_with_object({}) { |(key, value), hash| hash[key] = "#{value}&test=test" }
7791
{}.tap do |hash|
78-
hash[:data] = [data.values.flatten.second]
92+
hash[:data] = data.values.flatten[2..3]
7993
hash.merge! links: new_links
8094
end
8195
end
8296

97+
def expected_response_with_last_page_pagination_links
98+
{}.tap do |hash|
99+
hash[:data] = [data.values.flatten.last]
100+
hash.merge! last_page_links
101+
end
102+
end
103+
83104
def test_pagination_links_using_kaminari
84105
adapter = load_adapter(using_kaminari)
85106

@@ -102,6 +123,20 @@ def test_pagination_links_with_additional_params
102123
adapter.serializable_hash(@options)
103124
end
104125

126+
def test_last_page_pagination_links_using_kaminari
127+
adapter = load_adapter(using_kaminari(3))
128+
129+
mock_request
130+
assert_equal expected_response_with_last_page_pagination_links, adapter.serializable_hash(@options)
131+
end
132+
133+
def test_last_page_pagination_links_using_will_paginate
134+
adapter = load_adapter(using_will_paginate(3))
135+
136+
mock_request
137+
assert_equal expected_response_with_last_page_pagination_links, adapter.serializable_hash(@options)
138+
end
139+
105140
def test_not_showing_pagination_links
106141
adapter = load_adapter(@array)
107142

0 commit comments

Comments
 (0)