docs, instantiate
expects String
keys. [Rafal Piekarski & Yves Senn]
Closes #15122 Closes #15107
This commit is contained in:
parent
7591e8c1a7
commit
60bd2432b2
@ -37,7 +37,7 @@ def create(attributes = nil, &block)
|
||||
end
|
||||
|
||||
# Given an attributes hash, +instantiate+ returns a new instance of
|
||||
# the appropriate class.
|
||||
# the appropriate class. Accepts only keys as strings.
|
||||
#
|
||||
# For example, +Post.all+ may return Comments, Messages, and Emails
|
||||
# by storing the record's subclass in a +type+ attribute. By calling
|
||||
@ -46,10 +46,10 @@ def create(attributes = nil, &block)
|
||||
#
|
||||
# See +ActiveRecord::Inheritance#discriminate_class_for_record+ to see
|
||||
# how this "single-table" inheritance mapping is implemented.
|
||||
def instantiate(record, column_types = {})
|
||||
klass = discriminate_class_for_record(record)
|
||||
def instantiate(attributes, column_types = {})
|
||||
klass = discriminate_class_for_record(attributes)
|
||||
column_types = klass.decorate_columns(column_types.dup)
|
||||
klass.allocate.init_with('attributes' => record, 'column_types' => column_types)
|
||||
klass.allocate.init_with('attributes' => attributes, 'column_types' => column_types)
|
||||
end
|
||||
|
||||
private
|
||||
|
@ -843,4 +843,15 @@ def test_persist_inherited_class_with_different_table_name
|
||||
|
||||
assert_equal "Wright Glider", Aircraft.last.name
|
||||
end
|
||||
|
||||
def test_instantiate_creates_a_new_instance
|
||||
post = Post.instantiate("title" => "appropriate documentation", "type" => "SpecialPost")
|
||||
assert_equal "appropriate documentation", post.title
|
||||
assert_instance_of SpecialPost, post
|
||||
|
||||
# body was not initialized
|
||||
assert_raises ActiveModel::MissingAttributeError do
|
||||
post.body
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user