Do not build a projects_by_role Hash that gets updated when accessing a key that is not present (#11662).

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@10242 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2012-08-27 10:11:20 +00:00
parent 50506ef621
commit d33fa1f8c8
2 changed files with 13 additions and 3 deletions

View File

@ -418,10 +418,13 @@ class User < Principal
def projects_by_role def projects_by_role
return @projects_by_role if @projects_by_role return @projects_by_role if @projects_by_role
@projects_by_role = Hash.new {|h,k| h[k]=[]} @projects_by_role = Hash.new([])
memberships.each do |membership| memberships.each do |membership|
if membership.project
membership.roles.each do |role| membership.roles.each do |role|
@projects_by_role[role] << membership.project if membership.project @projects_by_role[role] = [] unless @projects_by_role.key?(role)
@projects_by_role[role] << membership.project
end
end end
end end
@projects_by_role.each do |role, projects| @projects_by_role.each do |role, projects|

View File

@ -744,6 +744,13 @@ class UserTest < ActiveSupport::TestCase
assert_equal [2], user.projects_by_role[Role.find(2)].collect(&:id).sort assert_equal [2], user.projects_by_role[Role.find(2)].collect(&:id).sort
end end
def test_accessing_projects_by_role_with_no_projects_should_return_an_empty_array
user = User.find(2)
assert_equal [], user.projects_by_role[Role.find(3)]
# should not update the hash
assert_nil user.projects_by_role.values.detect(&:blank?)
end
def test_projects_by_role_for_user_with_no_role def test_projects_by_role_for_user_with_no_role
user = User.generate! user = User.generate!
assert_equal({}, user.projects_by_role) assert_equal({}, user.projects_by_role)