From 94de5b8cb58a5b0e1b0493932ec8b6bdf2b8f6dc Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Mon, 14 Jun 2010 22:52:47 +0200 Subject: edit pass in #define_callbacks rdoc --- activesupport/lib/active_support/callbacks.rb | 42 ++++++++++++++------------- 1 file changed, 22 insertions(+), 20 deletions(-) (limited to 'activesupport/lib/active_support/callbacks.rb') diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 92a4635011..8d71be1a4e 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -449,7 +449,9 @@ module ActiveSupport # 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. @@ -510,33 +512,33 @@ module ActiveSupport __define_runner(symbol) end - # Define callbacks types. - # - # ==== Example + # Defines callbacks types: # # define_callbacks :validate # - # ==== Options + # This macro accepts the following options: # # * :terminator - Indicates when a before filter is considered # to be halted. # # define_callbacks :validate, :terminator => "result == false" # - # In the example above, if any before validate callbacks returns false, - # other callbacks are not executed. Defaults to "false". + # In the example above, if any before validate callbacks returns +false+, + # other callbacks are not executed. Defaults to "false", meaning no value + # halts the chain. # # * :rescuable - By default, after filters are not executed if - # the given block or an before_filter raises an error. Supply :rescuable => true - # to change this behavior. + # the given block or a before filter raises an error. Set this option to + # true to change this behavior. # # * :scope - Indicates which methods should be executed when a class - # is given as callback: + # is given as callback. Defaults to [:kind]. # # class Audit # def before(caller) # puts 'Audit: before is called' # end + # # def before_save(caller) # puts 'Audit: before_save is called' # end @@ -544,8 +546,10 @@ module ActiveSupport # # class Account # include ActiveSupport::Callbacks + # # define_callbacks :save # set_callback :save, :before, Audit.new + # # def save # run_callbacks :save do # puts 'save in main' @@ -553,22 +557,20 @@ module ActiveSupport # end # end # - # In the above case if you execute Account.new.save then method "before" of Audit class - # will be called. Now change the class "Account" and pass "scope" to method "define_callbacks". + # In the above case whenever you save an account the method Audit#before will + # be called. On the other hand # - # define_callbacks :save, :scope => [:kind, :name] + # define_callbacks :save, :scope => [:kind, :name] # - # Now if you invoke Account.new.save then method "before_save" of Audit will be called - # instead of method "before". + # would trigger Audit#before_save instead. That's constructed by calling + # "#{kind}_#{name}" on the given instance. In this case "kind" is "before" and + # "name" is "save". # - # When you do not pass any scope then the default value of scope ":kind" is implicitly being - # passed. In the above case method "before_save" is constructed by calling "#{kind}_#{name}" - # in the given class. In this case "kind" is "before" and "name" is "save". + # A declaration like # - # Although ":kind" is the default scope that is passed, it is possible to not to make use of ":kind". - # define_callbacks :save, :scope => [:name] . A declaration like this would call "save" method of - # Audit class since ":kind" is skipped. + # define_callbacks :save, :scope => [:name] # + # would call Audit#save. # def define_callbacks(*callbacks) config = callbacks.last.is_a?(Hash) ? callbacks.pop : {} -- cgit v1.2.3