2021-04-15 05:09:03 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-06-09 11:09:21 -04:00
|
|
|
# Optimization class to fix group member n+1 queries
|
2021-06-14 08:10:13 -04:00
|
|
|
class LastGroupOwnerAssigner
|
|
|
|
def initialize(group, members)
|
|
|
|
@group = group
|
|
|
|
@members = members
|
|
|
|
end
|
2021-04-15 05:09:03 -04:00
|
|
|
|
2021-06-14 08:10:13 -04:00
|
|
|
def execute
|
2022-08-09 20:09:11 -04:00
|
|
|
@last_blocked_owner = no_owners_in_hierarchy? && group.single_blocked_owner?
|
2021-06-14 08:10:13 -04:00
|
|
|
@group_single_owner = owners.size == 1
|
2021-04-15 05:09:03 -04:00
|
|
|
|
2021-06-14 08:10:13 -04:00
|
|
|
members.each { |member| set_last_owner(member) }
|
|
|
|
end
|
2021-04-15 05:09:03 -04:00
|
|
|
|
2021-06-14 08:10:13 -04:00
|
|
|
private
|
2021-04-15 05:09:03 -04:00
|
|
|
|
2021-06-14 08:10:13 -04:00
|
|
|
attr_reader :group, :members, :last_blocked_owner, :group_single_owner
|
2021-04-15 05:09:03 -04:00
|
|
|
|
2022-08-09 20:09:11 -04:00
|
|
|
def no_owners_in_hierarchy?
|
2021-06-14 08:10:13 -04:00
|
|
|
owners.empty?
|
|
|
|
end
|
2021-04-15 05:09:03 -04:00
|
|
|
|
2021-06-14 08:10:13 -04:00
|
|
|
def set_last_owner(member)
|
|
|
|
member.last_owner = member.id.in?(owner_ids) && group_single_owner
|
|
|
|
member.last_blocked_owner = member.id.in?(blocked_owner_ids) && last_blocked_owner
|
|
|
|
end
|
2021-04-15 05:09:03 -04:00
|
|
|
|
2021-06-14 08:10:13 -04:00
|
|
|
def owner_ids
|
|
|
|
@owner_ids ||= owners.where(id: member_ids).ids
|
|
|
|
end
|
2021-04-15 05:09:03 -04:00
|
|
|
|
2021-06-14 08:10:13 -04:00
|
|
|
def blocked_owner_ids
|
|
|
|
@blocked_owner_ids ||= group.blocked_owners.where(id: member_ids).ids
|
|
|
|
end
|
2021-04-15 05:09:03 -04:00
|
|
|
|
2021-06-14 08:10:13 -04:00
|
|
|
def member_ids
|
|
|
|
@members_ids ||= members.pluck(:id)
|
|
|
|
end
|
2021-04-15 05:09:03 -04:00
|
|
|
|
2021-06-14 08:10:13 -04:00
|
|
|
def owners
|
2022-06-09 11:09:21 -04:00
|
|
|
@owners ||= group.all_owners_excluding_project_bots.load
|
2021-04-15 05:09:03 -04:00
|
|
|
end
|
|
|
|
end
|