2012-04-15 19:59:39 +02:00
|
|
|
# encoding: utf-8
|
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
|
2014-05-06 10:51:45 +02:00
|
|
|
has_many :open_tasks, -> { where(:done => false).order('due_date', 'name') }, :class_name => 'Task'
|
2009-01-13 19:01:56 +01:00
|
|
|
|
2013-02-24 23:26:16 +01:00
|
|
|
validates_uniqueness_of :name
|
2011-06-10 13:22:15 +02:00
|
|
|
validate :last_admin_on_earth, :on => :update
|
|
|
|
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
|
2014-02-20 15:04:53 +01:00
|
|
|
if role_admin && Workgroup.where(role_admin: true).size == 1
|
2013-02-22 00:19:22 +01:00
|
|
|
raise I18n.t('workgroups.error_last_admin_group')
|
2011-06-10 13:22:15 +02:00
|
|
|
end
|
|
|
|
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
|
2014-02-20 15:04:53 +01:00
|
|
|
if !role_admin && !Workgroup.where(role_admin: true).where.not(id: id).exists?
|
2013-02-22 00:19:22 +01:00
|
|
|
errors.add(:role_admin, I18n.t('workgroups.error_last_admin_role'))
|
2011-06-10 13:22:15 +02:00
|
|
|
end
|
|
|
|
end
|
2011-05-07 20:50:39 +02:00
|
|
|
|
2017-10-12 20:50:40 +02:00
|
|
|
end
|