aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel/test/cases/attribute_set_test.rb
diff options
context:
space:
mode:
authorSean Griffin <sean@seantheprogrammer.com>2017-11-27 14:06:51 -0700
committerSean Griffin <sean@seantheprogrammer.com>2017-11-27 14:06:51 -0700
commit95b86e57a6c07bc636f8feab4afd4c7fe0ca512c (patch)
treec665e7cd5c61f2c6cd2dae0374838e8c144c98b5 /activemodel/test/cases/attribute_set_test.rb
parent924a368f5c654f5304e575c767eb0fc64adc8659 (diff)
downloadrails-95b86e57a6c07bc636f8feab4afd4c7fe0ca512c.tar.gz
rails-95b86e57a6c07bc636f8feab4afd4c7fe0ca512c.tar.bz2
rails-95b86e57a6c07bc636f8feab4afd4c7fe0ca512c.zip
Change how `AttributeSet::Builder` receives its defaults
There are two concerns which are both being combined into one here, but both have the same goal. There are certain attributes which we want to always consider initialized. Previously, they were handled separately. The primary key (which is assumed to be backed by a database column) needs to be initialized, because there is a ton of code in Active Record that assumes `foo.id` will never raise. Additionally, we want attributes which aren't backed by a database column to always be initialized, since we would never receive a database value for them. Ultimately these two concerns can be combined into one. The old implementation hid a lot of inherent complexity, and is hard to optimize from the outside. We can simplify things significantly by just passing in a hash. This has slightly different semantics from the old behavior, in that `Foo.select(:bar).first.id` will return the default value for the primary key, rather than `nil` unconditionally -- however, the default value is always `nil` in practice.
Diffstat (limited to 'activemodel/test/cases/attribute_set_test.rb')
-rw-r--r--activemodel/test/cases/attribute_set_test.rb3
1 files changed, 2 insertions, 1 deletions
diff --git a/activemodel/test/cases/attribute_set_test.rb b/activemodel/test/cases/attribute_set_test.rb
index 02c44c5d45..6e522d6c80 100644
--- a/activemodel/test/cases/attribute_set_test.rb
+++ b/activemodel/test/cases/attribute_set_test.rb
@@ -163,7 +163,8 @@ module ActiveModel
end
test "the primary_key is always initialized" do
- builder = AttributeSet::Builder.new({ foo: Type::Integer.new }, :foo)
+ defaults = { foo: Attribute.from_user(:foo, nil, nil) }
+ builder = AttributeSet::Builder.new({ foo: Type::Integer.new }, defaults)
attributes = builder.build_from_database
assert attributes.key?(:foo)