Always display self, first, last pagination links

This commit is contained in:
mecampbellsoup 2017-06-05 17:23:34 -04:00
parent dff621e174
commit 7387266c37
2 changed files with 22 additions and 11 deletions

View File

@ -35,19 +35,21 @@ module ActiveModelSerializers
private
def pages_from
return {} if collection.total_pages <= FIRST_PAGE
{}.tap do |pages|
pages[:self] = collection.current_page
unless collection.current_page == FIRST_PAGE
pages[:first] = FIRST_PAGE
pages[:last] = collection.total_pages
if collection.total_pages > 0
unless collection.current_page == FIRST_PAGE
pages[:prev] = collection.current_page - FIRST_PAGE
end
unless collection.current_page == collection.total_pages
pages[:next] = collection.current_page + FIRST_PAGE
pages[:last] = collection.total_pages
end
else
pages[:last] = FIRST_PAGE
end
end
end

View File

@ -54,6 +54,14 @@ module ActiveModelSerializers
}
end
def empty_collection_links
{
self: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
first: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
last: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2"
}
end
def links
{
links: {
@ -71,6 +79,7 @@ module ActiveModelSerializers
links: {
self: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=2",
first: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
last: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=2",
prev: "#{URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2"
}
}
@ -111,7 +120,7 @@ module ActiveModelSerializers
def expected_response_with_no_data_pagination_links
{}.tap do |hash|
hash[:data] = []
hash[:links] = {}
hash.merge! links: empty_collection_links
end
end