Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 8984

Show
Ignore:
Timestamp:
03/05/08 10:53:34 (2 months ago)
Author:
pratik
Message:

Docs for ActiveSupport::Callbacks. Closes #11254 [ernesto.jimenez]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/lib/active_support/callbacks.rb

    r8664 r8984  
    11module ActiveSupport 
     2  # Callbacks are hooks into the lifecycle of an object that allow you to trigger logic 
     3  # before or after an alteration of the object state. 
     4  # 
     5  # This mixing this module allos you to define callbacks in your class. 
     6  # 
     7  # Example: 
     8  #   class Storage 
     9  #     include ActiveSupport::Callbacks 
     10  # 
     11  #     define_callbacks :before_save, :after_save 
     12  #   end 
     13  # 
     14  #   class ConfigStorage < Storage 
     15  #     before_save :saving_message 
     16  #     def saving_message 
     17  #       puts "saving..." 
     18  #     end 
     19  # 
     20  #     after_save do |object| 
     21  #       puts "saved" 
     22  #     end 
     23  # 
     24  #     def save 
     25  #       run_callbacks(:before_save) 
     26  #       puts "- save" 
     27  #       run_callbacks(:after_save) 
     28  #     end 
     29  #   end 
     30  # 
     31  #   config = ConfigStorage.new 
     32  #   config.save 
     33  # 
     34  # Output: 
     35  #   saving... 
     36  #   - save 
     37  #   saved 
     38  # 
     39  # Callbacks from parent classes are inherited. 
     40  # 
     41  # Example: 
     42  #   class Storage 
     43  #     include ActiveSupport::Callbacks 
     44  # 
     45  #     define_callbacks :before_save, :after_save 
     46  # 
     47  #     before_save :prepare 
     48  #     def prepare 
     49  #       puts "preparing save" 
     50  #     end 
     51  #   end 
     52  # 
     53  #   class ConfigStorage < Storage 
     54  #     before_save :saving_message 
     55  #     def saving_message 
     56  #       puts "saving..." 
     57  #     end 
     58  # 
     59  #     after_save do |object| 
     60  #       puts "saved" 
     61  #     end 
     62  # 
     63  #     def save 
     64  #       run_callbacks(:before_save) 
     65  #       puts "- save" 
     66  #       run_callbacks(:after_save) 
     67  #     end 
     68  #   end 
     69  # 
     70  #   config = ConfigStorage.new 
     71  #   config.save 
     72  # 
     73  # Output: 
     74  #   preparing save 
     75  #   saving... 
     76  #   - save 
     77  #   saved 
    278  module Callbacks 
    379    class Callback 
     
    88164    end 
    89165 
     166    # Runs all the callbacks defined for the given options.  
     167    #  
     168    # If a block is given it will be called after each callback reciving as arguments: 
     169    # 
     170    #  * the result from the callback 
     171    #  * the object which has the callback 
     172    # 
     173    # If the result from the block evaluates as false, callback chain is stopped. 
     174    # 
     175    # Example: 
     176    #   class Storage 
     177    #     include ActiveSupport::Callbacks 
     178    #    
     179    #     define_callbacks :before_save, :after_save 
     180    #   end 
     181    #    
     182    #   class ConfigStorage < Storage 
     183    #     before_save :pass 
     184    #     before_save :pass 
     185    #     before_save :stop 
     186    #     before_save :pass 
     187    #    
     188    #     def pass 
     189    #       puts "pass" 
     190    #     end 
     191    #    
     192    #     def stop 
     193    #       puts "stop" 
     194    #       return false 
     195    #     end 
     196    #    
     197    #     def save 
     198    #       result = run_callbacks(:before_save) { |result, object| result == false } 
     199    #       puts "- save" if result 
     200    #     end 
     201    #   end 
     202    #    
     203    #   config = ConfigStorage.new 
     204    #   config.save 
     205    # 
     206    # Output: 
     207    #   pass 
     208    #   pass 
     209    #   stop 
    90210    def run_callbacks(kind, options = {}, &block) 
    91211      Callback.run(self.class.send("#{kind}_callback_chain"), self, options, &block)