2018-09-11 15:08:34 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-03 05:35:04 -05:00
|
|
|
# GroupProjectsFinder
|
|
|
|
#
|
|
|
|
# Used to filter Projects by set of params
|
|
|
|
#
|
|
|
|
# Arguments:
|
|
|
|
# current_user - which user use
|
|
|
|
# project_ids_relation: int[] - project ids to use
|
|
|
|
# group
|
|
|
|
# options:
|
|
|
|
# only_owned: boolean
|
|
|
|
# only_shared: boolean
|
|
|
|
# params:
|
|
|
|
# sort: string
|
|
|
|
# visibility_level: int
|
|
|
|
# tags: string[]
|
|
|
|
# personal: boolean
|
|
|
|
# search: string
|
|
|
|
# non_archived: boolean
|
|
|
|
#
|
|
|
|
class GroupProjectsFinder < ProjectsFinder
|
|
|
|
attr_reader :group, :options
|
|
|
|
|
|
|
|
def initialize(group:, params: {}, options: {}, current_user: nil, project_ids_relation: nil)
|
|
|
|
super(params: params, current_user: current_user, project_ids_relation: project_ids_relation)
|
2016-03-21 18:11:24 -04:00
|
|
|
@group = group
|
2016-03-20 16:03:53 -04:00
|
|
|
@options = options
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-03-03 05:35:04 -05:00
|
|
|
def init_collection
|
2017-06-15 09:19:25 -04:00
|
|
|
projects = if current_user
|
|
|
|
collection_with_user
|
|
|
|
else
|
|
|
|
collection_without_user
|
|
|
|
end
|
2018-01-11 11:34:01 -05:00
|
|
|
|
2017-06-15 09:19:25 -04:00
|
|
|
union(projects)
|
|
|
|
end
|
2016-03-20 16:03:53 -04:00
|
|
|
|
2017-06-15 09:19:25 -04:00
|
|
|
def collection_with_user
|
2018-05-17 10:02:07 -04:00
|
|
|
if only_shared?
|
|
|
|
[shared_projects.public_or_visible_to_user(current_user)]
|
|
|
|
elsif only_owned?
|
|
|
|
[owned_projects.public_or_visible_to_user(current_user)]
|
2016-03-20 16:03:53 -04:00
|
|
|
else
|
2018-05-17 10:02:07 -04:00
|
|
|
[
|
|
|
|
owned_projects.public_or_visible_to_user(current_user),
|
|
|
|
shared_projects.public_or_visible_to_user(current_user)
|
|
|
|
]
|
2016-03-20 16:03:53 -04:00
|
|
|
end
|
2017-06-15 09:19:25 -04:00
|
|
|
end
|
2016-03-20 16:03:53 -04:00
|
|
|
|
2017-06-15 09:19:25 -04:00
|
|
|
def collection_without_user
|
|
|
|
if only_shared?
|
|
|
|
[shared_projects.public_only]
|
|
|
|
elsif only_owned?
|
|
|
|
[owned_projects.public_only]
|
|
|
|
else
|
|
|
|
[shared_projects.public_only, owned_projects.public_only]
|
|
|
|
end
|
2016-03-20 16:03:53 -04:00
|
|
|
end
|
2017-03-03 05:35:04 -05:00
|
|
|
|
|
|
|
def union(items)
|
2017-06-15 09:19:25 -04:00
|
|
|
if items.one?
|
|
|
|
items.first
|
|
|
|
else
|
|
|
|
find_union(items, Project)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def only_owned?
|
|
|
|
options.fetch(:only_owned, false)
|
|
|
|
end
|
|
|
|
|
|
|
|
def only_shared?
|
|
|
|
options.fetch(:only_shared, false)
|
|
|
|
end
|
|
|
|
|
2018-01-24 01:06:24 -05:00
|
|
|
# subgroups are supported only for owned projects not for shared
|
|
|
|
def include_subgroups?
|
|
|
|
options.fetch(:include_subgroups, false)
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-06-15 09:19:25 -04:00
|
|
|
def owned_projects
|
2018-01-24 01:06:24 -05:00
|
|
|
if include_subgroups?
|
|
|
|
Project.where(namespace_id: group.self_and_descendants.select(:id))
|
|
|
|
else
|
|
|
|
group.projects
|
|
|
|
end
|
2017-06-15 09:19:25 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-06-15 09:19:25 -04:00
|
|
|
|
|
|
|
def shared_projects
|
|
|
|
group.shared_projects
|
2017-03-03 05:35:04 -05:00
|
|
|
end
|
2016-03-20 16:03:53 -04:00
|
|
|
end
|