From: Jean-Philippe Lang Date: Sat, 22 Sep 2012 06:16:53 +0000 (+0000) Subject: Potential can't dup NilClass error in UserPreference (#11905). X-Git-Tag: 2.2.0~352 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=838025372dc34096464cf19cdd39a1a587e4f176;p=redmine.git Potential can't dup NilClass error in UserPreference (#11905). git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@10438 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- diff --git a/app/models/user_preference.rb b/app/models/user_preference.rb index da99061a1..3c46712f9 100644 --- a/app/models/user_preference.rb +++ b/app/models/user_preference.rb @@ -44,7 +44,7 @@ class UserPreference < ActiveRecord::Base if attribute_present? attr_name super else - h = read_attribute(:others).dup || {} + h = (read_attribute(:others) || {}).dup h.update(attr_name => value) write_attribute(:others, h) value diff --git a/test/unit/user_preference_test.rb b/test/unit/user_preference_test.rb index 61dea0f59..9f49458eb 100644 --- a/test/unit/user_preference_test.rb +++ b/test/unit/user_preference_test.rb @@ -54,4 +54,19 @@ class UserPreferenceTest < ActiveSupport::TestCase assert up.save assert_kind_of Hash, up.others end + + def test_reading_value_from_nil_others_hash + up = UserPreference.new(:user => User.new) + up.others = nil + assert_nil up.others + assert_nil up[:foo] + end + + def test_writing_value_to_nil_others_hash + up = UserPreference.new(:user => User.new) + up.others = nil + assert_nil up.others + up[:foo] = 'bar' + assert_equal 'bar', up[:foo] + end end