group_projects_finder.rb 2.11 KB
Newer Older
1 2
# frozen_string_literal: true

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
# 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)
26 27 28 29 30 31
    super(
      params: params,
      current_user: current_user,
      project_ids_relation: project_ids_relation
    )
    @group = group
32 33 34 35 36
    @options = options
  end

  private

37
  def init_collection
38 39 40 41 42
    projects = if current_user
                 collection_with_user
               else
                 collection_without_user
               end
43

44 45
    union(projects)
  end
46

47
  def collection_with_user
48 49 50 51
    if only_shared?
      [shared_projects.public_or_visible_to_user(current_user)]
    elsif only_owned?
      [owned_projects.public_or_visible_to_user(current_user)]
52
    else
53 54 55 56
      [
        owned_projects.public_or_visible_to_user(current_user),
        shared_projects.public_or_visible_to_user(current_user)
      ]
57
    end
58
  end
59

60 61 62 63 64 65 66 67
  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
68
  end
69 70

  def union(items)
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    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

86 87 88 89 90
  # subgroups are supported only for owned projects not for shared
  def include_subgroups?
    options.fetch(:include_subgroups, false)
  end

91
  def owned_projects
92
    if include_subgroups?
93
      Project.for_group_and_its_subgroups(group)
94 95 96
    else
      group.projects
    end
97 98 99 100
  end

  def shared_projects
    group.shared_projects
101
  end
102
end