2010-03-27 14:50:11 -04:00
|
|
|
require 'active_support/core_ext/array/wrap'
|
2009-12-28 19:15:39 -05:00
|
|
|
require 'active_support/callbacks'
|
|
|
|
|
|
|
|
module ActiveModel
|
2010-06-15 14:18:10 -04:00
|
|
|
# == Active Model Callbacks
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-14 16:19:53 -05:00
|
|
|
# Provides an interface for any class to have Active Record like callbacks.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-06-15 14:18:10 -04:00
|
|
|
# Like the Active Record methods, the callback chain is aborted as soon as
|
2010-01-14 16:19:53 -05:00
|
|
|
# one of the methods in the chain returns false.
|
|
|
|
#
|
|
|
|
# First, extend ActiveModel::Callbacks from the class you are creating:
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-14 16:19:53 -05:00
|
|
|
# class MyModel
|
|
|
|
# extend ActiveModel::Callbacks
|
|
|
|
# end
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-06-15 14:18:10 -04:00
|
|
|
# Then define a list of methods that you want callbacks attached to:
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-14 16:19:53 -05:00
|
|
|
# define_model_callbacks :create, :update
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-07-18 18:02:40 -04:00
|
|
|
# This will provide all three standard callbacks (before, around and after) for
|
2010-08-14 01:13:00 -04:00
|
|
|
# both the :create and :update methods. To implement, you need to wrap the methods
|
2010-06-15 14:18:10 -04:00
|
|
|
# you want callbacks on in a block so that the callbacks get a chance to fire:
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-14 16:19:53 -05:00
|
|
|
# def create
|
|
|
|
# _run_create_callbacks do
|
|
|
|
# # Your create action methods here
|
|
|
|
# end
|
|
|
|
# end
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-14 16:19:53 -05:00
|
|
|
# The _run_<method_name>_callbacks methods are dynamically created when you extend
|
|
|
|
# the <tt>ActiveModel::Callbacks</tt> module.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-14 16:19:53 -05:00
|
|
|
# Then in your class, you can use the +before_create+, +after_create+ and +around_create+
|
|
|
|
# methods, just as you would in an Active Record module.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-14 16:19:53 -05:00
|
|
|
# before_create :action_before_create
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-14 16:19:53 -05:00
|
|
|
# def action_before_create
|
|
|
|
# # Your code here
|
|
|
|
# end
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
|
|
|
# You can choose not to have all three callbacks by passing a hash to the
|
2010-01-14 16:19:53 -05:00
|
|
|
# define_model_callbacks method.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-14 16:19:53 -05:00
|
|
|
# define_model_callbacks :create, :only => :after, :before
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-14 16:19:53 -05:00
|
|
|
# Would only create the after_create and before_create callback methods in your
|
|
|
|
# class.
|
2009-12-28 19:15:39 -05:00
|
|
|
module Callbacks
|
|
|
|
def self.extended(base)
|
|
|
|
base.class_eval do
|
|
|
|
include ActiveSupport::Callbacks
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-14 04:58:00 -04:00
|
|
|
# define_model_callbacks accepts the same options define_callbacks does, in case
|
2010-08-14 01:13:00 -04:00
|
|
|
# you want to overwrite a default. Besides that, it also accepts an :only option,
|
2010-01-14 16:19:53 -05:00
|
|
|
# where you can choose if you want all types (before, around or after) or just some.
|
2009-12-28 19:15:39 -05:00
|
|
|
#
|
2010-01-14 16:19:53 -05:00
|
|
|
# define_model_callbacks :initializer, :only => :after
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-06-15 14:18:10 -04:00
|
|
|
# Note, the <tt>:only => <type></tt> hash will apply to all callbacks defined on
|
2010-01-14 16:19:53 -05:00
|
|
|
# that method call. To get around this you can call the define_model_callbacks
|
|
|
|
# method as many times as you need.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-14 16:19:53 -05:00
|
|
|
# define_model_callbacks :create, :only => :after
|
|
|
|
# define_model_callbacks :update, :only => :before
|
|
|
|
# define_model_callbacks :destroy, :only => :around
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-14 16:19:53 -05:00
|
|
|
# Would create +after_create+, +before_update+ and +around_destroy+ methods only.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-14 16:19:53 -05:00
|
|
|
# You can pass in a class to before_<type>, after_<type> and around_<type>, in which
|
2010-06-15 14:18:10 -04:00
|
|
|
# case the callback will call that class's <action>_<type> method passing the object
|
|
|
|
# that the callback is being called on.
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2009-12-28 19:15:39 -05:00
|
|
|
# class MyModel
|
2010-01-14 16:19:53 -05:00
|
|
|
# extend ActiveModel::Callbacks
|
2009-12-28 19:15:39 -05:00
|
|
|
# define_model_callbacks :create
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-14 16:19:53 -05:00
|
|
|
# before_create AnotherClass
|
2009-12-28 19:15:39 -05:00
|
|
|
# end
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2010-01-14 16:19:53 -05:00
|
|
|
# class AnotherClass
|
|
|
|
# def self.before_create( obj )
|
2010-06-15 14:18:10 -04:00
|
|
|
# # obj is the MyModel instance that the callback is being called on
|
2009-12-28 19:15:39 -05:00
|
|
|
# end
|
|
|
|
# end
|
2010-08-14 01:13:00 -04:00
|
|
|
#
|
2009-12-28 19:15:39 -05:00
|
|
|
def define_model_callbacks(*callbacks)
|
|
|
|
options = callbacks.extract_options!
|
2010-08-14 01:13:00 -04:00
|
|
|
options = {
|
|
|
|
:terminator => "result == false",
|
|
|
|
:scope => [:kind, :name],
|
|
|
|
:only => [:before, :around, :after]
|
2010-08-03 17:04:41 -04:00
|
|
|
}.merge(options)
|
2009-12-28 19:15:39 -05:00
|
|
|
|
2010-08-03 17:04:41 -04:00
|
|
|
types = Array.wrap(options.delete(:only))
|
2009-12-28 19:15:39 -05:00
|
|
|
|
|
|
|
callbacks.each do |callback|
|
|
|
|
define_callbacks(callback, options)
|
|
|
|
|
|
|
|
types.each do |type|
|
|
|
|
send(:"_define_#{type}_model_callback", self, callback)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def _define_before_model_callback(klass, callback) #:nodoc:
|
2010-04-09 14:56:50 -04:00
|
|
|
klass.class_eval <<-CALLBACK, __FILE__, __LINE__ + 1
|
2009-12-28 19:15:39 -05:00
|
|
|
def self.before_#{callback}(*args, &block)
|
|
|
|
set_callback(:#{callback}, :before, *args, &block)
|
|
|
|
end
|
|
|
|
CALLBACK
|
|
|
|
end
|
|
|
|
|
|
|
|
def _define_around_model_callback(klass, callback) #:nodoc:
|
2010-04-09 14:56:50 -04:00
|
|
|
klass.class_eval <<-CALLBACK, __FILE__, __LINE__ + 1
|
2009-12-28 19:15:39 -05:00
|
|
|
def self.around_#{callback}(*args, &block)
|
|
|
|
set_callback(:#{callback}, :around, *args, &block)
|
|
|
|
end
|
|
|
|
CALLBACK
|
|
|
|
end
|
|
|
|
|
|
|
|
def _define_after_model_callback(klass, callback) #:nodoc:
|
2010-04-09 14:56:50 -04:00
|
|
|
klass.class_eval <<-CALLBACK, __FILE__, __LINE__ + 1
|
2009-12-28 19:15:39 -05:00
|
|
|
def self.after_#{callback}(*args, &block)
|
|
|
|
options = args.extract_options!
|
|
|
|
options[:prepend] = true
|
2010-03-27 14:50:11 -04:00
|
|
|
options[:if] = Array.wrap(options[:if]) << "!halted && value != false"
|
2009-12-28 19:15:39 -05:00
|
|
|
set_callback(:#{callback}, :after, *(args << options), &block)
|
|
|
|
end
|
|
|
|
CALLBACK
|
|
|
|
end
|
|
|
|
end
|
2010-03-27 14:50:11 -04:00
|
|
|
end
|