diff --git a/app/controllers/todos_controller.rb b/app/controllers/todos_controller.rb index 69c6c621..7c6d1ee2 100644 --- a/app/controllers/todos_controller.rb +++ b/app/controllers/todos_controller.rb @@ -54,6 +54,7 @@ class TodosController < ApplicationController p = TodoCreateParamsHelper.new(params, prefs) p.parse_dates() unless mobile? tag_list = p.tag_list + predecessor_list = p.predecessor_list @todo = current_user.todos.build(p.attributes) @@ -77,6 +78,14 @@ class TodosController < ApplicationController @todo.tags.reload end + unless predecessor_list.blank? + @todo.add_predecessor_list(predecessor_list) + unless @todo.uncompleted_predecessors.empty? || @todo.state == 'project_hidden' + @todo.state = 'pending' + end + @saved = @todo.save + end + respond_to do |format| format.html { redirect_to :action => "index" } format.m do @@ -559,20 +568,29 @@ class TodosController < ApplicationController end def auto_complete_for_predecessor - get_todo_from_params - # Begin matching todos in current project - @items = current_user.todos.find(:all, - :conditions => [ 'NOT (id = ?) AND description LIKE ? AND project_id = ?', - @todo.id, - '%' + params[:predecessor_list] + '%', - @todo.project_id ], - :order => 'description ASC', - :limit => 10 - ) - if @items.empty? # Match todos in other projects + unless params['id'].nil? + get_todo_from_params + # Begin matching todos in current project @items = current_user.todos.find(:all, - :conditions => [ 'NOT (id = ?) AND description LIKE ?', - params[:id], '%' + params[:predecessor_list] + '%' ], + :conditions => [ 'NOT (id = ?) AND description LIKE ? AND project_id = ?', + @todo.id, + '%' + params[:predecessor_list] + '%', + @todo.project_id ], + :order => 'description ASC', + :limit => 10 + ) + if @items.empty? # Match todos in other projects + @items = current_user.todos.find(:all, + :conditions => [ 'NOT (id = ?) AND description LIKE ?', + params[:id], '%' + params[:predecessor_list] + '%' ], + :order => 'description ASC', + :limit => 10 + ) + end + else + # New todo - TODO: Filter on project + @items = current_user.todos.find(:all, + :conditions => [ 'description LIKE ?', '%' + params[:predecessor_list] + '%' ], :order => 'description ASC', :limit => 10 ) @@ -1023,6 +1041,10 @@ class TodosController < ApplicationController def tag_list @params['tag_list'] end + + def predecessor_list + @params['predecessor_list'] + end def parse_dates() @attributes['show_from'] = @prefs.parse_date(show_from) diff --git a/app/models/todo.rb b/app/models/todo.rb index 06ab02d5..61558d67 100644 --- a/app/models/todo.rb +++ b/app/models/todo.rb @@ -74,7 +74,11 @@ class Todo < ActiveRecord::Base def update_state_from_project if state == 'project_hidden' and !project.hidden? - self.state = 'active' + if self.uncompleted_predecessors.empty? + self.state = 'pending' + else + self.state = 'active' + end elsif state == 'active' and project.hidden? self.state = 'project_hidden' end @@ -154,10 +158,6 @@ class Todo < ActiveRecord::Base return self.recurring_todo_id != nil end - def add_predecessor(predecessor) - logger.debug "add_predecessor #{predecessor.description}" - end - # TODO: DELIMITER # TODO: Todo::Error # TODO: Handle todos with the same description @@ -181,7 +181,11 @@ class Todo < ActiveRecord::Base t = Todo.find_by_description(description) #raise Todo::Error, "predecessor could not be found: #{description}" if t.nil? # Create dependency record - self.predecessors << t unless self.predecessors.include?(t) + unless t.nil? + self.predecessors << t unless self.predecessors.include?(t) + else + logger.error "Could not find #{description}" + end end # debugger diff --git a/app/views/shared/_add_new_item_form.rhtml b/app/views/shared/_add_new_item_form.rhtml index ca2c2032..7b2f313f 100644 --- a/app/views/shared/_add_new_item_form.rhtml +++ b/app/views/shared/_add_new_item_form.rhtml @@ -51,6 +51,14 @@ <%= text_field("todo", "show_from", "size" => 12, "class" => "Date", "tabindex" => 7, "autocomplete" => "off") %> + + <%= text_field_tag "predecessor_list", nil, :size => 30, :tabindex => 8 %> + <%= content_tag("div", "", :id => "predecessor_list_auto_complete", :class => "auto_complete") %> + <%= auto_complete_field 'predecessor_list', { + :url => {:controller => 'todos', :action => 'auto_complete_for_predecessor', :id => nil}, + :tokens => [','] + } %> + <%= source_view_tag( @source_view ) %> <%= hidden_field_tag :_tag_name, @tag_name.underscore.gsub(/\s+/,'_') if source_view_is :tag %>