2019-05-13 18:42:57 +02:00
|
|
|
class ConvertTodoToStateMachine < ActiveRecord::Migration[5.2]
|
2007-03-30 04:36:52 +00:00
|
|
|
|
|
|
|
|
class Todo < ActiveRecord::Base; belongs_to :project; end
|
|
|
|
|
class Immediate < Todo; end
|
|
|
|
|
class Deferred < Todo; end
|
|
|
|
|
class Project < ActiveRecord::Base; end
|
|
|
|
|
|
|
|
|
|
def self.up
|
|
|
|
|
add_column :todos, :state, :string, :limit => 20, :default => "immediate", :null => false
|
2013-09-13 16:44:59 +02:00
|
|
|
@todos = Todo.all
|
2007-03-30 04:36:52 +00:00
|
|
|
@todos.each do |todo|
|
|
|
|
|
if todo.done?
|
|
|
|
|
todo.state = 'completed'
|
|
|
|
|
elsif todo.type == 'Deferred'
|
|
|
|
|
todo.state = 'deferred'
|
|
|
|
|
elsif !todo.project.nil? && todo.project.state == 'hidden'
|
|
|
|
|
todo.state = 'project_hidden'
|
|
|
|
|
else
|
|
|
|
|
todo.state = 'active'
|
|
|
|
|
end
|
|
|
|
|
todo.save
|
|
|
|
|
end
|
2008-04-07 07:28:25 +00:00
|
|
|
rename_column :todos, 'completed', 'completed_at' #bug in sqlite requires column names as strings
|
2007-03-30 04:36:52 +00:00
|
|
|
remove_column :todos, :done
|
|
|
|
|
remove_column :todos, :type
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.down
|
|
|
|
|
add_column :todos, :done, :integer, :limit => 4, :default => 0, :null => false
|
|
|
|
|
add_column :todos, :type, :string, :default => "Immediate", :null => false
|
2008-04-07 07:28:25 +00:00
|
|
|
rename_column :todos, 'completed_at', 'completed' #bug in sqlite requires column names as strings
|
2013-09-13 16:44:59 +02:00
|
|
|
@todos = Todo.all
|
2007-03-30 04:36:52 +00:00
|
|
|
@todos.each do |todo|
|
|
|
|
|
todo.done = todo.state == 'completed'
|
|
|
|
|
todo.type = todo.type == 'deferred' ? 'Deferred' : 'Immediate'
|
|
|
|
|
todo.save
|
|
|
|
|
end
|
|
|
|
|
remove_column :todos, :state
|
|
|
|
|
end
|
|
|
|
|
end
|