2020-08-07 01:14:14 +02:00
|
|
|
class MoveWeeklyTasks < ActiveRecord::Migration[4.2]
|
2013-06-22 17:35:45 +02:00
|
|
|
def up
|
|
|
|
Workgroup.where(weekly_task: true).each do |workgroup|
|
|
|
|
task_group = PeriodicTaskGroup.create
|
|
|
|
puts "Moving weekly task for workgroup #{workgroup.name} to group #{task_group.id}"
|
|
|
|
workgroup.tasks.undone.each do |task|
|
2013-06-24 10:36:14 +02:00
|
|
|
task.update_column(:periodic_task_group_id, task_group.id) if weekly_task?(workgroup, task)
|
2013-06-22 17:35:45 +02:00
|
|
|
end
|
|
|
|
tasks = task_group.tasks.order(:due_date)
|
|
|
|
task_group.next_task_date = tasks.last.due_date + PeriodicTaskGroup::PeriodDays unless tasks.empty?
|
|
|
|
task_group.save!
|
|
|
|
puts "Associated #{tasks.count} tasks with group and set next_task_date to #{task_group.next_task_date}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
|
|
|
PeriodicTaskGroup.all.each do |task_group|
|
|
|
|
unless task_group.tasks.empty?
|
|
|
|
task = task_group.tasks.first
|
|
|
|
workgroup = task.workgroup
|
|
|
|
puts "Writing task data of group #{task_group.id} to workgroup #{workgroup.name}"
|
|
|
|
workgroup_attributes = {
|
|
|
|
weekly_task: true,
|
|
|
|
weekday: task.due_date.days_to_week_start(:sunday),
|
|
|
|
task_name: task.name,
|
|
|
|
task_description: task.description,
|
|
|
|
task_required_users: task.required_users,
|
|
|
|
task_duration: task.duration
|
|
|
|
}
|
2022-10-18 17:23:18 +02:00
|
|
|
workgroup.update workgroup_attributes
|
2013-06-22 17:35:45 +02:00
|
|
|
task_group.tasks.update_all weekly: true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-01 15:27:26 +01:00
|
|
|
private
|
|
|
|
|
2013-06-22 17:35:45 +02:00
|
|
|
def weekly_task?(workgroup, task)
|
2013-09-02 15:42:03 +02:00
|
|
|
return false if task.due_date.nil?
|
2013-09-02 16:46:44 +02:00
|
|
|
|
2013-06-22 17:35:45 +02:00
|
|
|
group_task = {
|
|
|
|
weekday: workgroup.weekday,
|
|
|
|
name: workgroup.task_name,
|
|
|
|
description: workgroup.task_description,
|
|
|
|
required_users: workgroup.task_required_users,
|
|
|
|
duration: workgroup.task_duration,
|
|
|
|
weekly: true,
|
|
|
|
done: false,
|
|
|
|
workgroup_id: workgroup.id
|
|
|
|
}
|
|
|
|
task_task = {
|
|
|
|
weekday: task.due_date.days_to_week_start(:sunday),
|
|
|
|
name: task.name,
|
|
|
|
description: task.description,
|
|
|
|
required_users: task.required_users,
|
|
|
|
duration: task.duration,
|
|
|
|
weekly: task.weekly,
|
|
|
|
done: task.done,
|
|
|
|
workgroup_id: task.workgroup_id
|
|
|
|
}
|
|
|
|
group_task == task_task
|
|
|
|
end
|
|
|
|
end
|