mirror of
https://github.com/TracksApp/tracks.git
synced 2026-01-03 07:48:50 +01:00
Add migration script to remove 'project_hidden'
This commit is contained in:
parent
ed3bca22a4
commit
6805cb801b
1 changed files with 42 additions and 0 deletions
|
|
@ -0,0 +1,42 @@
|
|||
class RemoveProjectHiddenStateFromTodos < ActiveRecord::Migration
|
||||
class Todo < ActiveRecord::Base
|
||||
has_many :successor_dependencies, :foreign_key => 'successor_id', :class_name => 'Dependency'
|
||||
belongs_to :project
|
||||
end
|
||||
|
||||
class Project < ActiveRecord::Base
|
||||
end
|
||||
|
||||
class Dependency < ActiveRecord::Base
|
||||
belongs_to :predecessor, :foreign_key => 'predecessor_id', :class_name => 'Todo'
|
||||
end
|
||||
|
||||
def self.up
|
||||
Todo.where(state: 'project_hidden').find_each do |todo|
|
||||
if todo.show_from.present?
|
||||
todo.state = 'deferred'
|
||||
elsif has_uncompleted_predecessor(todo)
|
||||
todo.state = 'pending'
|
||||
else
|
||||
todo.state = 'active'
|
||||
end
|
||||
todo.save
|
||||
end
|
||||
end
|
||||
|
||||
def self.down
|
||||
Project.where(state: 'hidden').find_each do |project|
|
||||
Todo.where(project_id: project.id).where.not(state: 'completed').find_each do |todo|
|
||||
todo.state = 'project_hidden'
|
||||
todo.save
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def has_uncompleted_predecessor(todo)
|
||||
todo.successor_dependencies.each do |dependency|
|
||||
return true unless dependency.predecessor.state == 'completed'
|
||||
end
|
||||
false
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue