Skip to content

Commit

Permalink
edit pass in #define_callbacks rdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
fxn committed Jun 14, 2010
1 parent e4c8bc1 commit 94de5b8
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions activesupport/lib/active_support/callbacks.rb
Expand Up @@ -449,7 +449,9 @@ def __update_callbacks(name, filters = [], block = nil) #:nodoc:
# we convert :only and :except conditions into per-key conditions. # we convert :only and :except conditions into per-key conditions.
# #
# before_filter :authenticate, :except => "index" # before_filter :authenticate, :except => "index"
#
# becomes # becomes
#
# dispatch_callback :before, :authenticate, :per_key => {:unless => proc {|c| c.action_name == "index"}} # 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. # Per-Key conditions are evaluated only once per use of a given key.
Expand Down Expand Up @@ -510,65 +512,65 @@ def reset_callbacks(symbol)
__define_runner(symbol) __define_runner(symbol)
end end


# Define callbacks types. # Defines callbacks types:
#
# ==== Example
# #
# define_callbacks :validate # define_callbacks :validate
# #
# ==== Options # This macro accepts the following options:
# #
# * <tt>:terminator</tt> - Indicates when a before filter is considered # * <tt>:terminator</tt> - Indicates when a before filter is considered
# to be halted. # to be halted.
# #
# define_callbacks :validate, :terminator => "result == false" # define_callbacks :validate, :terminator => "result == false"
# #
# In the example above, if any before validate callbacks returns false, # In the example above, if any before validate callbacks returns +false+,
# other callbacks are not executed. Defaults to "false". # other callbacks are not executed. Defaults to "false", meaning no value
# halts the chain.
# #
# * <tt>:rescuable</tt> - By default, after filters are not executed if # * <tt>:rescuable</tt> - By default, after filters are not executed if
# the given block or an before_filter raises an error. Supply :rescuable => true # the given block or a before filter raises an error. Set this option to
# to change this behavior. # true to change this behavior.
# #
# * <tt>:scope</tt> - Indicates which methods should be executed when a class # * <tt>:scope</tt> - Indicates which methods should be executed when a class
# is given as callback: # is given as callback. Defaults to <tt>[:kind]</tt>.
# #
# class Audit # class Audit
# def before(caller) # def before(caller)
# puts 'Audit: before is called' # puts 'Audit: before is called'
# end # end
#
# def before_save(caller) # def before_save(caller)
# puts 'Audit: before_save is called' # puts 'Audit: before_save is called'
# end # end
# end # end
# #
# class Account # class Account
# include ActiveSupport::Callbacks # include ActiveSupport::Callbacks
#
# define_callbacks :save # define_callbacks :save
# set_callback :save, :before, Audit.new # set_callback :save, :before, Audit.new
#
# def save # def save
# run_callbacks :save do # run_callbacks :save do
# puts 'save in main' # puts 'save in main'
# end # end
# end # end
# end # end
# #
# In the above case if you execute Account.new.save then method "before" of Audit class # In the above case whenever you save an account the method <tt>Audit#before</tt> will
# will be called. Now change the class "Account" and pass "scope" to method "define_callbacks". # 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 # would trigger <tt>Audit#before_save</tt> instead. That's constructed by calling
# instead of method "before". # <tt>"#{kind}_#{name}"</tt> 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 # A declaration like
# 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".
# #
# Although ":kind" is the default scope that is passed, it is possible to not to make use of ":kind". # define_callbacks :save, :scope => [:name]
# define_callbacks :save, :scope => [:name] . A declaration like this would call "save" method of
# Audit class since ":kind" is skipped.
# #
# would call <tt>Audit#save</tt>.
# #
def define_callbacks(*callbacks) def define_callbacks(*callbacks)
config = callbacks.last.is_a?(Hash) ? callbacks.pop : {} config = callbacks.last.is_a?(Hash) ? callbacks.pop : {}
Expand Down

2 comments on commit 94de5b8

@neerajsingh0101
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Xavier thanks for editing. You have certainly mastered the art of brevity. Nicely edited. Thank you.

@fxn
Copy link
Member Author

@fxn fxn commented on 94de5b8 Jun 14, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No prob! Thanks for contributing :).

Please sign in to comment.