File tree Expand file tree Collapse file tree 2 files changed +24
-3
lines changed Expand file tree Collapse file tree 2 files changed +24
-3
lines changed Original file line number Diff line number Diff line change @@ -178,11 +178,11 @@ def type_cast(value)
178
178
def forgetting_assignment
179
179
# If this attribute was not persisted (with a `value_for_database`
180
180
# that might differ from `value_before_type_cast`) and `value` has not
181
- # changed in place, we can simply dup this attribute to avoid
182
- # deserialize / cast / serialize calls from computing the new
181
+ # changed in place, we can use the existing `value_before_type_cast`
182
+ # to avoid deserialize / cast / serialize calls from computing the new
183
183
# attribute's `value_before_type_cast`.
184
184
if !defined? ( @value_for_database ) && !changed_in_place?
185
- dup
185
+ with_value_from_database ( value_before_type_cast )
186
186
else
187
187
super
188
188
end
Original file line number Diff line number Diff line change @@ -285,6 +285,27 @@ def assert_valid_value(*)
285
285
assert_not_predicate forgotten , :changed?
286
286
end
287
287
288
+ test "#forgetting_assignment on an unchanged .from_database attribute re-deserializes its value" do
289
+ deserialized_value_class = Struct . new ( :id ) do
290
+ def initialize_dup ( *)
291
+ self . id = nil # a la ActiveRecord::Base#dup
292
+ end
293
+ end
294
+
295
+ type = Type ::Value . new
296
+ type . define_singleton_method ( :deserialize ) do |value |
297
+ deserialized_value_class . new ( value )
298
+ end
299
+
300
+ original = Attribute . from_database ( :foo , 123 , type )
301
+ assert_equal 123 , original . value . id
302
+
303
+ forgotten = original . forgetting_assignment
304
+ assert_equal 123 , forgotten . value . id
305
+
306
+ assert_not_same original . value , forgotten . value
307
+ end
308
+
288
309
test "with_value_from_user validates the value" do
289
310
type = Type ::Value . new
290
311
type . define_singleton_method ( :assert_valid_value ) do |value |
You can’t perform that action at this time.
0 commit comments