Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added that all types of after_find/after_initialized callbacks are tr…

…iggered if the explicit implementation is present, not only the explicit implementation itself

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@898 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 7edb10fe682d5fa9802b2b5355b1073444042fc4 1 parent 0f4ee4b
@dhh dhh authored
View
2  activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added that all types of after_find/after_initialized callbacks are triggered if the explicit implementation is present, not only the explicit implementation itself
+
* Fixed that symbols can be used on attribute assignment, like page.emails.create(:subject => data.subject, :body => data.body)
View
21 activerecord/lib/active_record/callbacks.rb
@@ -151,8 +151,8 @@ module ActiveRecord
#
# Because after_find and after_initialize is called for each object instantiated found by a finder, such as Base.find_all, we've had
# to implement a simple performance constraint (50% more speed on a simple test case). Unlike all the other callbacks, after_find and
- # after_initialize can only be declared using an explicit implementation. So using the inheritable callback queue for after_find and
- # after_initialize won't work.
+ # after_initialize will only be run if an explicit implementation is defined (<tt>def after_find</tt>). In that case, all of the
+ # callback types will be called.
#
# == Cancelling callbacks
#
@@ -209,8 +209,19 @@ def self.#{method}(*callbacks, &block)
module ClassMethods #:nodoc:
def instantiate_with_callbacks(record)
object = instantiate_without_callbacks(record)
- object.send(:invoke_and_notify, :after_find)
- object.send(:invoke_and_notify, :after_initialize)
+
+ if object.send(:respond_to_without_attributes?, :after_find)
+ object.send(:callback, :after_find)
+ else
+ object.send(:invoke_and_notify, :after_find)
+ end
+
+ if object.send(:respond_to_without_attributes?, :after_initialize)
+ object.send(:callback, :after_initialize)
+ else
+ object.send(:invoke_and_notify, :after_initialize)
+ end
+
object
end
end
@@ -223,7 +234,7 @@ def instantiate_with_callbacks(record)
def initialize_with_callbacks(attributes = nil) #:nodoc:
initialize_without_callbacks(attributes)
result = yield self if block_given?
- invoke_and_notify(:after_initialize)
+ respond_to_without_attributes?(:after_initialize) ? callback(:after_initialize) : invoke_and_notify(:after_initialize)
result
end
View
80 activerecord/test/callbacks_test.rb
@@ -41,14 +41,11 @@ def history
@history ||= []
end
- # after_initialize and after_find may not be declared using class methods.
- # They are invoked only if instance methods have been defined.
+ # after_initialize and after_find are invoked only if instance methods have been defined.
def after_initialize
- history << [:after_initialize, :method]
end
def after_find
- history << [:after_find, :method]
end
end
@@ -61,15 +58,24 @@ def setup
def test_initialize
david = CallbackDeveloper.new
assert_equal [
- [ :after_initialize, :method ]
+ [ :after_initialize, :string ],
+ [ :after_initialize, :proc ],
+ [ :after_initialize, :object ],
+ [ :after_initialize, :block ],
], david.history
end
def test_find
david = CallbackDeveloper.find(1)
assert_equal [
- [ :after_find, :method ],
- [ :after_initialize, :method ]
+ [ :after_find, :string ],
+ [ :after_find, :proc ],
+ [ :after_find, :object ],
+ [ :after_find, :block ],
+ [ :after_initialize, :string ],
+ [ :after_initialize, :proc ],
+ [ :after_initialize, :object ],
+ [ :after_initialize, :block ],
], david.history
end
@@ -77,7 +83,10 @@ def test_new_valid?
david = CallbackDeveloper.new
david.valid?
assert_equal [
- [ :after_initialize, :method ],
+ [ :after_initialize, :string ],
+ [ :after_initialize, :proc ],
+ [ :after_initialize, :object ],
+ [ :after_initialize, :block ],
[ :before_validation, :string ],
[ :before_validation, :proc ],
[ :before_validation, :object ],
@@ -101,8 +110,14 @@ def test_existing_valid?
david = CallbackDeveloper.find(1)
david.valid?
assert_equal [
- [ :after_find, :method ],
- [ :after_initialize, :method ],
+ [ :after_find, :string ],
+ [ :after_find, :proc ],
+ [ :after_find, :object ],
+ [ :after_find, :block ],
+ [ :after_initialize, :string ],
+ [ :after_initialize, :proc ],
+ [ :after_initialize, :object ],
+ [ :after_initialize, :block ],
[ :before_validation, :string ],
[ :before_validation, :proc ],
[ :before_validation, :object ],
@@ -125,7 +140,10 @@ def test_existing_valid?
def test_create
david = CallbackDeveloper.create('name' => 'David', 'salary' => 1000000)
assert_equal [
- [ :after_initialize, :method ],
+ [ :after_initialize, :string ],
+ [ :after_initialize, :proc ],
+ [ :after_initialize, :object ],
+ [ :after_initialize, :block ],
[ :before_validation, :string ],
[ :before_validation, :proc ],
[ :before_validation, :object ],
@@ -165,8 +183,14 @@ def test_save
david = CallbackDeveloper.find(1)
david.save
assert_equal [
- [ :after_find, :method ],
- [ :after_initialize, :method ],
+ [ :after_find, :string ],
+ [ :after_find, :proc ],
+ [ :after_find, :object ],
+ [ :after_find, :block ],
+ [ :after_initialize, :string ],
+ [ :after_initialize, :proc ],
+ [ :after_initialize, :object ],
+ [ :after_initialize, :block ],
[ :before_validation, :string ],
[ :before_validation, :proc ],
[ :before_validation, :object ],
@@ -206,8 +230,14 @@ def test_destroy
david = CallbackDeveloper.find(1)
david.destroy
assert_equal [
- [ :after_find, :method ],
- [ :after_initialize, :method ],
+ [ :after_find, :string ],
+ [ :after_find, :proc ],
+ [ :after_find, :object ],
+ [ :after_find, :block ],
+ [ :after_initialize, :string ],
+ [ :after_initialize, :proc ],
+ [ :after_initialize, :object ],
+ [ :after_initialize, :block ],
[ :before_destroy, :string ],
[ :before_destroy, :proc ],
[ :before_destroy, :object ],
@@ -223,8 +253,14 @@ def test_delete
david = CallbackDeveloper.find(1)
CallbackDeveloper.delete(david.id)
assert_equal [
- [ :after_find, :method ],
- [ :after_initialize, :method ]
+ [ :after_find, :string ],
+ [ :after_find, :proc ],
+ [ :after_find, :object ],
+ [ :after_find, :block ],
+ [ :after_initialize, :string ],
+ [ :after_initialize, :proc ],
+ [ :after_initialize, :object ],
+ [ :after_initialize, :block ],
], david.history
end
@@ -234,8 +270,14 @@ def test_zzz_callback_returning_false # must be run last since we modify Callbac
CallbackDeveloper.before_validation proc { |model| model.history << [:before_validation, :should_never_get_here] }
david.save
assert_equal [
- [ :after_find, :method ],
- [ :after_initialize, :method ],
+ [ :after_find, :string ],
+ [ :after_find, :proc ],
+ [ :after_find, :object ],
+ [ :after_find, :block ],
+ [ :after_initialize, :string ],
+ [ :after_initialize, :proc ],
+ [ :after_initialize, :object ],
+ [ :after_initialize, :block ],
[ :before_validation, :string ],
[ :before_validation, :proc ],
[ :before_validation, :object ],
Please sign in to comment.
Something went wrong with that request. Please try again.