summaryrefslogtreecommitdiff
path: root/app/finders/group_projects_finder.rb
blob: f043c38c6f9d878eec2e46faefe55cd149aea237 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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)
    @group   = group
    @options = options
  end

  private

  def init_collection
    only_owned  = options.fetch(:only_owned, false)
    only_shared = options.fetch(:only_shared, false)

    projects = []

    if current_user
      if group.users.include?(current_user)
        projects << group.projects unless only_shared
        projects << group.shared_projects unless only_owned
      else
        unless only_shared
          projects << group.projects.visible_to_user(current_user)
          projects << group.projects.public_to_user(current_user)
        end

        unless only_owned
          projects << group.shared_projects.visible_to_user(current_user)
          projects << group.shared_projects.public_to_user(current_user)
        end
      end
    else
      projects << group.projects.public_only unless only_shared
      projects << group.shared_projects.public_only unless only_owned
    end

    projects
  end

  def union(items)
    find_union(items, Project)
  end
end