diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-03-08 18:08:54 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-03-08 18:08:54 +0300 |
commit | 770d6dbfa7111324f994f12664a2036c7121602a (patch) | |
tree | 6611d1a22e646f47819053ca5e8aeaceb9cbee4f /app/finders | |
parent | eb2d243280ff0fd72d097a1426c05ce1a3501914 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/finders')
-rw-r--r-- | app/finders/namespaces/projects_finder.rb | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/app/finders/namespaces/projects_finder.rb b/app/finders/namespaces/projects_finder.rb new file mode 100644 index 00000000000..a6d98015e9d --- /dev/null +++ b/app/finders/namespaces/projects_finder.rb @@ -0,0 +1,64 @@ +# frozen_string_literal: true + +# Namespaces::ProjectsFinder +# +# Used to filter Projects by set of params +# +# Arguments: +# current_user +# namespace +# params: +# sort: string +# search: string +# include_subgroups: boolean +# ids: int[] +# +module Namespaces + class ProjectsFinder + def initialize(namespace: nil, current_user: nil, params: {}) + @namespace = namespace + @current_user = current_user + @params = params + end + + def execute + return Project.none if namespace.nil? + + collection = if params[:include_subgroups].present? + namespace.all_projects.with_route + else + namespace.projects.with_route + end + + filter_projects(collection) + end + + private + + attr_reader :namespace, :params, :current_user + + def filter_projects(collection) + collection = by_ids(collection) + collection = by_similarity(collection) + collection + end + + def by_ids(items) + return items unless params[:ids].present? + + items.id_in(params[:ids]) + end + + def by_similarity(items) + return items unless params[:search].present? + + if params[:sort] == :similarity + items = items.sorted_by_similarity_desc(params[:search], include_in_select: true) + end + + items.merge(Project.search(params[:search])) + end + end +end + +Namespaces::ProjectsFinder.prepend_if_ee('::EE::Namespaces::ProjectsFinder') |