2019-01-13 07:05:54 +01:00
|
|
|
class Membership < ApplicationRecord
|
2009-01-06 11:49:19 +01:00
|
|
|
belongs_to :user
|
|
|
|
belongs_to :group
|
2011-05-15 23:43:23 +02:00
|
|
|
|
|
|
|
before_destroy :check_last_admin
|
2019-01-13 07:05:54 +01:00
|
|
|
|
2011-05-15 23:43:23 +02:00
|
|
|
protected
|
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# check if this is the last admin-membership and deny
|
2011-05-15 23:43:23 +02:00
|
|
|
def check_last_admin
|
2023-05-12 13:01:12 +02:00
|
|
|
raise I18n.t('model.membership.no_admin_delete') if group.role_admin? && group.memberships.size == 1 && Group.where(role_admin: true).count == 1
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
end
|