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

Ticket #11254: doc_for_active_support_callbacks.diff

File doc_for_active_support_callbacks.diff, 3.3 kB (added by ernesto.jimenez, 7 months ago)
  • activesupport/lib/active_support/callbacks.rb

    old new  
    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 
    480      def self.run(callbacks, object, options = {}, &terminator) 
     
    87163      end 
    88164    end 
    89165 
     166    # Calls all the _kind_ callbacks with the given options. If a block is given 
     167    # it will be called after each callback reciving as arguments: 
     168    # 
     169    #  * the result from the callback 
     170    #  * the object which has the callback 
     171    # 
     172    # If the result from the block evaluates as true, callback chain is stopped. 
     173    # 
     174    # Example: 
     175    #   class Storage 
     176    #     include ActiveSupport::Callbacks 
     177    #    
     178    #     define_callbacks :before_save, :after_save 
     179    #   end 
     180    #    
     181    #   class ConfigStorage < Storage 
     182    #     before_save :pass 
     183    #     before_save :pass 
     184    #     before_save :stop 
     185    #     before_save :pass 
     186    #    
     187    #     def pass 
     188    #       puts "pass" 
     189    #     end 
     190    #    
     191    #     def stop 
     192    #       puts "stop" 
     193    #       return false 
     194    #     end 
     195    #    
     196    #     def save 
     197    #       result = run_callbacks(:before_save) { |result, object| result == false } 
     198    #       puts "- save" if result 
     199    #     end 
     200    #   end 
     201    #    
     202    #   config = ConfigStorage.new 
     203    #   config.save 
     204    # 
     205    # Output: 
     206    #   pass 
     207    #   pass 
     208    #   stop 
    90209    def run_callbacks(kind, options = {}, &block) 
    91210      Callback.run(self.class.send("#{kind}_callback_chain"), self, options, &block) 
    92211    end