2012-04-15 19:59:39 +02:00
|
|
|
# encoding: utf-8
|
2009-01-13 19:01:56 +01:00
|
|
|
class Workgroup < Group
|
|
|
|
|
|
|
|
has_many :tasks
|
|
|
|
# returns all non-finished tasks
|
2013-09-05 10:16:57 +02:00
|
|
|
has_many :open_tasks, :class_name => 'Task', :conditions => ['done = ?', false], order: 'due_date ASC, name ASC'
|
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
|
|
|
|
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
|
2013-02-24 23:54:59 +01:00
|
|
|
if !role_admin && !Workgroup.where('role_admin = ? AND id != ?', true, 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
|
2009-08-11 18:10:14 +02:00
|
|
|
|
2009-01-13 19:01:56 +01:00
|
|
|
end
|
2011-05-07 20:50:39 +02:00
|
|
|
|