BigW Consortium Gitlab

group_projects_finder.rb 2.06 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
# 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)
25
    @group   = group
26 27 28 29 30
    @options = options
  end

  private

31
  def init_collection
32 33 34 35 36
    projects = if current_user
                 collection_with_user
               else
                 collection_without_user
               end
37

38 39
    union(projects)
  end
40

41 42 43 44 45 46
  def collection_with_user
    if group.users.include?(current_user)
      if only_shared?
        [shared_projects]
      elsif only_owned?
        [owned_projects]
47
      else
48
        [shared_projects, owned_projects]
49 50
      end
    else
51 52 53 54 55 56 57 58 59 60
      if only_shared?
        [shared_projects.public_or_visible_to_user(current_user)]
      elsif only_owned?
        [owned_projects.public_or_visible_to_user(current_user)]
      else
        [
          owned_projects.public_or_visible_to_user(current_user),
          shared_projects.public_or_visible_to_user(current_user)
        ]
      end
61
    end
62
  end
63

64 65 66 67 68 69 70 71
  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
72
  end
73 74

  def union(items)
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    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

  def owned_projects
    group.projects
  end

  def shared_projects
    group.shared_projects
96
  end
97
end