|
3 | 3 | require "active_support/core_ext/array/wrap" |
4 | 4 |
|
5 | 5 | module ActiveResource |
| 6 | + # = Active Resource \Callbacks |
| 7 | + # |
| 8 | + # \Callbacks are hooks into the life cycle of an Active Resource object that allow you to trigger logic |
| 9 | + # before or after a change in the object state. Active Resources instances trigger callbacks for the following methods: |
| 10 | + # |
| 11 | + # * <tt>save</tt> (<tt>around_save</tt>, <tt>before_save</tt>, <tt>after_save</tt>) |
| 12 | + # * <tt>create</tt> (<tt>around_create</tt>, <tt>before_create</tt>, <tt>after_create</tt>) |
| 13 | + # * <tt>update</tt> (<tt>around_update</tt>, <tt>before_update</tt>, <tt>after_update</tt>) |
| 14 | + # * <tt>destroy</tt> (<tt>around_destroy</tt>, <tt>before_destroy</tt>, <tt>after_destroy</tt>) |
| 15 | + # * <tt>reload</tt> (<tt>around_reload</tt>, <tt>before_reload</tt>, <tt>after_reload</tt>) |
| 16 | + # |
| 17 | + # As an example of the callbacks initiated, consider the {ActiveResource::Base#save}[rdoc-ref:Base#save] call for a new resource: |
| 18 | + # |
| 19 | + # * (-) <tt>save</tt> |
| 20 | + # * (-) <tt>valid?</tt> |
| 21 | + # * (1) <tt>before_validation</tt> |
| 22 | + # * (-) <tt>validate</tt> |
| 23 | + # * (2) <tt>after_validation</tt> |
| 24 | + # * (3) <tt>before_save</tt> |
| 25 | + # * (4) <tt>before_create</tt> |
| 26 | + # * (-) <tt>create</tt> |
| 27 | + # * (5) <tt>after_create</tt> |
| 28 | + # * (6) <tt>after_save</tt> |
| 29 | + # |
| 30 | + # == Canceling callbacks |
| 31 | + # |
| 32 | + # If a <tt>before_*</tt> callback throws +:abort+, all the later callbacks and |
| 33 | + # the associated action are cancelled. |
| 34 | + # \Callbacks are generally run in the order they are defined, with the exception of callbacks defined as |
| 35 | + # methods on the model, which are called last. |
| 36 | + # |
| 37 | + # == Debugging callbacks |
| 38 | + # |
| 39 | + # The callback chain is accessible via the <tt>_*_callbacks</tt> method on an object. Active Model \Callbacks support |
| 40 | + # <tt>:before</tt>, <tt>:after</tt> and <tt>:around</tt> as values for the <tt>kind</tt> property. The <tt>kind</tt> property |
| 41 | + # defines what part of the chain the callback runs in. |
| 42 | + # |
| 43 | + # To find all callbacks in the +before_save+ callback chain: |
| 44 | + # |
| 45 | + # Topic._save_callbacks.select { |cb| cb.kind.eql?(:before) } |
| 46 | + # |
| 47 | + # Returns an array of callback objects that form the +before_save+ chain. |
| 48 | + # |
| 49 | + # To further check if the before_save chain contains a proc defined as <tt>rest_when_dead</tt> use the <tt>filter</tt> property of the callback object: |
| 50 | + # |
| 51 | + # Topic._save_callbacks.select { |cb| cb.kind.eql?(:before) }.collect(&:filter).include?(:rest_when_dead) |
| 52 | + # |
| 53 | + # Returns true or false depending on whether the proc is contained in the +before_save+ callback chain on a Topic model. |
6 | 54 | module Callbacks |
7 | 55 | extend ActiveSupport::Concern |
8 | 56 |
|
9 | 57 | CALLBACKS = [ |
10 | 58 | :before_validation, :after_validation, :before_save, :around_save, :after_save, |
11 | 59 | :before_create, :around_create, :after_create, :before_update, :around_update, |
12 | | - :after_update, :before_destroy, :around_destroy, :after_destroy |
| 60 | + :after_update, :before_destroy, :around_destroy, :after_destroy, |
| 61 | + :before_reload, :around_reload, :after_reload |
13 | 62 | ] |
14 | 63 |
|
15 | 64 | included do |
16 | 65 | extend ActiveModel::Callbacks |
17 | 66 | include ActiveModel::Validations::Callbacks |
18 | 67 |
|
19 | | - define_model_callbacks :save, :create, :update, :destroy |
| 68 | + define_model_callbacks :save, :create, :update, :destroy, :reload |
20 | 69 | end |
21 | 70 | end |
22 | 71 | end |
0 commit comments