2021-04-29 21:17:54 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
# Optimization class to fix group member n+1 queries
|
2021-09-04 01:27:46 +05:30
|
|
|
class LastGroupOwnerAssigner
|
|
|
|
def initialize(group, members)
|
|
|
|
@group = group
|
|
|
|
@members = members
|
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
def execute
|
2022-08-27 11:52:29 +05:30
|
|
|
@last_blocked_owner = no_owners_in_hierarchy? && group.single_blocked_owner?
|
2021-09-04 01:27:46 +05:30
|
|
|
@group_single_owner = owners.size == 1
|
2021-04-29 21:17:54 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
members.each { |member| set_last_owner(member) }
|
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
private
|
2021-04-29 21:17:54 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
attr_reader :group, :members, :last_blocked_owner, :group_single_owner
|
2021-04-29 21:17:54 +05:30
|
|
|
|
2022-08-27 11:52:29 +05:30
|
|
|
def no_owners_in_hierarchy?
|
2021-09-04 01:27:46 +05:30
|
|
|
owners.empty?
|
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
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-29 21:17:54 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
def owner_ids
|
|
|
|
@owner_ids ||= owners.where(id: member_ids).ids
|
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
def blocked_owner_ids
|
|
|
|
@blocked_owner_ids ||= group.blocked_owners.where(id: member_ids).ids
|
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
def member_ids
|
|
|
|
@members_ids ||= members.pluck(:id)
|
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
def owners
|
2022-07-23 23:45:48 +05:30
|
|
|
@owners ||= group.all_owners_excluding_project_bots.load
|
2021-04-29 21:17:54 +05:30
|
|
|
end
|
|
|
|
end
|