未验证 提交 8a908451 编写于 作者: S Sean Griffin 提交者: GitHub

Merge pull request #33925 from rafaelfranca/rm-fix-column_defaults

Make a deep copy of the _default_attributes in column_defaults
......@@ -375,7 +375,7 @@ def type_for_attribute(attr_name, &block)
# default values when instantiating the Active Record object for this table.
def column_defaults
load_schema
@column_defaults ||= _default_attributes.to_hash
@column_defaults ||= _default_attributes.deep_dup.to_hash
end
def _default_attributes # :nodoc:
......
......@@ -148,6 +148,20 @@ def deserialize(*)
assert_equal 2, klass.new.counter
end
test "procs for default values are evaluated even after column_defaults is called" do
klass = Class.new(OverloadedType) do
@@counter = 0
attribute :counter, :integer, default: -> { @@counter += 1 }
end
assert_equal 1, klass.new.counter
# column_defaults will increment the counter since the proc is called
klass.column_defaults
assert_equal 3, klass.new.counter
end
test "procs are memoized before type casting" do
klass = Class.new(OverloadedType) do
@@counter = 0
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册