From 55d70f3a9848f5c3cafe99f9d3553418e4b6045f Mon Sep 17 00:00:00 2001 From: CassioMarques Date: Tue, 25 Nov 2008 20:36:29 -0200 Subject: Added some text about callbacks classes --- .../html/activerecord_validations_callbacks.html | 50 +++++++++++++++++++++- .../source/activerecord_validations_callbacks.txt | 43 ++++++++++++++++++- 2 files changed, 90 insertions(+), 3 deletions(-) (limited to 'railties/doc/guides') diff --git a/railties/doc/guides/html/activerecord_validations_callbacks.html b/railties/doc/guides/html/activerecord_validations_callbacks.html index 1656cea492..097ad76d1e 100644 --- a/railties/doc/guides/html/activerecord_validations_callbacks.html +++ b/railties/doc/guides/html/activerecord_validations_callbacks.html @@ -301,7 +301,7 @@ ul#navMain { Halting Execution
  • - Callback classes and objects + Callback classes
  • Changelog @@ -1086,8 +1086,54 @@ Readability, since your callback declarations will live at the beggining of your

    As you start registering new callbacks for your models, they will be queued for execution. This queue will include all your model's validations, the registered callbacks and the database operation to be executed. However, if at any moment one of the callback methods returns a boolean false (not nil) value, this execution chain will be halted and the desired operation will not complete: your model will not get persisted in the database, or your records will not get deleted and so on.

    -

    11. Callback classes and objects

    +

    11. Callback classes

    +

    Sometimes the callback methods that you'll write will be useful enough to be reused at other models. Active Record makes it possible to create classes that encapsulate the callback methods, so it becomes very easy to reuse them.

    +

    Here's an example where we create a class with a after_destroy callback for a PictureFile model.

    +
    +
    +
    class PictureFileCallbacks
    +  def after_destroy(picture_file)
    +    File.delete(picture_file.filepath) if File.exists?(picture_file.filepath)
    +  end
    +end
    +
    +

    When declared inside a class the callback method will receive the model object as a parameter. We can now use it this way:

    +
    +
    +
    class PictureFile < ActiveRecord::Base
    +  after_destroy PictureFileCallbacks.new
    +end
    +
    +

    Note that we needed to instantiate a new PictureFileCallbacks object, since we declared our callback as an instance method. Sometimes it will make more sense to have it as a class method.

    +
    +
    +
    class PictureFileCallbacks
    +  def self.after_destroy(picture_file)
    +    File.delete(picture_file.filepath) if File.exists?(picture_file.filepath)
    +  end
    +end
    +
    +

    If the callback method is declared this way, it won't be necessary to instantiate a PictureFileCallbacks object.

    +
    +
    +
    class PictureFile < ActiveRecord::Base
    +  after_destroy PictureFileCallbacks
    +end
    +
    +

    You can declare as many callbacks as you want inside your callback classes.

    12. Changelog

    diff --git a/railties/doc/guides/source/activerecord_validations_callbacks.txt b/railties/doc/guides/source/activerecord_validations_callbacks.txt index 02cdbcf146..3db98027d2 100644 --- a/railties/doc/guides/source/activerecord_validations_callbacks.txt +++ b/railties/doc/guides/source/activerecord_validations_callbacks.txt @@ -589,10 +589,51 @@ The +after_initialize+ and +after_find+ callbacks are a bit different from the o As you start registering new callbacks for your models, they will be queued for execution. This queue will include all your model's validations, the registered callbacks and the database operation to be executed. However, if at any moment one of the callback methods returns a boolean +false+ (not +nil+) value, this execution chain will be halted and the desired operation will not complete: your model will not get persisted in the database, or your records will not get deleted and so on. +== Callback classes -== Callback classes and objects +Sometimes the callback methods that you'll write will be useful enough to be reused at other models. Active Record makes it possible to create classes that encapsulate the callback methods, so it becomes very easy to reuse them. +Here's an example where we create a class with a after_destroy callback for a PictureFile model. +[source, ruby] +------------------------------------------------------------------ +class PictureFileCallbacks + def after_destroy(picture_file) + File.delete(picture_file.filepath) if File.exists?(picture_file.filepath) + end +end +------------------------------------------------------------------ + +When declared inside a class the callback method will receive the model object as a parameter. We can now use it this way: + +[source, ruby] +------------------------------------------------------------------ +class PictureFile < ActiveRecord::Base + after_destroy PictureFileCallbacks.new +end +------------------------------------------------------------------ + +Note that we needed to instantiate a new PictureFileCallbacks object, since we declared our callback as an instance method. Sometimes it will make more sense to have it as a class method. + +[source, ruby] +------------------------------------------------------------------ +class PictureFileCallbacks + def self.after_destroy(picture_file) + File.delete(picture_file.filepath) if File.exists?(picture_file.filepath) + end +end +------------------------------------------------------------------ + +If the callback method is declared this way, it won't be necessary to instantiate a PictureFileCallbacks object. + +[source, ruby] +------------------------------------------------------------------ +class PictureFile < ActiveRecord::Base + after_destroy PictureFileCallbacks +end +------------------------------------------------------------------ + +You can declare as many callbacks as you want inside your callback classes. == Changelog -- cgit v1.2.3