@@ -44,7 +44,7 class UserPreference < ActiveRecord::Base | |||||
44 | if attribute_present? attr_name |
|
44 | if attribute_present? attr_name | |
45 | super |
|
45 | super | |
46 | else |
|
46 | else | |
47 |
h = read_attribute(:others) |
|
47 | h = (read_attribute(:others) || {}).dup | |
48 | h.update(attr_name => value) |
|
48 | h.update(attr_name => value) | |
49 | write_attribute(:others, h) |
|
49 | write_attribute(:others, h) | |
50 | value |
|
50 | value |
@@ -54,4 +54,19 class UserPreferenceTest < ActiveSupport::TestCase | |||||
54 | assert up.save |
|
54 | assert up.save | |
55 | assert_kind_of Hash, up.others |
|
55 | assert_kind_of Hash, up.others | |
56 | end |
|
56 | end | |
|
57 | ||||
|
58 | def test_reading_value_from_nil_others_hash | |||
|
59 | up = UserPreference.new(:user => User.new) | |||
|
60 | up.others = nil | |||
|
61 | assert_nil up.others | |||
|
62 | assert_nil up[:foo] | |||
|
63 | end | |||
|
64 | ||||
|
65 | def test_writing_value_to_nil_others_hash | |||
|
66 | up = UserPreference.new(:user => User.new) | |||
|
67 | up.others = nil | |||
|
68 | assert_nil up.others | |||
|
69 | up[:foo] = 'bar' | |||
|
70 | assert_equal 'bar', up[:foo] | |||
|
71 | end | |||
57 | end |
|
72 | end |
General Comments 0
You need to be logged in to leave comments.
Login now