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
|
|
|
|
has_many :open_tasks, :class_name => 'Task', :conditions => ['done = ?', false], :order => 'due_date ASC'
|
|
|
|
|
2013-02-24 23:26:16 +01:00
|
|
|
validates_uniqueness_of :name
|
2012-06-24 11:01:16 +02:00
|
|
|
validates_presence_of :task_name, :weekday, :task_required_users, :next_weekly_tasks_number,
|
2012-08-24 11:11:40 +02:00
|
|
|
:if => :weekly_task
|
2012-06-24 11:01:16 +02:00
|
|
|
validates_numericality_of :next_weekly_tasks_number, :greater_than => 0, :less_than => 21, :only_integer => true,
|
2012-08-24 11:11:40 +02:00
|
|
|
:if => :weekly_task
|
|
|
|
validates_length_of :task_description, maximum: 250
|
2011-06-10 13:22:15 +02:00
|
|
|
validate :last_admin_on_earth, :on => :update
|
|
|
|
before_destroy :check_last_admin_group
|
|
|
|
|
2009-01-13 19:01:56 +01:00
|
|
|
def self.weekdays
|
2013-02-22 00:19:22 +01:00
|
|
|
days = I18n.t('date.day_names')
|
|
|
|
(0..days.length-1).map {|i| [days[i], i.to_s]}
|
2009-01-13 19:01:56 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns an Array with date-objects to represent the next weekly-tasks
|
2012-06-24 11:01:16 +02:00
|
|
|
def next_weekly_tasks
|
2009-01-13 19:01:56 +01:00
|
|
|
# our system starts from 0 (sunday) to 6 (saturday)
|
|
|
|
# get difference between groups weekday and now
|
|
|
|
diff = self.weekday - Time.now.wday
|
|
|
|
if diff >= 0
|
|
|
|
# weektask is in current week
|
|
|
|
nextTask = diff.day.from_now
|
|
|
|
else
|
|
|
|
# weektask is in the next week
|
|
|
|
nextTask = (diff + 7).day.from_now
|
|
|
|
end
|
|
|
|
# now generate the Array
|
|
|
|
nextTasks = Array.new
|
2012-06-24 11:01:16 +02:00
|
|
|
next_weekly_tasks_number.times do
|
2009-08-11 18:10:14 +02:00
|
|
|
nextTasks << nextTask.to_date
|
|
|
|
nextTask = 1.week.from_now(nextTask)
|
2009-01-13 19:01:56 +01:00
|
|
|
end
|
|
|
|
return nextTasks
|
|
|
|
end
|
|
|
|
|
2009-08-11 17:17:18 +02:00
|
|
|
def task_attributes(date)
|
|
|
|
{
|
2011-05-07 21:54:00 +02:00
|
|
|
:name => task_name,
|
|
|
|
:description => task_description,
|
|
|
|
:due_date => date,
|
|
|
|
:required_users => task_required_users,
|
|
|
|
:duration => task_duration,
|
|
|
|
:weekly => true
|
2009-08-11 17:17:18 +02:00
|
|
|
}
|
|
|
|
end
|
2011-06-10 13:22:15 +02:00
|
|
|
|
|
|
|
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
|
|
|
|