Skip to content

Commit 7387266

Browse files
Always display self, first, last pagination links
1 parent dff621e commit 7387266

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

lib/active_model_serializers/adapter/json_api/pagination_links.rb

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,21 @@ def as_json
3535
private
3636

3737
def pages_from
38-
return {} if collection.total_pages <= FIRST_PAGE
39-
4038
{}.tap do |pages|
41-
pages[:self] = collection.current_page
39+
pages[:self] = collection.current_page
40+
pages[:first] = FIRST_PAGE
41+
pages[:last] = collection.total_pages
4242

43-
unless collection.current_page == FIRST_PAGE
44-
pages[:first] = FIRST_PAGE
45-
pages[:prev] = collection.current_page - FIRST_PAGE
46-
end
43+
if collection.total_pages > 0
44+
unless collection.current_page == FIRST_PAGE
45+
pages[:prev] = collection.current_page - FIRST_PAGE
46+
end
4747

48-
unless collection.current_page == collection.total_pages
49-
pages[:next] = collection.current_page + FIRST_PAGE
50-
pages[:last] = collection.total_pages
48+
unless collection.current_page == collection.total_pages
49+
pages[:next] = collection.current_page + FIRST_PAGE
50+
end
51+
else
52+
pages[:last] = FIRST_PAGE
5153
end
5254
end
5355
end

test/adapter/json_api/pagination_links_test.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ def data
5454
}
5555
end
5656

57+
def empty_collection_links
58+
{
59+
self: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
60+
first: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
61+
last: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2"
62+
}
63+
end
64+
5765
def links
5866
{
5967
links: {
@@ -71,6 +79,7 @@ def last_page_links
7179
links: {
7280
self: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=2",
7381
first: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
82+
last: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=2",
7483
prev: "#{URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2"
7584
}
7685
}
@@ -111,7 +120,7 @@ def expected_response_with_last_page_pagination_links
111120
def expected_response_with_no_data_pagination_links
112121
{}.tap do |hash|
113122
hash[:data] = []
114-
hash[:links] = {}
123+
hash.merge! links: empty_collection_links
115124
end
116125
end
117126

0 commit comments

Comments
 (0)