Skip to content

Commit 8c8edd6

Browse files
committed
Merge pull request #1700 from iamnader/master
Support pagination link for Kaminari when no data is returned
2 parents 17d0759 + 37ca0c1 commit 8c8edd6

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

CHANGELOG.md

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

4848
Fixes:
49+
- [#1700](https://github.com/rails-api/active_model_serializers/pull/1700) Support pagination link for Kaminari when no data is returned
4950
- [#1657](https://github.com/rails-api/active_model_serializers/pull/1657) Add missing missing require "active_support/json". (@andreaseger)
5051
- [#1661](https://github.com/rails-api/active_model_serializers/pull/1661) Fixes `read_attribute_for_serialization` not
5152
seeing methods defined in serialization superclass (#1653, #1658, #1660), introduced in #1650. (@bf4)

lib/active_model_serializers/adapter/json_api/pagination_links.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def as_json
2828
private
2929

3030
def pages_from
31-
return {} if collection.total_pages == FIRST_PAGE
31+
return {} if collection.total_pages <= FIRST_PAGE
3232

3333
{}.tap do |pages|
3434
pages[:self] = collection.current_page

test/adapter/json_api/pagination_links_test.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ def expected_response_with_last_page_pagination_links
101101
end
102102
end
103103

104+
def expected_response_with_no_data_pagination_links
105+
{}.tap do |hash|
106+
hash[:data] = []
107+
hash[:links] = {}
108+
end
109+
end
110+
104111
def test_pagination_links_using_kaminari
105112
adapter = load_adapter(using_kaminari, mock_request)
106113

@@ -120,6 +127,22 @@ def test_pagination_links_with_additional_params
120127
adapter.serializable_hash
121128
end
122129

130+
def test_pagination_links_when_zero_results_kaminari
131+
@array = []
132+
133+
adapter = load_adapter(using_kaminari(1), mock_request)
134+
135+
assert_equal expected_response_with_no_data_pagination_links, adapter.serializable_hash
136+
end
137+
138+
def test_pagination_links_when_zero_results_will_paginate
139+
@array = []
140+
141+
adapter = load_adapter(using_will_paginate(1), mock_request)
142+
143+
assert_equal expected_response_with_no_data_pagination_links, adapter.serializable_hash
144+
end
145+
123146
def test_last_page_pagination_links_using_kaminari
124147
adapter = load_adapter(using_kaminari(3), mock_request)
125148

0 commit comments

Comments
 (0)