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

Ticket #7377: callbacks_doc.diff

File callbacks_doc.diff, 6.4 kB (added by smeade, 1 year ago)

callbacks_doc.diff

  • activerecord/lib/active_record/callbacks.rb

    old new  
    55  # before or after an alteration of the object state. This can be used to make sure that associated and 
    66  # dependent objects are deleted when destroy is called (by overwriting before_destroy) or to massage attributes 
    77  # before they're validated (by overwriting before_validation). As an example of the callbacks initiated, consider 
    8   # the Base#save call
     8  # the Base#save call on a new record
    99  # 
    1010  # * (-) save 
    1111  # * (-) valid? 
     
    2121  # * (7) after_create 
    2222  # * (8) after_save 
    2323  # 
     24  # and the Base#save call on an existing record: 
     25  # 
     26  # * (-) save 
     27  # * (-) valid? 
     28  # * (1) before_validation 
     29  # * (2) before_validation_on_update 
     30  # * (-) validate 
     31  # * (-) validate_on_update 
     32  # * (3) after_validation 
     33  # * (4) after_validation_on_update 
     34  # * (5) before_save 
     35  # * (6) before_update 
     36  # * (-) update 
     37  # * (7) after_update 
     38  # * (8) after_save 
     39  # 
    2440  # That's a total of eight callbacks, which gives you immense power to react and prepare for each state in the 
    2541  # Active Record lifecycle. 
    2642  # 
     43  # Note: The destroy method has two callbacks 
     44  # * (-) destroy 
     45  # * (1) before_destroy 
     46  # * (-) delete operation 
     47  # * (2) after_destroy 
     48  # 
    2749  # Examples: 
    2850  #   class CreditCard < ActiveRecord::Base 
    2951  #     # Strip everything but digits, so the user can specify "555 234 34" or 
     
    169191  # If a before_* callback returns false, all the later callbacks and the associated action are cancelled. If an after_* callback returns 
    170192  # false, all the later callbacks are cancelled. Callbacks are generally run in the order they are defined, with the exception of callbacks 
    171193  # defined as methods on the model, which are called last. 
     194  # 
    172195  module Callbacks 
    173196    CALLBACKS = %w( 
    174197      after_find after_initialize before_save after_save before_create after_create before_update after_update before_validation 
     
    228251      result 
    229252    end 
    230253 
    231     # Is called _before_ Base.save (regardless of whether it's a create or update save). 
     254    # Is called _before_ a record is saved (regardless of whether it's a create or update save)  
     255    # 
     256    # (See Callbacks for the order in which callbacks are executed) 
    232257    def before_save() end 
    233258 
    234     # Is called _after_ Base.save (regardless of whether it's a create or update save). 
     259    # Is called _after_ a record is saved (regardless of whether it's a create or update save). 
    235260    # 
     261    # (See Callbacks for the order in which callbacks are executed) 
     262    # 
    236263    #  class Contact < ActiveRecord::Base 
    237264    #    after_save { logger.info( 'New contact saved!' ) } 
    238265    #  end 
     
    244271      result 
    245272    end 
    246273 
    247     # Is called _before_ Base.save on new objects that haven't been saved yet (no record exists). 
     274    # Is called _before_ a new record is created (no record exists)  
     275    # 
     276    # (See Callbacks for the order in which callbacks are executed) 
    248277    def before_create() end 
    249278 
    250     # Is called _after_ Base.save on new objects that haven't been saved yet (no record exists). 
     279    # Is called _after_ a new record is created for new objects that haven't been saved yet (no record exists). 
     280    # 
     281    # (See Callbacks for the order in which callbacks are executed) 
    251282    def after_create() end 
    252283    def create_with_callbacks #:nodoc: 
    253284      return false if callback(:before_create) == false 
     
    256287      result 
    257288    end 
    258289 
    259     # Is called _before_ Base.save on existing objects that have a record. 
     290    # Is called _before_ an existing record is updated  
     291    # 
     292    # (See Callbacks for the order in which callbacks are executed) 
    260293    def before_update() end 
    261294 
    262     # Is called _after_ Base.save on existing objects that have a record. 
     295    # Is called _after_ an existing record is updated    
     296    # 
     297    # (See Callbacks for the order in which callbacks are executed) 
    263298    def after_update() end 
    264299 
    265300    def update_with_callbacks #:nodoc: 
     
    270305    end 
    271306 
    272307    # Is called _before_ Validations.validate (which is part of the Base.save call). 
     308    # 
     309    # (See Callbacks for the order in which callbacks are executed)     
    273310    def before_validation() end 
    274311 
    275312    # Is called _after_ Validations.validate (which is part of the Base.save call). 
     313    # 
     314    # (See Callbacks for the order in which callbacks are executed) 
    276315    def after_validation() end 
    277316 
    278317    # Is called _before_ Validations.validate (which is part of the Base.save call) on new objects 
    279318    # that haven't been saved yet (no record exists). 
     319    # 
     320    # (See Callbacks for the order in which callbacks are executed) 
    280321    def before_validation_on_create() end 
    281322 
    282323    # Is called _after_ Validations.validate (which is part of the Base.save call) on new objects 
    283324    # that haven't been saved yet (no record exists). 
     325    # 
     326    # (See Callbacks for the order in which callbacks are executed) 
    284327    def after_validation_on_create()  end 
    285328 
    286329    # Is called _before_ Validations.validate (which is part of the Base.save call) on 
    287330    # existing objects that have a record. 
     331    # 
     332    # (See Callbacks for the order in which callbacks are executed) 
    288333    def before_validation_on_update() end 
    289334 
    290335    # Is called _after_ Validations.validate (which is part of the Base.save call) on 
    291336    # existing objects that have a record. 
     337    # 
     338    # (See Callbacks for the order in which callbacks are executed) 
    292339    def after_validation_on_update()  end 
    293340 
    294341    def valid_with_callbacks? #:nodoc: 
     
    308355    # 
    309356    # Note: If you need to _destroy_ or _nullify_ associated records first, 
    310357    # use the _:dependent_ option on your associations. 
     358    # Note that before_ and after_ validations are not run (i.e. there is no new info to validate) 
     359    # 
     360    # (See Callbacks for the order in which callbacks are executed) 
    311361    def before_destroy() end 
    312362 
    313363    # Is called _after_ Base.destroy (and all the attributes have been frozen). 
     
    315365    #  class Contact < ActiveRecord::Base 
    316366    #    after_destroy { |record| logger.info( "Contact #{record.id} was destroyed." ) } 
    317367    #  end 
     368    # 
     369    # (See Callbacks for the order in which callbacks are executed) 
    318370    def after_destroy()  end 
    319371    def destroy_with_callbacks #:nodoc: 
    320372      return false if callback(:before_destroy) == false