Merge pull request #46 from schmidt/b/373-duplicates-in-issue-autocomplete

Avoid duplicates in issue autocomplete. #373
This commit is contained in:
Felix Schäfer 2011-05-05 11:55:38 -07:00
commit 800cee548b
2 changed files with 15 additions and 14 deletions

View File

@ -4,17 +4,18 @@ class AutoCompletesController < ApplicationController
def issues def issues
@issues = [] @issues = []
q = params[:q].to_s q = params[:q].to_s
if q.present?
query = (params[:scope] == "all" && Setting.cross_project_issue_relations?) ? Issue : @project.issues 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) @issues |= query.visible.find_all_by_id(q.to_i) if q =~ /^\d+$/
end
unless q.blank? @issues |= query.visible.find(:all,
@issues += query.visible.find(:all,
:limit => 10, :limit => 10,
:order => "#{Issue.table_name}.id ASC", :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}%" }]) :conditions => ["LOWER(#{Issue.table_name}.subject) LIKE :q OR CAST(#{Issue.table_name}.id AS CHAR(13)) LIKE :q", {:q => "%#{q.downcase}%" }])
end end
@issues.compact!
render :layout => false render :layout => false
end end

View File

@ -20,19 +20,19 @@ class AutoCompletesControllerTest < ActionController::TestCase
test 'should return issues matching a given id' do test 'should return issues matching a given id' do
@project = Project.find('subproject1') @project = Project.find('subproject1')
@issue_21 = Issue.generate_for_project!(@project, :id => 21) @issue_21 = Issue.generate_for_project!(@project, :id => 21)
@issue_101 = Issue.generate_for_project!(@project, :id => 101) @issue_2101 = Issue.generate_for_project!(@project, :id => 2101)
@issue_102 = Issue.generate_for_project!(@project, :id => 102) @issue_2102 = Issue.generate_for_project!(@project, :id => 2102)
@issue_with_subject = Issue.generate_for_project!(@project, :subject => 'This has 1 in the subject') @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_response :success
assert_not_nil assigns(:issues) assert_not_nil assigns(:issues)
assert assigns(:issues).include?(Issue.find(13))
assert assigns(:issues).include?(@issue_21) assert assigns(:issues).include?(@issue_21)
assert assigns(:issues).include?(@issue_101) assert assigns(:issues).include?(@issue_2101)
assert assigns(:issues).include?(@issue_102) assert assigns(:issues).include?(@issue_2102)
assert assigns(:issues).include?(@issue_with_subject) assert assigns(:issues).include?(@issue_with_subject)
assert_equal assigns(:issues).size, assigns(:issues).uniq.size, "Issues list includes duplicates"
end end
def test_auto_complete_with_scope_all_and_cross_project_relations def test_auto_complete_with_scope_all_and_cross_project_relations