From ce04c41830d7cc301c7827bfe0c7108815a2bbe6 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Tue, 13 Nov 2012 22:01:31 +0000 Subject: [PATCH] Fixed that Principal#like scope does not work with cyrillic characters and Postgresql (#12349). git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@10801 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/models/principal.rb | 6 +++--- test/unit/principal_test.rb | 9 +++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/app/models/principal.rb b/app/models/principal.rb index 2b89e16a9..90b56b6a1 100644 --- a/app/models/principal.rb +++ b/app/models/principal.rb @@ -30,13 +30,13 @@ class Principal < ActiveRecord::Base if q.blank? {} else - q = q.to_s.downcase + q = q.to_s pattern = "%#{q}%" - sql = "LOWER(login) LIKE :p OR LOWER(firstname) LIKE :p OR LOWER(lastname) LIKE :p OR LOWER(mail) LIKE :p" + sql = "LOWER(login) LIKE LOWER(:p) OR LOWER(firstname) LIKE LOWER(:p) OR LOWER(lastname) LIKE LOWER(:p) OR LOWER(mail) LIKE LOWER(:p)" params = {:p => pattern} if q =~ /^(.+)\s+(.+)$/ a, b = "#{$1}%", "#{$2}%" - sql << " OR (LOWER(firstname) LIKE :a AND LOWER(lastname) LIKE :b) OR (LOWER(firstname) LIKE :b AND LOWER(lastname) LIKE :a)" + sql << " OR (LOWER(firstname) LIKE LOWER(:a) AND LOWER(lastname) LIKE LOWER(:b)) OR (LOWER(firstname) LIKE LOWER(:b) AND LOWER(lastname) LIKE LOWER(:a))" params.merge!(:a => a, :b => b) end {:conditions => [sql, params]} diff --git a/test/unit/principal_test.rb b/test/unit/principal_test.rb index 6a4aed700..6d06a3105 100644 --- a/test/unit/principal_test.rb +++ b/test/unit/principal_test.rb @@ -1,3 +1,5 @@ +# encoding: utf-8 +# # Redmine - project management software # Copyright (C) 2006-2012 Jean-Philippe Lang # @@ -106,4 +108,11 @@ class PrincipalTest < ActiveSupport::TestCase assert_equal @palmer, results.first end end + + def test_like_scope_with_cyrillic_name + user = User.generate!(:firstname => 'Соболев', :lastname => 'Денис') + results = Principal.like('Собо') + assert_equal 1, results.count + assert_equal user, results.first + end end