提交 72b92e81 编写于 作者: Y Yves Senn

don't rely on the columns hash to get defaults. follow-up to #17169.

This will also get the defaults from attribute definitions like:

     attribute :type, :string, default: "SomethingElse"
上级 4294a7ee
......@@ -52,7 +52,7 @@ def new(*args, &block)
attrs = args.first
if has_attribute?(inheritance_column)
subclass = subclass_from_attributes(attrs) || subclass_from_defaults
subclass = subclass_from_attributes(attrs) || subclass_from_attributes(column_defaults)
end
if subclass && subclass != self
......@@ -207,12 +207,6 @@ def subclass_from_attributes(attrs)
end
end
end
def subclass_from_defaults
if default = columns_hash[inheritance_column].default
find_sti_class(default)
end
end
end
def initialize_dup(other)
......
......@@ -500,3 +500,27 @@ def test_inheritance_new_with_subclass_as_default
Company.reset_column_information
end
end
class InheritanceAttributeTest < ActiveRecord::TestCase
class Company < ActiveRecord::Base
self.table_name = 'companies'
attribute :type, :string, default: "InheritanceAttributeTest::Startup"
end
class Startup < Company
end
class Empire < Company
end
def test_inheritance_new_with_subclass_as_default
startup = Company.new # without arguments
assert_equal 'InheritanceAttributeTest::Startup', startup.type
assert_instance_of Startup, startup
empire = Company.new(type: 'InheritanceAttributeTest::Empire') # without arguments
assert_equal 'InheritanceAttributeTest::Empire', empire.type
assert_instance_of Empire, empire
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册