Sha256: 79b3eff8018259ecce7da91e2cd8bf4c1dca4a3ec96e2a33ec868f62f9605400

Contents?: true

Size: 1.68 KB

Versions: 4

Compression:

Stored size: 1.68 KB

Contents

class AddPeriod < ActiveRecord::Migration
  def self.up
    create_table :periods do |table|
      table.column :backlog_id, :integer, :null => false
      table.column :position, :integer, :null => false
      table.column :start_on, :date, :null => false
      table.column :end_on, :date, :null => false
    end
    add_column :tasks, :period_id, :integer
    Backlog.find(:all).each do |backlog|
      default_period = Period.new
      default_period.backlog_id = backlog.id
      default_period.position = 1
      default_period.start_on = Date.parse('2006-01-01')
      default_period.end_on = Date.parse('2006-12-31')
      default_period.save
      start_date = Date.today
      Task.find(:all, :conditions => "backlog_id = #{backlog.id}").each do |task|
        if task.works.first
          task_completed_on = task.works.first.completed_at.to_date
          start_date = task_completed_on if task_completed_on < start_date
        end
        task.period_id = default_period.id
        task.save
      end
      default_period.start_on = start_date
      default_period.end_on = start_date + 365
      default_period.save
    end
    remove_column :tasks, :backlog_id
  end

  def self.down
    add_column :tasks, :backlog_id, :integer
    Task.find(:all).each do |task|
      task.backlog_id = task.root_task.period.backlog.id
      task.save
    end
    remove_column :tasks, :period_id
    drop_table :periods
  end
end

class Backlog < ActiveRecord::Base
end

class Period < ActiveRecord::Base
  belongs_to :backlog
end

class Task < ActiveRecord::Base
  belongs_to :period
  acts_as_tree :order => 'position'
  
  def root_task
    if parent
      parent.root_task
    else
      self
    end
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
backlog-0.0.0 db/migrate/004_add_period.rb
backlog-0.0.1 db/migrate/004_add_period.rb
backlog-0.0.2 db/migrate/004_add_period.rb
backlog-0.0.4 db/migrate/004_add_period.rb