summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2012-02-14 16:29:48 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2012-02-14 16:29:48 +0000
commit008557581d8a9a428c262e09fe8f36060fe945b9 (patch)
treeb80c5e2c15036634391aeebb4d62a114f21eec3e
parentbd2581e7c9584d91111ca26b55327972c2cb6727 (diff)
downloadredmine-008557581d8a9a428c262e09fe8f36060fe945b9.tar.gz
redmine-008557581d8a9a428c262e09fe8f36060fe945b9.zip
Makes user autocompleters work with firstname and lastname.
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@8875 e93f8b46-1217-0410-a6f0-8f06a7374b81
-rw-r--r--app/models/principal.rb18
-rw-r--r--app/models/user.rb8
-rw-r--r--test/unit/principal_test.rb17
3 files changed, 30 insertions, 13 deletions
diff --git a/app/models/principal.rb b/app/models/principal.rb
index 5c4e17a8b..9955501e3 100644
--- a/app/models/principal.rb
+++ b/app/models/principal.rb
@@ -27,10 +27,20 @@ class Principal < ActiveRecord::Base
named_scope :active, :conditions => "#{Principal.table_name}.status = 1"
named_scope :like, lambda {|q|
- s = "%#{q.to_s.strip.downcase}%"
- {:conditions => ["LOWER(login) LIKE :s OR LOWER(firstname) LIKE :s OR LOWER(lastname) LIKE :s OR LOWER(mail) LIKE :s", {:s => s}],
- :order => 'type, login, lastname, firstname, mail'
- }
+ if q.blank?
+ {}
+ else
+ q = q.to_s.downcase
+ pattern = "%#{q}%"
+ sql = "LOWER(login) LIKE :p OR LOWER(firstname) LIKE :p OR LOWER(lastname) LIKE :p OR LOWER(mail) LIKE :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)"
+ params.merge!(:a => a, :b => b)
+ end
+ {:conditions => [sql, params]}
+ end
}
# Principals that are members of a collection of projects
diff --git a/app/models/user.rb b/app/models/user.rb
index 02aecd341..9b0ab8110 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -56,14 +56,6 @@ class User < Principal
named_scope :active, :conditions => "#{User.table_name}.status = #{STATUS_ACTIVE}"
named_scope :logged, :conditions => "#{User.table_name}.status <> #{STATUS_ANONYMOUS}"
named_scope :status, lambda {|arg| arg.blank? ? {} : {:conditions => {:status => arg.to_i}} }
- named_scope :like, lambda {|arg|
- if arg.blank?
- {}
- else
- pattern = "%#{arg.to_s.strip.downcase}%"
- {:conditions => ["LOWER(login) LIKE :p OR LOWER(firstname) LIKE :p OR LOWER(lastname) LIKE :p OR LOWER(mail) LIKE :p", {:p => pattern}]}
- end
- }
acts_as_customizable
diff --git a/test/unit/principal_test.rb b/test/unit/principal_test.rb
index 694f7e027..c33286208 100644
--- a/test/unit/principal_test.rb
+++ b/test/unit/principal_test.rb
@@ -45,6 +45,8 @@ class PrincipalTest < ActiveSupport::TestCase
Principal.generate!(:mail => 'mail@example.com')
Principal.generate!(:mail => 'mail2@example.com')
+
+ @palmer = Principal.generate!(:firstname => 'David', :lastname => 'Palmer')
end
should "search login" do
@@ -74,6 +76,19 @@ class PrincipalTest < ActiveSupport::TestCase
assert_equal 2, results.count
assert results.all? {|u| u.mail.match(/mail/) }
end
- end
+ should "search firstname and lastname" do
+ results = Principal.like('david palm')
+
+ assert_equal 1, results.count
+ assert_equal @palmer, results.first
+ end
+
+ should "search lastname and firstname" do
+ results = Principal.like('palmer davi')
+
+ assert_equal 1, results.count
+ assert_equal @palmer, results.first
+ end
+ end
end