2012-11-28 10:13:54 +01:00
|
|
|
class PeriodicTaskGroup < ActiveRecord::Base
|
2013-06-21 22:04:36 +02:00
|
|
|
has_many :tasks, dependent: :destroy
|
2012-11-28 10:13:54 +01:00
|
|
|
|
|
|
|
PeriodDays = 7
|
|
|
|
|
|
|
|
def has_next_task?
|
2013-06-21 22:04:36 +02:00
|
|
|
return false if tasks.empty?
|
|
|
|
return false if tasks.first.due_date.nil?
|
2012-11-28 10:13:54 +01:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_next_task
|
2013-06-21 22:04:36 +02:00
|
|
|
template_task = tasks.first
|
2012-11-28 10:13:54 +01:00
|
|
|
self.next_task_date ||= template_task.due_date + PeriodDays
|
|
|
|
|
|
|
|
next_task = template_task.dup
|
2013-06-21 22:04:36 +02:00
|
|
|
next_task.due_date = next_task_date
|
2012-11-28 10:13:54 +01:00
|
|
|
next_task.save
|
|
|
|
|
|
|
|
self.next_task_date += PeriodDays
|
|
|
|
self.save
|
|
|
|
end
|
2013-06-21 22:04:36 +02:00
|
|
|
|
|
|
|
def exclude_tasks_before(task)
|
|
|
|
tasks.where("due_date < '#{task.due_date}'").each do |t|
|
|
|
|
t.update_attribute(:periodic_task_group, nil)
|
|
|
|
end
|
|
|
|
end
|
2012-11-28 10:13:54 +01:00
|
|
|
end
|