@@ -49,6 +49,33 @@ def test_attributes_can_be_read_for_serialization
4949 assert_equal 1 , instance . read_attribute_for_serialization ( :one )
5050 end
5151
52+ def test_attributes_can_be_read_for_serialization_with_attributes_accessors_fix
53+ klass = Class . new ( ActiveModelSerializers ::Model ) do
54+ derive_attributes_from_names_and_fix_accessors
55+ attributes :one , :two , :three
56+ end
57+ original_attributes = { one : 1 , two : 2 , three : 3 }
58+ original_instance = klass . new ( original_attributes )
59+
60+ # Initial value
61+ instance = original_instance
62+ expected_attributes = { one : 1 , two : 2 , three : 3 } . with_indifferent_access
63+ assert_equal expected_attributes , instance . attributes
64+ assert_equal 1 , instance . one
65+ assert_equal 1 , instance . read_attribute_for_serialization ( :one )
66+
67+ expected_attributes = { one : :not_one , two : 2 , three : 3 } . with_indifferent_access
68+ # Change via accessor
69+ instance = original_instance . dup
70+ instance . one = :not_one
71+ assert_equal expected_attributes , instance . attributes
72+ assert_equal :not_one , instance . one
73+ assert_equal :not_one , instance . read_attribute_for_serialization ( :one )
74+
75+ # Attributes frozen
76+ assert instance . attributes . frozen?
77+ end
78+
5279 def test_id_attribute_can_be_read_for_serialization
5380 klass = Class . new ( ActiveModelSerializers ::Model ) do
5481 attributes :id , :one , :two , :three
@@ -81,5 +108,35 @@ def test_id_attribute_can_be_read_for_serialization
81108 ensure
82109 self . class . send ( :remove_const , :SomeTestModel )
83110 end
111+
112+ def test_id_attribute_can_be_read_for_serialization_with_attributes_accessors_fix
113+ klass = Class . new ( ActiveModelSerializers ::Model ) do
114+ derive_attributes_from_names_and_fix_accessors
115+ attributes :id , :one , :two , :three
116+ end
117+ self . class . const_set ( :SomeTestModel , klass )
118+ original_attributes = { id : :ego , one : 1 , two : 2 , three : 3 }
119+ original_instance = klass . new ( original_attributes )
120+
121+ # Initial value
122+ instance = original_instance . dup
123+ expected_attributes = { id : :ego , one : 1 , two : 2 , three : 3 } . with_indifferent_access
124+ assert_equal expected_attributes , instance . attributes
125+ assert_equal :ego , instance . id
126+ assert_equal :ego , instance . read_attribute_for_serialization ( :id )
127+
128+ expected_attributes = { id : :superego , one : 1 , two : 2 , three : 3 } . with_indifferent_access
129+ # Change via accessor
130+ instance = original_instance . dup
131+ instance . id = :superego
132+ assert_equal expected_attributes , instance . attributes
133+ assert_equal :superego , instance . id
134+ assert_equal :superego , instance . read_attribute_for_serialization ( :id )
135+
136+ # Attributes frozen
137+ assert instance . attributes . frozen?
138+ ensure
139+ self . class . send ( :remove_const , :SomeTestModel )
140+ end
84141 end
85142end
0 commit comments