2018-09-11 15:08:34 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-07 06:58:14 -05:00
|
|
|
class GroupMembersFinder
|
2017-02-08 10:02:25 -05:00
|
|
|
def initialize(group)
|
|
|
|
@group = group
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-06-07 16:54:24 -04:00
|
|
|
def execute(include_descendants: false)
|
2017-02-08 10:02:25 -05:00
|
|
|
group_members = @group.members
|
2018-06-07 16:54:24 -04:00
|
|
|
wheres = []
|
2017-02-08 10:02:25 -05:00
|
|
|
|
2018-06-07 16:54:24 -04:00
|
|
|
return group_members unless @group.parent || include_descendants
|
2017-02-08 10:02:25 -05:00
|
|
|
|
2018-06-07 16:54:24 -04:00
|
|
|
wheres << "members.id IN (#{group_members.select(:id).to_sql})"
|
2017-02-08 10:02:25 -05:00
|
|
|
|
2018-06-07 16:54:24 -04:00
|
|
|
if @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 (#{parents_members.select(:id).to_sql})"
|
|
|
|
end
|
|
|
|
|
|
|
|
if include_descendants
|
|
|
|
descendant_members = GroupMember.non_request
|
|
|
|
.where(source_id: @group.descendants.select(:id))
|
|
|
|
.where.not(user_id: @group.users.select(:id))
|
|
|
|
|
|
|
|
wheres << "members.id IN (#{descendant_members.select(:id).to_sql})"
|
|
|
|
end
|
2017-02-08 10:02:25 -05:00
|
|
|
|
|
|
|
GroupMember.where(wheres.join(' OR '))
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-02-08 10:02:25 -05:00
|
|
|
end
|