aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2012-01-07 01:22:49 -0800
committerJosé Valim <jose.valim@gmail.com>2012-01-07 01:22:49 -0800
commitef023ce1d88691461a03fb90f0b8bff94d9fa4e1 (patch)
tree0f4e5687c85aa0348488d1855e4c1033f479bbd3
parentb5dceafd302750c7e9c8ca21d6c8a195a5a4607c (diff)
parent30468f58a7bd36143daddc64ac23f86684c1de16 (diff)
downloadrails-ef023ce1d88691461a03fb90f0b8bff94d9fa4e1.tar.gz
rails-ef023ce1d88691461a03fb90f0b8bff94d9fa4e1.tar.bz2
rails-ef023ce1d88691461a03fb90f0b8bff94d9fa4e1.zip
Merge pull request #4376 from bogdan/after_initialize_on_dup
Fixed after_initialize callbacks call on AR model #dup
-rw-r--r--activerecord/lib/active_record/core.rb2
-rw-r--r--activerecord/test/cases/dup_test.rb8
-rw-r--r--activerecord/test/models/topic.rb5
3 files changed, 14 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb
index 0755379a74..89f6eccbd2 100644
--- a/activerecord/lib/active_record/core.rb
+++ b/activerecord/lib/active_record/core.rb
@@ -210,7 +210,7 @@ module ActiveRecord
@attributes = cloned_attributes
- _run_after_initialize_callbacks if respond_to?(:_run_after_initialize_callbacks)
+ _run_initialize_callbacks if _initialize_callbacks.any?
@changed_attributes = {}
self.class.column_defaults.each do |attr, orig_value|
diff --git a/activerecord/test/cases/dup_test.rb b/activerecord/test/cases/dup_test.rb
index 0236f9b0a1..9705a11387 100644
--- a/activerecord/test/cases/dup_test.rb
+++ b/activerecord/test/cases/dup_test.rb
@@ -99,5 +99,13 @@ module ActiveRecord
assert_not_nil new_topic.created_at
end
+ def test_dup_after_initialize_callbacks
+ topic = Topic.new
+ assert Topic.after_initialize_called
+ Topic.after_initialize_called = false
+ topic.dup
+ assert Topic.after_initialize_called
+ end
+
end
end
diff --git a/activerecord/test/models/topic.rb b/activerecord/test/models/topic.rb
index 1a1a18166a..8bcb9df8a8 100644
--- a/activerecord/test/models/topic.rb
+++ b/activerecord/test/models/topic.rb
@@ -80,6 +80,11 @@ class Topic < ActiveRecord::Base
after_initialize :set_email_address
+ class_attribute :after_initialize_called
+ after_initialize do
+ self.class.after_initialize_called = true
+ end
+
def approved=(val)
@custom_approved = val
write_attribute(:approved, val)