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

134 lines
3.1 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
2017-09-10 17:25:29 +05:30
# owned: boolean
2017-08-17 22:00:37 +05:30
# 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
2017-09-10 17:25:29 +05:30
user = params.delete(:user)
collection =
if user
PersonalProjectsFinder.new(user).execute(current_user)
else
init_collection
end
collection = by_ids(collection)
collection = by_personal(collection)
collection = by_starred(collection)
collection = by_trending(collection)
collection = by_visibilty_level(collection)
collection = by_tags(collection)
collection = by_search(collection)
collection = by_archived(collection)
sort(collection)
2014-09-02 18:07:02 +05:30
end
private
2017-08-17 22:00:37 +05:30
def init_collection
2017-09-10 17:25:29 +05:30
if current_user
collection_with_user
else
collection_without_user
end
end
def collection_with_user
if owned_projects?
current_user.owned_projects
else
if private_only?
current_user.authorized_projects
else
Project.public_or_visible_to_user(current_user)
end
end
end
2015-11-26 14:37:03 +05:30
2017-09-10 17:25:29 +05:30
# Builds a collection for an anonymous user.
def collection_without_user
if private_only? || owned_projects?
Project.none
2017-08-17 22:00:37 +05:30
else
2017-09-10 17:25:29 +05:30
Project.public_to_user
2017-08-17 22:00:37 +05:30
end
2017-09-10 17:25:29 +05:30
end
def owned_projects?
params[:owned].present?
end
2015-11-26 14:37:03 +05:30
2017-09-10 17:25:29 +05:30
def private_only?
params[:non_public].present?
2015-11-26 14:37:03 +05:30
end
2017-08-17 22:00:37 +05:30
def by_ids(items)
2017-09-10 17:25:29 +05:30
project_ids_relation ? items.where(id: project_ids_relation) : items
2017-08-17 22:00:37 +05:30
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
2017-09-10 17:25:29 +05:30
def by_starred(items)
(params[:starred].present? && current_user) ? items.starred_by(current_user) : items
end
def by_trending(items)
params[:trending].present? ? items.trending : items
end
2017-08-17 22:00:37 +05:30
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