From ef329b564732056726d61feddb019a15762f5255 Mon Sep 17 00:00:00 2001 From: Robert Waltemath Date: Sat, 22 Jun 2013 17:35:45 +0200 Subject: [PATCH] Added up and down migration for weekly task data. --- app/models/task.rb | 2 +- ...0615073715_create_periodic_task_groups.rb} | 0 .../20130622095040_move_weekly_tasks.rb | 60 +++++++++++++++++++ db/schema.rb | 2 +- 4 files changed, 62 insertions(+), 2 deletions(-) rename db/migrate/{20121115073715_create_periodic_task_groups.rb => 20130615073715_create_periodic_task_groups.rb} (100%) create mode 100644 db/migrate/20130622095040_move_weekly_tasks.rb diff --git a/app/models/task.rb b/app/models/task.rb index 8d17652c..453af12b 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -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 diff --git a/db/migrate/20121115073715_create_periodic_task_groups.rb b/db/migrate/20130615073715_create_periodic_task_groups.rb similarity index 100% rename from db/migrate/20121115073715_create_periodic_task_groups.rb rename to db/migrate/20130615073715_create_periodic_task_groups.rb diff --git a/db/migrate/20130622095040_move_weekly_tasks.rb b/db/migrate/20130622095040_move_weekly_tasks.rb new file mode 100644 index 00000000..7c9cde8f --- /dev/null +++ b/db/migrate/20130622095040_move_weekly_tasks.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 59fa21c4..73cb2ef1 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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