diff --git a/app/controllers/todos_controller.rb b/app/controllers/todos_controller.rb index fc0ff4f0..b668a308 100644 --- a/app/controllers/todos_controller.rb +++ b/app/controllers/todos_controller.rb @@ -166,26 +166,14 @@ class TodosController < ApplicationController @new_recurring_todo = check_for_next_todo(@todo) if @saved if @todo.completed? - logger.debug "completed #{@todo.description}" - # A todo was completed - check for pending todos to activate - @pending_to_activate = [] - @todo.successors.each do |t| - if t.uncompleted_predecessors.empty? # Activate pending todos - logger.debug "activated #{t.description}" + @pending_to_activate = @todo.pending_to_activate + @pending_to_activate.each do |t| t.activate! - @pending_to_activate << t - end end else - # Block active successors for undone action - @active_to_block = [] - logger.debug "undid #{@todo.description}" - @todo.successors.each do |t| - if t.state == 'active' - logger.debug "blocked #{t.description}" + @active_to_block = @todo.active_to_block + @active_to_block.each do |t| t.block! - @active_to_block << t - end end end @@ -279,11 +267,17 @@ class TodosController < ApplicationController if params['done'] == '1' && !@todo.completed? @todo.complete! + @todo.pending_to_activate.each do |t| + t.activate! + end end # strange. if checkbox is not checked, there is no 'done' in params. # Therefore I've used the negation if !(params['done'] == '1') && @todo.completed? @todo.activate! + @todo.active_to_block.each do |t| + t.block! + end end @todo.attributes = params["todo"] diff --git a/app/models/todo.rb b/app/models/todo.rb index a54d5451..20df7a06 100644 --- a/app/models/todo.rb +++ b/app/models/todo.rb @@ -233,6 +233,16 @@ class Todo < ActiveRecord::Base @predecessor_array << t.description end + # Return todos that should be activated if the current todo is completed + def pending_to_activate + return successors.find_all {|t| t.uncompleted_predecessors.empty?} + end + + # Return todos that should be blocked if the current todo is undone + def active_to_block + return successors.find_all {|t| t.active?} + end + # Rich Todo API def self.from_rich_message(user, default_context_id, description, notes)