Permalink
Browse files

docs, `instantiate` expects `String` keys. [Rafal Piekarski & Yves Senn]

Closes #15122
Closes #15107
  • Loading branch information...
1 parent 7591e8c commit 60bd2432b2f1fa5a84554aea38b8971368b79cfe @senny senny committed May 20, 2014
Showing with 15 additions and 4 deletions.
  1. +4 −4 activerecord/lib/active_record/persistence.rb
  2. +11 −0 activerecord/test/cases/persistence_test.rb
@@ -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

0 comments on commit 60bd243

Please sign in to comment.