2018-07-19 14:43:13 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-04 12:04:33 -04:00
|
|
|
module WithPagination
|
2017-09-15 06:28:21 -04:00
|
|
|
attr_accessor :paginator
|
|
|
|
|
2017-09-04 12:04:33 -04:00
|
|
|
def with_pagination(request, response)
|
2017-09-15 06:28:21 -04:00
|
|
|
tap { self.paginator = Gitlab::Serializer::Pagination.new(request, response) }
|
2017-09-04 12:04:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def paginated?
|
2017-09-15 06:28:21 -04:00
|
|
|
paginator.present?
|
2017-09-04 12:04:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# super is `BaseSerializer#represent` here.
|
|
|
|
#
|
|
|
|
# we shouldn't try to paginate single resources
|
|
|
|
def represent(resource, opts = {})
|
|
|
|
if paginated? && resource.respond_to?(:page)
|
2017-11-17 07:27:16 -05:00
|
|
|
super(paginator.paginate(resource), opts)
|
2017-09-04 12:04:33 -04:00
|
|
|
else
|
|
|
|
super(resource, opts)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|