mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Always display self, first, last pagination links
This commit is contained in:
parent
dff621e174
commit
7387266c37
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user