summaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorGo MAEDA <maeda@farend.jp>2018-04-07 06:39:57 +0000
committerGo MAEDA <maeda@farend.jp>2018-04-07 06:39:57 +0000
commitb4a703ce4ea166d855d8e467e0b95a962d762a01 (patch)
tree3f85e2979971a7884bd9b54b07f2fee9bc6da88b /app
parente5a8bcfd72de2b4ac5d90a534f3bd927619904a7 (diff)
downloadredmine-b4a703ce4ea166d855d8e467e0b95a962d762a01.tar.gz
redmine-b4a703ce4ea166d855d8e467e0b95a962d762a01.zip
Support for lastnames with spaces in user autocompleters (#28154).
Patch by Gregor Schmidt. git-svn-id: http://svn.redmine.org/redmine/trunk@17259 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app')
-rw-r--r--app/models/principal.rb17
1 files changed, 11 insertions, 6 deletions
diff --git a/app/models/principal.rb b/app/models/principal.rb
index e4410dde2..b2476c154 100644
--- a/app/models/principal.rb
+++ b/app/models/principal.rb
@@ -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
}