2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
class GroupMembersFinder
|
|
|
|
def initialize(group)
|
|
|
|
@group = group
|
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-11-08 19:23:39 +05:30
|
|
|
def execute(include_descendants: false)
|
2017-08-17 22:00:37 +05:30
|
|
|
group_members = @group.members
|
2018-11-08 19:23:39 +05:30
|
|
|
wheres = []
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
return group_members unless @group.parent || include_descendants
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
wheres << "members.id IN (#{group_members.select(:id).to_sql})"
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
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-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
GroupMember.where(wheres.join(' OR '))
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|