module ActiveSupport::Callbacks::ClassMethods
def define_callbacks(*callbacks)
define_callbacks :save, scope: [:name]
A declaration like
which callbacks are being defined.
callback (before/after/around) and +:name+ refers to the method on
and +:name+ have special meanings: +:kind+ refers to the kind of
case "kind" is "before" and "name" is "save". In this context +:kind+
by calling #{kind}_#{name} on the given instance. In this
would trigger Audit#before_save instead. That's constructed
define_callbacks :save, scope: [:kind, :name]
Audit#before will be called. On the other hand
In the above case whenever you save an account the method
end
end
end
puts 'save in main'
run_callbacks :save do
def save
set_callback :save, :before, Audit.new
define_callbacks :save
include ActiveSupport::Callbacks
class Account
end
end
puts 'Audit: before_save is called'
def before_save(caller)
end
puts 'Audit: before is called'
def before(caller)
class Audit
object is used as a callback.
* :scope - Indicates which methods should be executed when an
option is specified.
terminated or not. Option makes sense only when :terminator
default after callbacks executed no matter if callback chain was
callbacks should be terminated by the :terminator option. By
* :skip_after_callbacks_if_terminated - Determines if after
halts the chain.
other callbacks are not executed. Defaults to +false+, meaning no value
In this example, if any before validate callbacks returns +false+,
define_callbacks :validate, terminator: 'result == false'
result of the callback is available in the +result+ variable.
the event from being triggered. This is a string to be eval'ed. The
callback chain, preventing following callbacks from being called and
* :terminator - Determines when a before filter will halt the
===== Options
define_callbacks :initialize, :save, :destroy
define_callbacks :validate
Define sets of events in the object life cycle that support callbacks.
def define_callbacks(*callbacks) config = callbacks.last.is_a?(Hash) ? callbacks.pop : {} callbacks.each do |callback| class_attribute "_#{callback}_callbacks" send("_#{callback}_callbacks=", CallbackChain.new(callback, config)) end end