summaryrefslogtreecommitdiff
path: root/app/finders/group_members_finder.rb
blob: fce3775f40e57fc51f1124bff256e0f3128738c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class GroupMembersFinder
  def initialize(group)
    @group = group
  end

  def execute
    group_members = @group.members

    return group_members unless @group.parent

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

    wheres = ["members.id IN (#{group_members.select(:id).to_sql})"]
    wheres << "members.id IN (#{parents_members.select(:id).to_sql})"

    GroupMember.where(wheres.join(' OR '))
  end
end