Group's `last_owner?` method may not return correct result
Related to https://gitlab.zendesk.com/agent/tickets/21590
Summary
Due to memoization it looks like Group's last_owner?
method may return true
when it should return false
in cases where last_owner?
is called multiple times in between modifying group membership.
Steps to reproduce
In EE, group sync relies on last_owner?
to determine if it should demote an owner or not (we don't want to demote the last owner). Customers have reported strange cases where the owner is actually demoted.
Expected behavior
last_owner?
should return the correct value, no matter how many times it's called on the same group object.
I think the actual issue is because of the following method:
def owners
@owners ||= group_members.owners.includes(:user).map(&:user)
end
Given the last_owner?
method of:
def last_owner?(user)
has_owner?(user) && owners.size == 1
end
size
is going to be wrong on subsequent calls if we've since deleted the second to last owner.