Ticket #7377: callbacks_doc.diff
| File callbacks_doc.diff, 6.4 kB (added by smeade, 1 year ago) |
|---|
-
activerecord/lib/active_record/callbacks.rb
old new 5 5 # before or after an alteration of the object state. This can be used to make sure that associated and 6 6 # dependent objects are deleted when destroy is called (by overwriting before_destroy) or to massage attributes 7 7 # 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: 9 9 # 10 10 # * (-) save 11 11 # * (-) valid? … … 21 21 # * (7) after_create 22 22 # * (8) after_save 23 23 # 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 # 24 40 # That's a total of eight callbacks, which gives you immense power to react and prepare for each state in the 25 41 # Active Record lifecycle. 26 42 # 43 # Note: The destroy method has two callbacks 44 # * (-) destroy 45 # * (1) before_destroy 46 # * (-) delete operation 47 # * (2) after_destroy 48 # 27 49 # Examples: 28 50 # class CreditCard < ActiveRecord::Base 29 51 # # Strip everything but digits, so the user can specify "555 234 34" or … … 169 191 # If a before_* callback returns false, all the later callbacks and the associated action are cancelled. If an after_* callback returns 170 192 # false, all the later callbacks are cancelled. Callbacks are generally run in the order they are defined, with the exception of callbacks 171 193 # defined as methods on the model, which are called last. 194 # 172 195 module Callbacks 173 196 CALLBACKS = %w( 174 197 after_find after_initialize before_save after_save before_create after_create before_update after_update before_validation … … 228 251 result 229 252 end 230 253 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) 232 257 def before_save() end 233 258 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). 235 260 # 261 # (See Callbacks for the order in which callbacks are executed) 262 # 236 263 # class Contact < ActiveRecord::Base 237 264 # after_save { logger.info( 'New contact saved!' ) } 238 265 # end … … 244 271 result 245 272 end 246 273 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) 248 277 def before_create() end 249 278 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) 251 282 def after_create() end 252 283 def create_with_callbacks #:nodoc: 253 284 return false if callback(:before_create) == false … … 256 287 result 257 288 end 258 289 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) 260 293 def before_update() end 261 294 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) 263 298 def after_update() end 264 299 265 300 def update_with_callbacks #:nodoc: … … 270 305 end 271 306 272 307 # 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) 273 310 def before_validation() end 274 311 275 312 # 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) 276 315 def after_validation() end 277 316 278 317 # Is called _before_ Validations.validate (which is part of the Base.save call) on new objects 279 318 # that haven't been saved yet (no record exists). 319 # 320 # (See Callbacks for the order in which callbacks are executed) 280 321 def before_validation_on_create() end 281 322 282 323 # Is called _after_ Validations.validate (which is part of the Base.save call) on new objects 283 324 # that haven't been saved yet (no record exists). 325 # 326 # (See Callbacks for the order in which callbacks are executed) 284 327 def after_validation_on_create() end 285 328 286 329 # Is called _before_ Validations.validate (which is part of the Base.save call) on 287 330 # existing objects that have a record. 331 # 332 # (See Callbacks for the order in which callbacks are executed) 288 333 def before_validation_on_update() end 289 334 290 335 # Is called _after_ Validations.validate (which is part of the Base.save call) on 291 336 # existing objects that have a record. 337 # 338 # (See Callbacks for the order in which callbacks are executed) 292 339 def after_validation_on_update() end 293 340 294 341 def valid_with_callbacks? #:nodoc: … … 308 355 # 309 356 # Note: If you need to _destroy_ or _nullify_ associated records first, 310 357 # 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) 311 361 def before_destroy() end 312 362 313 363 # Is called _after_ Base.destroy (and all the attributes have been frozen). … … 315 365 # class Contact < ActiveRecord::Base 316 366 # after_destroy { |record| logger.info( "Contact #{record.id} was destroyed." ) } 317 367 # end 368 # 369 # (See Callbacks for the order in which callbacks are executed) 318 370 def after_destroy() end 319 371 def destroy_with_callbacks #:nodoc: 320 372 return false if callback(:before_destroy) == false