From 8509212991bbc155e12c04cf1ebbc9cf2fe35ef8 Mon Sep 17 00:00:00 2001 From: Gregor Schmidt Date: Wed, 4 May 2011 19:01:15 +0200 Subject: [PATCH 1/2] [#373] Adding test and fix for duplicates in issue autocompleter --- app/controllers/auto_completes_controller.rb | 1 + test/functional/auto_completes_controller_test.rb | 14 +++++++------- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/app/controllers/auto_completes_controller.rb b/app/controllers/auto_completes_controller.rb index 021d5367b8..365da47791 100644 --- a/app/controllers/auto_completes_controller.rb +++ b/app/controllers/auto_completes_controller.rb @@ -15,6 +15,7 @@ class AutoCompletesController < ApplicationController :conditions => ["LOWER(#{Issue.table_name}.subject) LIKE :q OR CAST(#{Issue.table_name}.id AS CHAR(13)) LIKE :q", {:q => "%#{q.downcase}%" }]) end @issues.compact! + @issues.uniq! render :layout => false end diff --git a/test/functional/auto_completes_controller_test.rb b/test/functional/auto_completes_controller_test.rb index db246c30fa..0cea1112d1 100644 --- a/test/functional/auto_completes_controller_test.rb +++ b/test/functional/auto_completes_controller_test.rb @@ -20,19 +20,19 @@ class AutoCompletesControllerTest < ActionController::TestCase test 'should return issues matching a given id' do @project = Project.find('subproject1') @issue_21 = Issue.generate_for_project!(@project, :id => 21) - @issue_101 = Issue.generate_for_project!(@project, :id => 101) - @issue_102 = Issue.generate_for_project!(@project, :id => 102) - @issue_with_subject = Issue.generate_for_project!(@project, :subject => 'This has 1 in the subject') + @issue_2101 = Issue.generate_for_project!(@project, :id => 2101) + @issue_2102 = Issue.generate_for_project!(@project, :id => 2102) + @issue_with_subject = Issue.generate_for_project!(@project, :subject => 'This has 21 in the subject') - get :issues, :project_id => @project.id, :q => '1' + get :issues, :project_id => @project.id, :q => '21' assert_response :success assert_not_nil assigns(:issues) - assert assigns(:issues).include?(Issue.find(13)) assert assigns(:issues).include?(@issue_21) - assert assigns(:issues).include?(@issue_101) - assert assigns(:issues).include?(@issue_102) + assert assigns(:issues).include?(@issue_2101) + assert assigns(:issues).include?(@issue_2102) assert assigns(:issues).include?(@issue_with_subject) + assert_equal assigns(:issues).size, assigns(:issues).uniq.size, "Issues list includes duplicates" end def test_auto_complete_with_scope_all_and_cross_project_relations From dbd6dfc69b95e3989eaaeec3f8bd1061493b1b77 Mon Sep 17 00:00:00 2001 From: Gregor Schmidt Date: Wed, 4 May 2011 19:12:56 +0200 Subject: [PATCH 2/2] [#373] Refactoring auto_completes#issues action hoping to make it more intention revealing --- app/controllers/auto_completes_controller.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/app/controllers/auto_completes_controller.rb b/app/controllers/auto_completes_controller.rb index 365da47791..17d86f3469 100644 --- a/app/controllers/auto_completes_controller.rb +++ b/app/controllers/auto_completes_controller.rb @@ -4,18 +4,18 @@ class AutoCompletesController < ApplicationController def issues @issues = [] q = params[:q].to_s - query = (params[:scope] == "all" && Setting.cross_project_issue_relations?) ? Issue : @project.issues - if q.match(/^\d+$/) - @issues << query.visible.find_by_id(q.to_i) - end - unless q.blank? - @issues += query.visible.find(:all, + + if q.present? + query = (params[:scope] == "all" && Setting.cross_project_issue_relations?) ? Issue : @project.issues + + @issues |= query.visible.find_all_by_id(q.to_i) if q =~ /^\d+$/ + + @issues |= query.visible.find(:all, :limit => 10, :order => "#{Issue.table_name}.id ASC", :conditions => ["LOWER(#{Issue.table_name}.subject) LIKE :q OR CAST(#{Issue.table_name}.id AS CHAR(13)) LIKE :q", {:q => "%#{q.downcase}%" }]) end - @issues.compact! - @issues.uniq! + render :layout => false end