debian-mirror-gitlab/app/finders/projects_finder.rb

94 lines
2.4 KiB
Ruby
Raw Normal View History

2017-08-17 22:00:37 +05:30
# ProjectsFinder
#
# Used to filter Projects by set of params
#
# Arguments:
# current_user - which user use
# project_ids_relation: int[] - project ids to use
# params:
# trending: boolean
# non_public: boolean
# starred: boolean
# sort: string
# visibility_level: int
# tags: string[]
# personal: boolean
# search: string
# non_archived: boolean
#
2016-06-02 11:05:42 +05:30
class ProjectsFinder < UnionFinder
2017-08-17 22:00:37 +05:30
attr_accessor :params
attr_reader :current_user, :project_ids_relation
2014-09-02 18:07:02 +05:30
2017-08-17 22:00:37 +05:30
def initialize(params: {}, current_user: nil, project_ids_relation: nil)
@params = params
@current_user = current_user
@project_ids_relation = project_ids_relation
end
def execute
items = init_collection
items = by_ids(items)
items = union(items)
items = by_personal(items)
items = by_visibilty_level(items)
items = by_tags(items)
items = by_search(items)
items = by_archived(items)
sort(items)
2014-09-02 18:07:02 +05:30
end
private
2017-08-17 22:00:37 +05:30
def init_collection
2016-06-02 11:05:42 +05:30
projects = []
2015-11-26 14:37:03 +05:30
2017-08-17 22:00:37 +05:30
if params[:trending].present?
projects << Project.trending
elsif params[:starred].present? && current_user
projects << current_user.viewable_starred_projects
else
projects << current_user.authorized_projects if current_user
projects << Project.unscoped.public_to_user(current_user) unless params[:non_public].present?
end
2015-11-26 14:37:03 +05:30
2016-06-02 11:05:42 +05:30
projects
2015-11-26 14:37:03 +05:30
end
2017-08-17 22:00:37 +05:30
def by_ids(items)
project_ids_relation ? items.map { |item| item.where(id: project_ids_relation) } : items
end
def union(items)
find_union(items, Project).with_route
end
def by_personal(items)
(params[:personal].present? && current_user) ? items.personal(current_user) : items
end
def by_visibilty_level(items)
params[:visibility_level].present? ? items.where(visibility_level: params[:visibility_level]) : items
end
def by_tags(items)
params[:tag].present? ? items.tagged_with(params[:tag]) : items
end
def by_search(items)
params[:search] ||= params[:name]
params[:search].present? ? items.search(params[:search]) : items
end
def sort(items)
params[:sort].present? ? items.sort(params[:sort]) : items
end
def by_archived(projects)
# Back-compatibility with the places where `params[:archived]` can be set explicitly to `false`
params[:non_archived] = !Gitlab::Utils.to_boolean(params[:archived]) if params.key?(:archived)
params[:non_archived] ? projects.non_archived : projects
end
2014-09-02 18:07:02 +05:30
end