summaryrefslogtreecommitdiff
path: root/app/finders/joined_groups_finder.rb
blob: 2a3f0296d37d6b1df69377d3d9a6dcd1501f5d06 (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
class JoinedGroupsFinder < UnionFinder
  def initialize(user)
    @user = user
  end

  # Finds the groups of the source user, optionally limited to those visible to
  # the current user.
  #
  # current_user - If given the groups of "@user" will only include the groups
  #                "current_user" can also see.
  #
  # Returns an ActiveRecord::Relation.
  def execute(current_user = nil)
    segments = all_groups(current_user)

    find_union(segments, Group).order_id_desc
  end

  private

  def all_groups(current_user)
    groups = []

    groups << @user.authorized_groups.visible_to_user(current_user) if current_user
    groups << @user.authorized_groups.public_to_user(current_user)

    groups
  end
end