diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2012-08-27 10:11:20 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2012-08-27 10:11:20 +0000 |
commit | d33fa1f8c87f1de83eb56c2f0376a2e3f7f6ed86 (patch) | |
tree | de452cc4b7affe6c39d4c8354291e7ae72bd8d14 | |
parent | 50506ef621b1f4d67a8fd350b1d94bf09dd07c9b (diff) | |
download | redmine-d33fa1f8c87f1de83eb56c2f0376a2e3f7f6ed86.tar.gz redmine-d33fa1f8c87f1de83eb56c2f0376a2e3f7f6ed86.zip |
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
-rw-r--r-- | app/models/user.rb | 9 | ||||
-rw-r--r-- | test/unit/user_test.rb | 7 |
2 files changed, 13 insertions, 3 deletions
diff --git a/app/models/user.rb b/app/models/user.rb index 9350fb95c..73c8d6c5d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -418,10 +418,13 @@ class User < Principal def 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| - membership.roles.each do |role| - @projects_by_role[role] << membership.project if membership.project + if membership.project + membership.roles.each do |role| + @projects_by_role[role] = [] unless @projects_by_role.key?(role) + @projects_by_role[role] << membership.project + end end end @projects_by_role.each do |role, projects| diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index 51a49de01..7b4dac725 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -744,6 +744,13 @@ class UserTest < ActiveSupport::TestCase assert_equal [2], user.projects_by_role[Role.find(2)].collect(&:id).sort 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 user = User.generate! assert_equal({}, user.projects_by_role) |