summaryrefslogtreecommitdiff
path: root/app/finders/group_projects_finder.rb
blob: 84fe468ae5d769b0dc7d81a3cb66a37386b8ec8a (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
class GroupProjectsFinder < UnionFinder
  def initialize(group, options = {})
    @group = group
    @options = options
  end

  def execute(current_user = nil)
    segments = group_projects(current_user)

    find_union(segments, Project)
  end

  private

  def group_projects(current_user)
    include_owned = @options.fetch(:owned, true)
    include_shared = @options.fetch(:shared, true)

    projects = []

    if current_user
      if @group.users.include?(current_user)
        projects << @group.projects if include_owned
        projects << @group.shared_projects if include_shared
      else
        if include_owned
          projects << @group.projects.visible_to_user(current_user)
          projects << @group.projects.public_to_user(current_user)
        end

        if include_shared
          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 if include_owned
      projects << @group.shared_projects.public_only if include_shared
    end

    projects
  end
end