2009-01-13 19:01:56 +01:00
|
|
|
class Workgroup < Group
|
2017-10-12 20:50:40 +02:00
|
|
|
include CustomFields
|
|
|
|
|
2009-01-13 19:01:56 +01:00
|
|
|
has_many :tasks
|
|
|
|
# returns all non-finished tasks
|
2023-05-12 13:01:12 +02:00
|
|
|
has_many :open_tasks, -> { where(done: false).order('due_date', 'name') }, class_name: 'Task'
|
2009-01-13 19:01:56 +01:00
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
validates :name, uniqueness: true
|
|
|
|
validate :last_admin_on_earth, on: :update
|
2011-06-10 13:22:15 +02:00
|
|
|
before_destroy :check_last_admin_group
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
# Check before destroy a group, if this is the last group with admin role
|
|
|
|
def check_last_admin_group
|
2023-05-12 13:01:12 +02:00
|
|
|
return unless role_admin && Workgroup.where(role_admin: true).size == 1
|
|
|
|
|
|
|
|
raise I18n.t('workgroups.error_last_admin_group')
|
2011-06-10 13:22:15 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# add validation check on update
|
|
|
|
# Return an error if this is the last group with admin role and role_admin should set to false
|
|
|
|
def last_admin_on_earth
|
2023-05-12 13:01:12 +02:00
|
|
|
return unless !role_admin && !Workgroup.where(role_admin: true).where.not(id: id).exists?
|
|
|
|
|
|
|
|
errors.add(:role_admin, I18n.t('workgroups.error_last_admin_role'))
|
2011-06-10 13:22:15 +02:00
|
|
|
end
|
2017-10-12 20:50:40 +02:00
|
|
|
end
|