aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/core.rb
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2018-09-26 04:17:35 +0900
committerRyuta Kamizono <kamipo@gmail.com>2018-10-17 10:51:53 +0900
commit1198a3880bb2918bb771b55d94cfb333813744fa (patch)
tree55c7bdd9ada28997f5d08aab764622fb5256d9d9 /activerecord/lib/active_record/core.rb
parentbb0c02e9b5178a2833ca509494e28d85e5d3d5df (diff)
downloadrails-1198a3880bb2918bb771b55d94cfb333813744fa.tar.gz
rails-1198a3880bb2918bb771b55d94cfb333813744fa.tar.bz2
rails-1198a3880bb2918bb771b55d94cfb333813744fa.zip
Consolidate duplicated code that initializing an empty model object
`init_with` and `init_from_db` are almost the same code except decode `coder`. And also, named `init_from_db` is a little misreading, a raw values hash from the database is already converted to an attributes object by `attributes_builder.build_from_database`, so passed `attributes` in that method is just an attributes object. I renamed that method to `init_with_attributes` since the method is shared with `init_with` to initialize an empty model object.
Diffstat (limited to 'activerecord/lib/active_record/core.rb')
-rw-r--r--activerecord/lib/active_record/core.rb26
1 files changed, 7 insertions, 19 deletions
diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb
index b53681ee20..0718688863 100644
--- a/activerecord/lib/active_record/core.rb
+++ b/activerecord/lib/active_record/core.rb
@@ -344,34 +344,22 @@ module ActiveRecord
# post = Post.allocate
# post.init_with(coder)
# post.title # => 'hello world'
- def init_with(coder)
+ def init_with(coder, &block)
coder = LegacyYamlAdapter.convert(self.class, coder)
- @attributes = self.class.yaml_encoder.decode(coder)
-
- init_internals
-
- @new_record = coder["new_record"]
-
- self.class.define_attribute_methods
-
- yield self if block_given?
-
- _run_find_callbacks
- _run_initialize_callbacks
-
- self
+ attributes = self.class.yaml_encoder.decode(coder)
+ init_with_attributes(attributes, coder["new_record"], &block)
end
##
- # Initializer used for instantiating objects that have been read from the
- # database. +attributes+ should be an attributes object, and unlike the
+ # Initialize an empty model object from +attributes+.
+ # +attributes+ should be an attributes object, and unlike the
# `initialize` method, no assignment calls are made per attribute.
#
# :nodoc:
- def init_from_db(attributes)
+ def init_with_attributes(attributes, new_record = false)
init_internals
- @new_record = false
+ @new_record = new_record
@attributes = attributes
self.class.define_attribute_methods