method

set_callback

Importance_2
v3.0.9 - Show latest stable - 0 notes - Class: ActiveSupport::Callbacks::ClassMethods
set_callback(name, *filter_list, &block) public

Set callbacks for a previously defined callback.

Syntax:

set_callback :save, :before, :before_meth
set_callback :save, :after,  :after_meth, :if => :condition
set_callback :save, :around, lambda { |r| stuff; yield; stuff }

Use skip_callback to skip any defined one.

When creating or skipping callbacks, you can specify conditions that are always the same for a given key. For instance, in Action Pack, we convert :only and :except conditions into per-key conditions.

before_filter :authenticate, :except => "index"

becomes

dispatch_callback :before, :authenticate, :per_key => {:unless => proc {|c| c.action_name == "index"}}

Per-Key conditions are evaluated only once per use of a given key. In the case of the above example, you would do:

run_callbacks(:dispatch, action_name) { ... dispatch stuff ... }

In that case, each action_name would get its own compiled callback method that took into consideration the per_key conditions. This is a speed improvement for ActionPack.

Show source
Register or log in to add new notes.