summaryrefslogtreecommitdiff
path: root/app/finders/group_members_finder.rb
blob: 33ec6a715f92e984105a529c48ee7da7111dd17e (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
# frozen_string_literal: true

class GroupMembersFinder < UnionFinder
  def initialize(group)
    @group = group
  end

  # rubocop: disable CodeReuse/ActiveRecord
  def execute(include_descendants: false)
    group_members = @group.members
    relations = []

    return group_members unless @group.parent || include_descendants

    relations << group_members

    if @group.parent
      parents_members = GroupMember.non_request
        .where(source_id: @group.ancestors.select(:id))
        .where.not(user_id: @group.users.select(:id))

      relations << parents_members
    end

    if include_descendants
      descendant_members = GroupMember.non_request
        .where(source_id: @group.descendants.select(:id))
        .where.not(user_id: @group.users.select(:id))

      relations << descendant_members
    end

    find_union(relations, GroupMember)
  end
  # rubocop: enable CodeReuse/ActiveRecord
end