Active Model 回调
为任何类提供一个接口,使其具有类似 Active Record 的回调。
与 Active Record 方法类似,只要某个方法抛出 :abort
,回调链就会中止。
首先,从你创建的类中扩展 ActiveModel::Callbacks
class MyModel
extend ActiveModel::Callbacks
end
然后定义一个方法列表,你想将回调附加到这些方法上
define_model_callbacks :create, :update
这将为 :create
和 :update
方法提供所有三个标准回调(before、around 和 after)。要实现,你需要将想要回调的方法包装在一个块中,以便回调有机会触发
def create
run_callbacks :create do
# Your create action methods here
end
end
然后在你的类中,你可以使用 before_create
、after_create
和 around_create
方法,就像在 Active Record 模型中一样。
before_create :action_before_create
def action_before_create
# Your code here
end
在定义 around 回调时,请记住要让出块,否则它将不会被执行
around_create :log_status
def log_status
puts 'going to call the block...'
yield
puts 'block successfully called.'
end
你可以选择仅通过将哈希传递给 define_model_callbacks
方法来获得特定的回调。
define_model_callbacks :create, only: [:after, :before]
只会创建 after_create
和 before_create
回调方法。
注意:多次调用同一个回调将覆盖之前的回调定义。
实例公共方法
define_model_callbacks(*callbacks) 链接
define_model_callbacks
接受与 define_callbacks
相同的选项,以防你想覆盖默认值。除此之外,它还接受一个 :only
选项,你可以选择是想要所有类型(before、around 或 after)还是仅选择一些。
define_model_callbacks :initialize, only: :after
请注意,only: <type>
哈希将应用于在该方法调用上定义的所有回调。为了解决这个问题,你可以根据需要多次调用 define_model_callbacks
方法。
define_model_callbacks :create, only: :after
define_model_callbacks :update, only: :before
define_model_callbacks :destroy, only: :around
只会创建 after_create
、before_update
和 around_destroy
方法。
你可以向 before_<type>、after_<type> 和 around_<type> 传递一个类,在这种情况下,回调将调用该类的 <action>_<type> 方法,并传递回调被调用的对象。
class MyModel
extend ActiveModel::Callbacks
define_model_callbacks :create
before_create AnotherClass
end
class AnotherClass
def self.before_create( obj )
# obj is the MyModel instance that the callback is being called on
end
end
注意:传递给 define_model_callbacks
的 method_name
不能以 !
、?
或 =
结尾。
来源:显示 | 在 GitHub 上
# File activemodel/lib/active_model/callbacks.rb, line 109 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