Sha256: f568ddb9a1b198294b8fec82fecd99ba5c84ae46dab9a8084b212bc708b94ed9

Contents?: true

Size: 1.68 KB

Versions: 117

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

117 entries across 117 versions & 1 rubygems

Version Path
backlog-0.36.2 db/migrate/004_add_period.rb
backlog-0.0.5 db/migrate/004_add_period.rb
backlog-0.1.1 db/migrate/004_add_period.rb
backlog-0.1.0 db/migrate/004_add_period.rb
backlog-0.1.2 db/migrate/004_add_period.rb
backlog-0.10.1 db/migrate/004_add_period.rb
backlog-0.10.0 db/migrate/004_add_period.rb
backlog-0.10.5 db/migrate/004_add_period.rb
backlog-0.10.2 db/migrate/004_add_period.rb
backlog-0.10.4 db/migrate/004_add_period.rb
backlog-0.10.3 db/migrate/004_add_period.rb
backlog-0.10.6 db/migrate/004_add_period.rb
backlog-0.10.7 db/migrate/004_add_period.rb
backlog-0.10.8 db/migrate/004_add_period.rb
backlog-0.12.0 db/migrate/004_add_period.rb
backlog-0.11.0 db/migrate/004_add_period.rb
backlog-0.12.2 db/migrate/004_add_period.rb
backlog-0.12.1 db/migrate/004_add_period.rb
backlog-0.12.3 db/migrate/004_add_period.rb
backlog-0.12.4 db/migrate/004_add_period.rb