Commit 87af985a authored by maeda's avatar maeda

Support for lastnames with spaces in user autocompleters (#28154).

Patch by Gregor Schmidt.


git-svn-id: https://svn.redmine.org/redmine/trunk@17259 e93f8b46-1217-0410-a6f0-8f06a7374b81
parent e3654a12
......@@ -69,15 +69,20 @@ class Principal < ActiveRecord::Base
where({})
else
pattern = "%#{q}%"
sql = %w(login firstname lastname).map {|column| "LOWER(#{table_name}.#{column}) LIKE LOWER(:p)"}.join(" OR ")
sql = "LOWER(#{table_name}.login) LIKE LOWER(:p)"
sql << " OR #{table_name}.id IN (SELECT user_id FROM #{EmailAddress.table_name} WHERE LOWER(address) LIKE LOWER(:p))"
params = {:p => pattern}
if q =~ /^(.+)\s+(.+)$/
a, b = "#{$1}%", "#{$2}%"
sql << " OR (LOWER(#{table_name}.firstname) LIKE LOWER(:a) AND LOWER(#{table_name}.lastname) LIKE LOWER(:b))"
sql << " OR (LOWER(#{table_name}.firstname) LIKE LOWER(:b) AND LOWER(#{table_name}.lastname) LIKE LOWER(:a))"
params.merge!(:a => a, :b => b)
tokens = q.split(/\s+/).reject(&:blank?).map { |token| "%#{token}%" }
if tokens.present?
sql << ' OR ('
sql << tokens.map.with_index do |token, index|
params.merge!(:"token_#{index}" => token)
"(LOWER(#{table_name}.firstname) LIKE LOWER(:token_#{index}) OR LOWER(#{table_name}.lastname) LIKE LOWER(:token_#{index}))"
end.join(' AND ')
sql << ')'
end
where(sql, params)
end
}
......
......@@ -127,6 +127,16 @@ class PrincipalTest < ActiveSupport::TestCase
assert_equal User.find(2), results.first
end
test "like scope should find lastname with spaces" do
user = User.find(1)
user.update_columns(:firstname => 'Leonardo', :lastname => 'da Vinci')
results = Principal.like('Leonardo da Vinci')
assert_equal 1, results.count
assert_equal user, results.first
end
def test_like_scope_with_cyrillic_name
user = User.generate!(:firstname => 'Соболев', :lastname => 'Денис')
results = Principal.like('Собо')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment