aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorSean Griffin <sean@seantheprogrammer.com>2015-09-28 16:17:08 -0400
committerSean Griffin <sean@seantheprogrammer.com>2015-09-28 16:26:50 -0400
commitfb03a9ab35ed22e569ec9cef8a50ef72754b5dbe (patch)
tree6a3bd9b1b02e35f452c8158544a8748d7a6ec301 /activerecord/test
parente950c4b4a503d801ac141c143171dbffe758e6eb (diff)
downloadrails-fb03a9ab35ed22e569ec9cef8a50ef72754b5dbe.tar.gz
rails-fb03a9ab35ed22e569ec9cef8a50ef72754b5dbe.tar.bz2
rails-fb03a9ab35ed22e569ec9cef8a50ef72754b5dbe.zip
Separate `dup` from `deep_dup` in the attributes hash
I'm looking to move towards a tree-like structure for dirty checking that involves an attribute holding onto the attribute that it was created from. This means that `changed?` can be fully encapsulated on that object. Since the objects are immutable, in `changes_applied`, we can simply perform a shallow dup, instead of a deep one. I'm not sure if that will actually end up in a performance boost, but I'd like to semantically separate these concepts regardless
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/attribute_set_test.rb20
1 files changed, 19 insertions, 1 deletions
diff --git a/activerecord/test/cases/attribute_set_test.rb b/activerecord/test/cases/attribute_set_test.rb
index 7524243270..5a0e463a48 100644
--- a/activerecord/test/cases/attribute_set_test.rb
+++ b/activerecord/test/cases/attribute_set_test.rb
@@ -29,7 +29,7 @@ module ActiveRecord
assert_equal :bar, attributes[:bar].name
end
- test "duping creates a new hash and dups each attribute" do
+ test "duping creates a new hash, but does not dup the attributes" do
builder = AttributeSet::Builder.new(foo: Type::Integer.new, bar: Type::String.new)
attributes = builder.build_from_database(foo: 1, bar: 'foo')
@@ -43,6 +43,24 @@ module ActiveRecord
assert_equal 1, attributes[:foo].value
assert_equal 2, duped[:foo].value
+ assert_equal 'foobar', attributes[:bar].value
+ assert_equal 'foobar', duped[:bar].value
+ end
+
+ test "deep_duping creates a new hash and dups each attribute" do
+ builder = AttributeSet::Builder.new(foo: Type::Integer.new, bar: Type::String.new)
+ attributes = builder.build_from_database(foo: 1, bar: 'foo')
+
+ # Ensure the type cast value is cached
+ attributes[:foo].value
+ attributes[:bar].value
+
+ duped = attributes.deep_dup
+ duped.write_from_database(:foo, 2)
+ duped[:bar].value << 'bar'
+
+ assert_equal 1, attributes[:foo].value
+ assert_equal 2, duped[:foo].value
assert_equal 'foo', attributes[:bar].value
assert_equal 'foobar', duped[:bar].value
end