module ActiveModel::Callbacks
def define_model_callbacks(*callbacks)
NOTE: +method_name+ passed to +define_model_callbacks+ must not end with
end
end
# obj is the MyModel instance that the callback is being called on
def self.before_create( obj )
class AnotherClass
end
before_create AnotherClass
define_model_callbacks :create
extend ActiveModel::Callbacks
class MyModel
passing the object that the callback is being called on.
in which case the callback will call that class's
You can pass in a class to before_
only.
Would create +after_create+, +before_update+, and +around_destroy+ methods
define_model_callbacks :destroy, only: :around
define_model_callbacks :update, only: :before
define_model_callbacks :create, only: :after
method as many times as you need.
on that method call. To get around this you can call the +define_model_callbacks+
Note, the only:
define_model_callbacks :initialize, only: :after
around or after) or just some.
:only option, where you can choose if you want all types (before,
in case you want to overwrite a default. Besides that, it also accepts an
+define_model_callbacks+ accepts the same options +define_callbacks+ does,
def define_model_callbacks(*callbacks) options = callbacks.extract_options! options = { skip_after_callbacks_if_terminated: true, scope: [:kind, :name], only: [:before, :around, :after] }.merge!(options) types = Array(options.delete(:only)) callbacks.each do |callback| define_callbacks(callback, options) types.each do |type| send("_define_#{type}_model_callback", self, callback) end end end