summaryrefslogtreecommitdiff
path: root/app/finders/snippets_finder.rb
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-07-20 12:26:25 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2020-07-20 12:26:25 +0000
commita09983ae35713f5a2bbb100981116d31ce99826e (patch)
tree2ee2af7bd104d57086db360a7e6d8c9d5d43667a /app/finders/snippets_finder.rb
parent18c5ab32b738c0b6ecb4d0df3994000482f34bd8 (diff)
downloadgitlab-ce-a09983ae35713f5a2bbb100981116d31ce99826e.tar.gz
Add latest changes from gitlab-org/gitlab@13-2-stable-ee
Diffstat (limited to 'app/finders/snippets_finder.rb')
-rw-r--r--app/finders/snippets_finder.rb23
1 files changed, 20 insertions, 3 deletions
diff --git a/app/finders/snippets_finder.rb b/app/finders/snippets_finder.rb
index 4f63810423b..941abb70400 100644
--- a/app/finders/snippets_finder.rb
+++ b/app/finders/snippets_finder.rb
@@ -43,7 +43,7 @@ class SnippetsFinder < UnionFinder
include Gitlab::Utils::StrongMemoize
attr_accessor :current_user, :params
- delegate :explore, :only_personal, :only_project, :scope, to: :params
+ delegate :explore, :only_personal, :only_project, :scope, :sort, to: :params
def initialize(current_user = nil, params = {})
@current_user = current_user
@@ -69,7 +69,9 @@ class SnippetsFinder < UnionFinder
items = init_collection
items = by_ids(items)
- items.with_optional_visibility(visibility_from_scope).fresh
+ items = items.with_optional_visibility(visibility_from_scope)
+
+ items.order_by(sort_param)
end
private
@@ -115,7 +117,7 @@ class SnippetsFinder < UnionFinder
queries << snippets_of_authorized_projects if current_user
end
- find_union(queries, Snippet)
+ prepared_union(queries)
end
def snippets_for_a_single_project
@@ -202,6 +204,21 @@ class SnippetsFinder < UnionFinder
params[:project].is_a?(Project) ? params[:project] : Project.find_by_id(params[:project])
end
end
+
+ def sort_param
+ sort.presence || 'id_desc'
+ end
+
+ def prepared_union(queries)
+ return Snippet.none if queries.empty?
+ return queries.first if queries.length == 1
+
+ # The queries are going to be part of a global `where`
+ # therefore we only need to retrieve the `id` column
+ # which will speed the query
+ queries.map! { |rel| rel.select(:id) }
+ Snippet.id_in(find_union(queries, Snippet))
+ end
end
SnippetsFinder.prepend_if_ee('EE::SnippetsFinder')