2021-04-17 20:07:23 +05:30
|
|
|
# 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[]
|
2023-04-23 21:23:45 +05:30
|
|
|
# with_issues_enabled: boolean
|
|
|
|
# with_merge_requests_enabled: boolean
|
2021-04-17 20:07:23 +05:30
|
|
|
#
|
|
|
|
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
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
collection = filter_projects(collection)
|
|
|
|
|
|
|
|
sort(collection)
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :namespace, :params, :current_user
|
|
|
|
|
|
|
|
def filter_projects(collection)
|
|
|
|
collection = by_ids(collection)
|
2023-04-23 21:23:45 +05:30
|
|
|
collection = by_similarity(collection)
|
|
|
|
by_feature_availability(collection)
|
2021-04-17 20:07:23 +05:30
|
|
|
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?
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
items.merge(Project.search(params[:search]))
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_feature_availability(items)
|
|
|
|
items = items.with_issues_available_for_user(current_user) if params[:with_issues_enabled].present?
|
|
|
|
if params[:with_merge_requests_enabled].present?
|
|
|
|
items = items.with_merge_requests_available_for_user(current_user)
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
items
|
|
|
|
end
|
|
|
|
|
|
|
|
def sort(items)
|
|
|
|
return items.projects_order_id_desc unless params[:sort]
|
|
|
|
|
|
|
|
if params[:sort] == :similarity && params[:search].present?
|
|
|
|
return items.sorted_by_similarity_desc(params[:search], include_in_select: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
items.sort_by_attribute(params[:sort])
|
2021-04-17 20:07:23 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
Namespaces::ProjectsFinder.prepend_mod_with('Namespaces::ProjectsFinder')
|