aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/callbacks.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/lib/active_record/callbacks.rb')
-rwxr-xr-xactiverecord/lib/active_record/callbacks.rb100
1 files changed, 50 insertions, 50 deletions
diff --git a/activerecord/lib/active_record/callbacks.rb b/activerecord/lib/active_record/callbacks.rb
index 7a19dcb746..83c54680b5 100755
--- a/activerecord/lib/active_record/callbacks.rb
+++ b/activerecord/lib/active_record/callbacks.rb
@@ -1,25 +1,25 @@
require 'observer'
module ActiveRecord
- # Callbacks are hooks into the lifecycle of an Active Record object that allows you to trigger logic
+ # Callbacks are hooks into the lifecycle of an Active Record object that allow you to trigger logic
# before or after an alteration of the object state. This can be used to make sure that associated and
- # dependent objects are deleted when destroy is called (by overwriting before_destroy) or to massage attributes
- # before they're validated (by overwriting before_validation). As an example of the callbacks initiated, consider
- # the Base#save call:
- #
- # * (-) save
- # * (-) valid?
- # * (1) before_validation
- # * (2) before_validation_on_create
- # * (-) validate
- # * (-) validate_on_create
- # * (3) after_validation
- # * (4) after_validation_on_create
- # * (5) before_save
- # * (6) before_create
- # * (-) create
- # * (7) after_create
- # * (8) after_save
+ # dependent objects are deleted when destroy is called (by overwriting +before_destroy+) or to massage attributes
+ # before they're validated (by overwriting +before_validation+). As an example of the callbacks initiated, consider
+ # the <tt>Base#save</tt> call:
+ #
+ # * (-) <tt>save</tt>
+ # * (-) <tt>valid</tt>
+ # * (1) <tt>before_validation</tt>
+ # * (2) <tt>before_validation_on_create</tt>
+ # * (-) <tt>validate</tt>
+ # * (-) <tt>validate_on_create</tt>
+ # * (3) <tt>after_validation</tt>
+ # * (4) <tt>after_validation_on_create</tt>
+ # * (5) <tt>before_save</tt>
+ # * (6) <tt>before_create</tt>
+ # * (-) <tt>create</tt>
+ # * (7) <tt>after_create</tt>
+ # * (8) <tt>after_save</tt>
#
# That's a total of eight callbacks, which gives you immense power to react and prepare for each state in the
# Active Record lifecycle.
@@ -62,8 +62,8 @@ module ActiveRecord
# before_destroy :destroy_readers
# end
#
- # Now, when Topic#destroy is run only +destroy_author+ is called. When Reply#destroy is run both +destroy_author+ and
- # +destroy_readers+ is called. Contrast this to the situation where we've implemented the save behavior through overwriteable
+ # Now, when <tt>Topic#destroy</tt> is run only +destroy_author+ is called. When <tt>Reply#destroy</tt> is run, both +destroy_author+ and
+ # +destroy_readers+ are called. Contrast this to the situation where we've implemented the save behavior through overwriteable
# methods:
#
# class Topic < ActiveRecord::Base
@@ -74,9 +74,9 @@ module ActiveRecord
# def before_destroy() destroy_readers end
# end
#
- # In that case, Reply#destroy would only run +destroy_readers+ and _not_ +destroy_author+. So use the callback macros when
- # you want to ensure that a certain callback is called for the entire hierarchy and the regular overwriteable methods when you
- # want to leave it up to each descendent to decide whether they want to call +super+ and trigger the inherited callbacks.
+ # In that case, <tt>Reply#destroy</tt> would only run +destroy_readers+ and _not_ +destroy_author+. So, use the callback macros when
+ # you want to ensure that a certain callback is called for the entire hierarchy, and use the regular overwriteable methods
+ # when you want to leave it up to each descendent to decide whether they want to call +super+ and trigger the inherited callbacks.
#
# *IMPORTANT:* In order for inheritance to work for the callback queues, you must specify the callbacks before specifying the
# associations. Otherwise, you might trigger the loading of a child before the parent has registered the callbacks and they won't
@@ -143,7 +143,7 @@ module ActiveRecord
# before_destroy 'self.class.delete_all "parent_id = #{id}"'
# end
#
- # Notice that single plings (') are used so the #{id} part isn't evaluated until the callback is triggered. Also note that these
+ # Notice that single plings (') are used so the <tt>#{id}</tt> part isn't evaluated until the callback is triggered. Also note that these
# inline callbacks can be stacked just like the regular ones:
#
# class Topic < ActiveRecord::Base
@@ -151,23 +151,23 @@ module ActiveRecord
# 'puts "Evaluated after parents are destroyed"'
# end
#
- # == The after_find and after_initialize exceptions
+ # == The +after_find+ and +after_initialize+ exceptions
#
- # Because after_find and after_initialize are called for each object found and instantiated 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 will only be run if an explicit implementation is defined (<tt>def after_find</tt>). In that case, all of the
+ # Because +after_find+ and +after_initialize+ are called for each object found and instantiated by a finder, such as <tt>Base.find(:all)</tt>, 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+ 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.
#
- # == before_validation* returning statements
+ # == <tt>before_validation*</tt> returning statements
#
- # If the returning value of a before_validation callback can be evaluated to false, the process will be aborted and Base#save will return false.
- # If Base#save! is called it will raise a RecordNotSave error.
+ # If the returning value of a +before_validation+ callback can be evaluated to +false+, the process will be aborted and <tt>Base#save</tt> will return +false+.
+ # If <tt>Base#save!</tt> is called it will raise a +RecordNotSave+ exception.
# Nothing will be appended to the errors object.
#
# == Cancelling callbacks
#
- # If a before_* callback returns false, all the later callbacks and the associated action are cancelled. If an after_* callback returns
- # false, all the later callbacks are cancelled. Callbacks are generally run in the order they are defined, with the exception of callbacks
+ # If a <tt>before_*</tt> callback returns +false+, all the later callbacks and the associated action are cancelled. If an <tt>after_*</tt> callback returns
+ # +false+, all the later callbacks are cancelled. Callbacks are generally run in the order they are defined, with the exception of callbacks
# defined as methods on the model, which are called last.
module Callbacks
CALLBACKS = %w(
@@ -215,10 +215,10 @@ module ActiveRecord
end
end
- # Is called when the object was instantiated by one of the finders, like Base.find.
+ # Is called when the object was instantiated by one of the finders, like <tt>Base.find</tt>.
#def after_find() end
- # Is called after the object has been instantiated by a call to Base.new.
+ # Is called after the object has been instantiated by a call to <tt>Base.new</tt>.
#def after_initialize() end
def initialize_with_callbacks(attributes = nil) #:nodoc:
@@ -229,10 +229,10 @@ module ActiveRecord
end
private :initialize_with_callbacks
- # Is called _before_ Base.save (regardless of whether it's a create or update save).
+ # Is called _before_ <tt>Base.save</tt> (regardless of whether it's a +create+ or +update+ save).
def before_save() end
- # Is called _after_ Base.save (regardless of whether it's a create or update save).
+ # Is called _after_ <tt>Base.save</tt> (regardless of whether it's a +create+ or +update+ save).
#
# class Contact < ActiveRecord::Base
# after_save { logger.info( 'New contact saved!' ) }
@@ -246,10 +246,10 @@ module ActiveRecord
end
private :create_or_update_with_callbacks
- # Is called _before_ Base.save on new objects that haven't been saved yet (no record exists).
+ # Is called _before_ <tt>Base.save</tt> on new objects that haven't been saved yet (no record exists).
def before_create() end
- # Is called _after_ Base.save on new objects that haven't been saved yet (no record exists).
+ # Is called _after_ <tt>Base.save</tt> on new objects that haven't been saved yet (no record exists).
def after_create() end
def create_with_callbacks #:nodoc:
return false if callback(:before_create) == false
@@ -259,10 +259,10 @@ module ActiveRecord
end
private :create_with_callbacks
- # Is called _before_ Base.save on existing objects that have a record.
+ # Is called _before_ <tt>Base.save</tt> on existing objects that have a record.
def before_update() end
- # Is called _after_ Base.save on existing objects that have a record.
+ # Is called _after_ <tt>Base.save</tt> on existing objects that have a record.
def after_update() end
def update_with_callbacks #:nodoc:
@@ -273,25 +273,25 @@ module ActiveRecord
end
private :update_with_callbacks
- # Is called _before_ Validations.validate (which is part of the Base.save call).
+ # Is called _before_ <tt>Validations.validate</tt> (which is part of the <tt>Base.save</tt> call).
def before_validation() end
- # Is called _after_ Validations.validate (which is part of the Base.save call).
+ # Is called _after_ <tt>Validations.validate</tt> (which is part of the <tt>Base.save</tt> call).
def after_validation() end
- # Is called _before_ Validations.validate (which is part of the Base.save call) on new objects
+ # Is called _before_ <tt>Validations.validate</tt> (which is part of the <tt>Base.save</tt> call) on new objects
# that haven't been saved yet (no record exists).
def before_validation_on_create() end
- # Is called _after_ Validations.validate (which is part of the Base.save call) on new objects
+ # Is called _after_ <tt>Validations.validate</tt> (which is part of the <tt>Base.save</tt> call) on new objects
# that haven't been saved yet (no record exists).
def after_validation_on_create() end
- # Is called _before_ Validations.validate (which is part of the Base.save call) on
+ # Is called _before_ <tt>Validations.validate</tt> (which is part of the <tt>Base.save</tt> call) on
# existing objects that have a record.
def before_validation_on_update() end
- # Is called _after_ Validations.validate (which is part of the Base.save call) on
+ # Is called _after_ <tt>Validations.validate</tt> (which is part of the <tt>Base.save</tt> call) on
# existing objects that have a record.
def after_validation_on_update() end
@@ -308,13 +308,13 @@ module ActiveRecord
return result
end
- # Is called _before_ Base.destroy.
+ # Is called _before_ <tt>Base.destroy</tt>.
#
# Note: If you need to _destroy_ or _nullify_ associated records first,
- # use the _:dependent_ option on your associations.
+ # use the <tt>:dependent</tt> option on your associations.
def before_destroy() end
- # Is called _after_ Base.destroy (and all the attributes have been frozen).
+ # Is called _after_ <tt>Base.destroy</tt> (and all the attributes have been frozen).
#
# class Contact < ActiveRecord::Base
# after_destroy { |record| logger.info( "Contact #{record.id} was destroyed." ) }