Added up and down migration for weekly task data.

This commit is contained in:
Robert Waltemath 2013-06-22 17:35:45 +02:00
parent 2e1a3eb47f
commit ef329b5647
4 changed files with 62 additions and 2 deletions

View file

@ -108,7 +108,7 @@ class Task < ActiveRecord::Base
end
def exclude_from_periodic_task_group
if changed? and not new_record?
if changed? and not new_record? and not changed.include? 'periodic_task_group_id'
self.periodic_task_group = nil
end
end

View file

@ -0,0 +1,60 @@
class MoveWeeklyTasks < ActiveRecord::Migration
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|
task_group.tasks << task if weekly_task?(workgroup, task)
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
}
workgroup.update_attributes workgroup_attributes
task_group.tasks.update_all weekly: true
end
end
end
private
def weekly_task?(workgroup, task)
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

View file

@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20121230142516) do
ActiveRecord::Schema.define(:version => 20130622095040) do
create_table "article_categories", :force => true do |t|
t.string "name", :default => "", :null => false