Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBob Van Landuyt <bob@vanlanduyt.co>2017-09-04 19:04:33 +0300
committerBob Van Landuyt <bob@vanlanduyt.co>2017-10-04 23:46:49 +0300
commitca538899b66a6a82582d2d590297cfef1d310dcf (patch)
tree7ecddf4e410aff8afde79345364b536976626385 /app/serializers/concerns
parent063b5312111ccea62f84fa9f68a2262dc1f66e64 (diff)
Add a `WithPagination` concern to reuse across serializers
Diffstat (limited to 'app/serializers/concerns')
-rw-r--r--app/serializers/concerns/with_pagination.rb20
1 files changed, 20 insertions, 0 deletions
diff --git a/app/serializers/concerns/with_pagination.rb b/app/serializers/concerns/with_pagination.rb
new file mode 100644
index 00000000000..484c6855f7c
--- /dev/null
+++ b/app/serializers/concerns/with_pagination.rb
@@ -0,0 +1,20 @@
+module WithPagination
+ def with_pagination(request, response)
+ tap { @paginator = Gitlab::Serializer::Pagination.new(request, response) }
+ end
+
+ def paginated?
+ @paginator.present?
+ end
+
+ # super is `BaseSerializer#represent` here.
+ #
+ # we shouldn't try to paginate single resources
+ def represent(resource, opts = {})
+ if paginated? && resource.respond_to?(:page)
+ super(@paginator.paginate(resource), opts)
+ else
+ super(resource, opts)
+ end
+ end
+end