mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Amend tests to always include all pagination keys
This commit is contained in:
@@ -3,6 +3,7 @@ module ActiveModelSerializers
|
||||
class JsonApi < Base
|
||||
class PaginationLinks
|
||||
MissingSerializationContextError = Class.new(KeyError)
|
||||
FIRST_PAGE = 1
|
||||
|
||||
attr_reader :collection, :context
|
||||
|
||||
@@ -42,20 +43,36 @@ module ActiveModelSerializers
|
||||
url_for_page(1)
|
||||
end
|
||||
|
||||
def last_page_url
|
||||
if collection.total_pages == 0
|
||||
url_for_page(FIRST_PAGE)
|
||||
else
|
||||
url_for_page(collection.total_pages)
|
||||
end
|
||||
end
|
||||
|
||||
def prev_page_url
|
||||
return nil if collection.first_page?
|
||||
url_for_page(collection.prev_page)
|
||||
return nil if collection.current_page == FIRST_PAGE
|
||||
url_for_page(collection.current_page - FIRST_PAGE)
|
||||
end
|
||||
|
||||
def next_page_url
|
||||
return nil if collection.last_page? || collection.out_of_range?
|
||||
return nil if (collection.total_pages == 0 || collection.current_page == collection.total_pages)
|
||||
url_for_page(collection.next_page)
|
||||
end
|
||||
|
||||
def url_for_page(number)
|
||||
params = query_parameters.dup
|
||||
params[:page] = { page: per_page, number: number }
|
||||
context.url_for(action: :index, params: params)
|
||||
params[:page] = { size: per_page, number: number }
|
||||
"#{url(adapter_options)}?#{params.to_query}"
|
||||
end
|
||||
|
||||
def url(options = {})
|
||||
@url ||= options.fetch(:links, {}).fetch(:self, nil) || request_url
|
||||
end
|
||||
|
||||
def request_url
|
||||
@request_url ||= context.request_url
|
||||
end
|
||||
|
||||
def query_parameters
|
||||
|
||||
Reference in New Issue
Block a user