Fixed potential error when sorting members without role (#10053).

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@8863 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2012-02-12 14:54:30 +00:00
parent 10a462369b
commit d289619c75
3 changed files with 31 additions and 2 deletions

View File

@ -58,7 +58,17 @@ class Member < ActiveRecord::Base
def <=>(member) def <=>(member)
a, b = roles.sort.first, member.roles.sort.first a, b = roles.sort.first, member.roles.sort.first
a == b ? (principal <=> member.principal) : (a <=> b) if a == b
if principal
principal <=> member.principal
else
1
end
elsif a
a <=> b
else
1
end
end end
def deletable? def deletable?

View File

@ -50,7 +50,9 @@ class Principal < ActiveRecord::Base
end end
def <=>(principal) def <=>(principal)
if self.class.name == principal.class.name if principal.nil?
-1
elsif self.class.name == principal.class.name
self.to_s.downcase <=> principal.to_s.downcase self.to_s.downcase <=> principal.to_s.downcase
else else
# groups after users # groups after users

View File

@ -106,6 +106,23 @@ class MemberTest < ActiveSupport::TestCase
assert_nil category1.assigned_to_id assert_nil category1.assigned_to_id
end end
def test_sort_without_roles
a = Member.new(:roles => [Role.first])
b = Member.new
assert_equal -1, a <=> b
assert_equal 1, b <=> a
end
def test_sort_without_principal
role = Role.first
a = Member.new(:roles => [role], :principal => User.first)
b = Member.new(:roles => [role])
assert_equal -1, a <=> b
assert_equal 1, b <=> a
end
context "removing permissions" do context "removing permissions" do
setup do setup do
Watcher.delete_all("user_id = 9") Watcher.delete_all("user_id = 9")