summaryrefslogtreecommitdiff
path: root/lib/api/helpers/pagination_strategies.rb
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-01-10 15:07:47 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-01-10 15:07:47 +0000
commit8b1228b0d409d7751f01d9fb72ebfbbf62399486 (patch)
tree1b4126fe48d7666a90c0d7ee26230cf8379b6410 /lib/api/helpers/pagination_strategies.rb
parent96b0c1245c93585a8b0fe23e22306d32ff4e4905 (diff)
downloadgitlab-ce-8b1228b0d409d7751f01d9fb72ebfbbf62399486.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib/api/helpers/pagination_strategies.rb')
-rw-r--r--lib/api/helpers/pagination_strategies.rb33
1 files changed, 33 insertions, 0 deletions
diff --git a/lib/api/helpers/pagination_strategies.rb b/lib/api/helpers/pagination_strategies.rb
new file mode 100644
index 00000000000..5f63635297a
--- /dev/null
+++ b/lib/api/helpers/pagination_strategies.rb
@@ -0,0 +1,33 @@
+# frozen_string_literal: true
+
+module API
+ module Helpers
+ module PaginationStrategies
+ def paginate_with_strategies(relation)
+ paginator = paginator(relation)
+
+ yield(paginator.paginate(relation)).tap do |records, _|
+ paginator.finalize(records)
+ end
+ end
+
+ def paginator(relation)
+ return Gitlab::Pagination::OffsetPagination.new(self) unless keyset_pagination_enabled?
+
+ request_context = Gitlab::Pagination::Keyset::RequestContext.new(self)
+
+ unless Gitlab::Pagination::Keyset.available?(request_context, relation)
+ return error!('Keyset pagination is not yet available for this type of request', 405)
+ end
+
+ Gitlab::Pagination::Keyset::Pager.new(request_context)
+ end
+
+ private
+
+ def keyset_pagination_enabled?
+ params[:pagination] == 'keyset' && Feature.enabled?(:api_keyset_pagination, default_enabled: true)
+ end
+ end
+ end
+end