From 0e22d91804f70ed0afe85952a894754a8c45700a Mon Sep 17 00:00:00 2001 From: Reinier Balt Date: Mon, 9 Apr 2012 12:48:40 +0200 Subject: [PATCH] fix #1195. Update all autocompleters for more sane order of returned items --- app/controllers/contexts_controller.rb | 11 ++++- app/controllers/projects_controller.rb | 9 +++- app/controllers/todos_controller.rb | 57 +++++++++++++------------- 3 files changed, 46 insertions(+), 31 deletions(-) diff --git a/app/controllers/contexts_controller.rb b/app/controllers/contexts_controller.rb index b738fdcf..aff19c89 100644 --- a/app/controllers/contexts_controller.rb +++ b/app/controllers/contexts_controller.rb @@ -31,7 +31,7 @@ class ContextsController < ApplicationController @all_contexts = current_user.contexts.all render :action => 'index', :layout => false, :content_type => Mime::TEXT end - format.autocomplete { render :text => for_autocomplete(@active_contexts + @hidden_contexts, params[:term])} + format.autocomplete &render_autocomplete end end @@ -248,6 +248,15 @@ class ContextsController < ApplicationController :author => lambda { |c| nil } } end end + + def render_autocomplete + lambda do + # first get active contexts with todos then those without + filled_contexts = @active_contexts.reject { |ctx| ctx.todos.count == 0 } + @hidden_contexts.reject { |ctx| ctx.todos.count == 0 } + empty_contexts = @active_contexts.find_all { |ctx| ctx.todos.count == 0 } + @hidden_contexts.find_all { |ctx| ctx.todos.count == 0 } + render :text => for_autocomplete(filled_contexts + empty_contexts, params[:term]) + end + end def feed_options Context.feed_options(current_user) diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 381ba8bf..b9766f89 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -28,7 +28,7 @@ class ProjectsController < ApplicationController format.rss &render_rss_feed format.atom &render_atom_feed format.text &render_text_feed - format.autocomplete { render :text => for_autocomplete(current_user.projects.uncompleted, params[:term]) } + format.autocomplete &render_autocomplete end end end @@ -385,6 +385,13 @@ class ProjectsController < ApplicationController render :action => 'index', :layout => false, :content_type => Mime::TEXT end end + + def render_autocomplete + lambda do + projects = current_user.projects.active + current_user.projects.hidden + render :text => for_autocomplete(projects, params[:term]) + end + end def set_project_from_params @project = current_user.projects.find_by_params(params) diff --git a/app/controllers/todos_controller.rb b/app/controllers/todos_controller.rb index c6361b22..fc6491a2 100644 --- a/app/controllers/todos_controller.rb +++ b/app/controllers/todos_controller.rb @@ -694,9 +694,13 @@ class TodosController < ApplicationController end def tags - @tags = Tag.find(:all, :conditions =>['name like ?', '%'+params[:term]+'%']) + # TODO: limit to current_user + tags_beginning = Tag.find(:all, :conditions => ['name like ?', params[:term]+'%']) + tags_all = Tag.find(:all, :conditions =>['name like ?', '%'+params[:term]+'%']) + tags_all= tags_all - tags_beginning + respond_to do |format| - format.autocomplete { render :text => for_autocomplete(@tags, params[:term]) } + format.autocomplete { render :text => for_autocomplete(tags_beginning+tags_all, params[:term]) } end end @@ -805,36 +809,32 @@ class TodosController < ApplicationController def auto_complete_for_predecessor unless params['id'].nil? get_todo_from_params - # Begin matching todos in current project - @items = current_user.todos.find(:all, + # Begin matching todos in current project, excluding @todo itself + @items = @todo.project.todos.not_completed.find(:all, :include => [:context, :project], - :conditions => [ '(todos.state = ? OR todos.state = ? OR todos.state = ?) AND ' + - 'NOT (todos.id = ?) AND lower(todos.description) LIKE ? AND todos.project_id = ?', - 'active', 'pending', 'deferred', - @todo.id, - '%' + params[:predecessor_list].downcase + '%', - @todo.project_id ], + :conditions => ['(LOWER(todos.description) LIKE ?) AND NOT(todos.id=?)', "%#{params[:term].downcase}%", @todo.id], :order => 'description ASC', :limit => 10 - ) - if @items.empty? # Match todos in other projects - @items = current_user.todos.find(:all, - :include => [:context, :project], - :conditions => [ '(todos.state = ? OR todos.state = ? OR todos.state = ?) AND ' + - 'NOT (todos.id = ?) AND lower(todos.description) LIKE ?', - 'active', 'pending', 'deferred', - params[:id], '%' + params[:term].downcase + '%' ], - :order => 'description ASC', - :limit => 10 - ) - end - else - # New todo - TODO: Filter on project - @items = current_user.todos.find(:all, + ) unless @todo.project.nil? + # Then look in the current context, excluding @todo itself + @items = @todo.context.todos.not_completed.find(:all, :include => [:context, :project], - :conditions => [ '(todos.state = ? OR todos.state = ? OR todos.state = ?) AND lower(todos.description) LIKE ?', - 'active', 'pending', 'deferred', - '%' + params[:term].downcase + '%' ], + :conditions => ['(LOWER(todos.description) LIKE ?) AND NOT(todos.id=?)', "%#{params[:term].downcase}%", @todo.id], + :order => 'description ASC', + :limit => 10 + ) unless !@items.empty? || @todo.context.nil? + # Match todos in other projects, excluding @todo itself + @items = current_user.todos.not_completed.find(:all, + :include => [:context, :project], + :conditions => ['(LOWER(todos.description) LIKE ?) AND NOT(todos.id=?)', "%#{params[:term].downcase}%", @todo.id], + :order => 'description ASC', + :limit => 10 + ) unless !@items.empty? + else + # New todo - TODO: Filter on current project in project view + @items = current_user.todos.not_completed.find(:all, + :include => [:context, :project], + :conditions => ['(LOWER(todos.description) LIKE ?)', "%#{params[:term].downcase}%"], :order => 'description ASC', :limit => 10 ) @@ -847,7 +847,6 @@ class TodosController < ApplicationController @project = current_user.projects.new(:name => @todo.description, :description => @todo.notes, :default_context => @todo.context) - unless @project.invalid? @todo.destroy @project.save!