Skip to content

Commit a91e3c7

Browse files
committed
Clarify documentation on ActiveRecord Callbacks
Mainly these changes aim to make this documentation a bit more ESL (English as a second language friendly). Here are some examples of the things I changed: - replace the constant use of `get <verb>` with `is <verb>` - replace $10 words like `alteration` with clearer $1 words like `change` - `give you immense power` -> `offer you a lot of control` - missing `<code>` wrapping to denote method/variable references and SQL statements like `COMMIT` and `ROLLBACK`. - clarify the ordering callback example specifically to explain what children is (an association with records) This is minimal stuff but I think it improves the clarity and legibility of this extremely important piece of documentation. I'm actually surprised that it's not linked to from the [Callbacks Guide][1] [ci skip] [1]: https://guides.rubyonrails.org/active_record_callbacks.html
1 parent c70112e commit a91e3c7

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

activerecord/lib/active_record/callbacks.rb

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module ActiveRecord
44
# = Active Record \Callbacks
55
#
66
# \Callbacks are hooks into the life cycle of an Active Record object that allow you to trigger logic
7-
# before or after an alteration of the object state. This can be used to make sure that associated and
7+
# before or after a change in the object state. This can be used to make sure that associated and
88
# dependent objects are deleted when {ActiveRecord::Base#destroy}[rdoc-ref:Persistence#destroy] is called (by overwriting +before_destroy+) or
99
# to massage attributes before they're validated (by overwriting +before_validation+).
1010
# As an example of the callbacks initiated, consider the {ActiveRecord::Base#save}[rdoc-ref:Persistence#save] call for a new record:
@@ -21,8 +21,8 @@ module ActiveRecord
2121
# * (6) <tt>after_save</tt>
2222
# * (7) <tt>after_commit</tt>
2323
#
24-
# Also, an <tt>after_rollback</tt> callback can be configured to be triggered whenever a rollback is issued.
2524
# Check out ActiveRecord::Transactions for more details about <tt>after_commit</tt> and
25+
# Also, an <tt>after_rollback</tt> callback can be configured to be triggered whenever a rollback is issued.
2626
# <tt>after_rollback</tt>.
2727
#
2828
# Additionally, an <tt>after_touch</tt> callback is triggered whenever an
@@ -32,7 +32,7 @@ module ActiveRecord
3232
# is found and instantiated by a finder, with <tt>after_initialize</tt> being triggered after new objects
3333
# are instantiated as well.
3434
#
35-
# There are nineteen callbacks in total, which give you immense power to react and prepare for each state in the
35+
# There are nineteen callbacks in total, which give a lot of control over how to react and prepare for each state in the
3636
# Active Record life cycle. The sequence for calling {ActiveRecord::Base#save}[rdoc-ref:Persistence#save] for an existing record is similar,
3737
# except that each <tt>_create</tt> callback is replaced by the corresponding <tt>_update</tt> callback.
3838
#
@@ -64,7 +64,7 @@ module ActiveRecord
6464
#
6565
# Besides the overwritable callback methods, it's also possible to register callbacks through the
6666
# use of the callback macros. Their main advantage is that the macros add behavior into a callback
67-
# queue that is kept intact down through an inheritance hierarchy.
67+
# queue that is kept intact through an inheritance hierarchy.
6868
#
6969
# class Topic < ActiveRecord::Base
7070
# before_destroy :destroy_author
@@ -74,7 +74,7 @@ module ActiveRecord
7474
# before_destroy :destroy_readers
7575
# end
7676
#
77-
# Now, when <tt>Topic#destroy</tt> is run only +destroy_author+ is called. When <tt>Reply#destroy</tt> is
77+
# When <tt>Topic#destroy</tt> is run only +destroy_author+ is called. When <tt>Reply#destroy</tt> is
7878
# run, both +destroy_author+ and +destroy_readers+ are called.
7979
#
8080
# *IMPORTANT:* In order for inheritance to work for the callback queues, you must specify the
@@ -83,10 +83,9 @@ module ActiveRecord
8383
#
8484
# == Types of callbacks
8585
#
86-
# There are three types of callbacks accepted by the callback macros: Method references (symbol), callback objects,
87-
# and inline methods (using a proc). Method references and callback objects
88-
# are the recommended approaches, inline methods using a proc are sometimes appropriate (such as for
89-
# creating mix-ins).
86+
# There are four types of callbacks accepted by the callback macros: method references (symbol), callback objects,
87+
# inline methods (using a proc). Method references and callback objects are the recommended approaches,
88+
# inline methods using a proc are sometimes appropriate (such as for creating mix-ins).
9089
#
9190
# The method reference callbacks work by specifying a protected or private method available in the object, like this:
9291
#
@@ -179,8 +178,8 @@ module ActiveRecord
179178
#
180179
# == Ordering callbacks
181180
#
182-
# Sometimes the code needs that the callbacks execute in a specific order. For example, a +before_destroy+
183-
# callback (+log_children+ in this case) should be executed before the children get destroyed by the
181+
# Sometimes application code requires that callbacks execute in a specific order. For example, a +before_destroy+
182+
# callback (+log_children+ in this case) should be executed before records in the +children+ association are destroyed by the
184183
# <tt>dependent: :destroy</tt> option.
185184
#
186185
# Let's look at the code below:
@@ -196,8 +195,8 @@ module ActiveRecord
196195
# end
197196
# end
198197
#
199-
# In this case, the problem is that when the +before_destroy+ callback is executed, the children are not available
200-
# because the {ActiveRecord::Base#destroy}[rdoc-ref:Persistence#destroy] callback gets executed first.
198+
# In this case, the problem is that when the +before_destroy+ callback is executed, records in the +children+ association no
199+
# longer exist because the {ActiveRecord::Base#destroy}[rdoc-ref:Persistence#destroy] callback was executed first.
201200
# You can use the +prepend+ option on the +before_destroy+ callback to avoid this.
202201
#
203202
# class Topic < ActiveRecord::Base
@@ -211,7 +210,7 @@ module ActiveRecord
211210
# end
212211
# end
213212
#
214-
# This way, the +before_destroy+ gets executed before the <tt>dependent: :destroy</tt> is called, and the data is still available.
213+
# This way, the +before_destroy+ is executed before the <tt>dependent: :destroy</tt> is called, and the data is still available.
215214
#
216215
# Also, there are cases when you want several callbacks of the same type to
217216
# be executed in order.
@@ -235,10 +234,10 @@ module ActiveRecord
235234
# end
236235
# end
237236
#
238-
# In this case the +log_children+ gets executed before +do_something_else+.
237+
# In this case the +log_children+ is executed before +do_something_else+.
239238
# The same applies to all non-transactional callbacks.
240239
#
241-
# In case there are multiple transactional callbacks as seen below, the order
240+
# As seen below, in case there are multiple transactional callbacks the order
242241
# is reversed.
243242
#
244243
# For example:
@@ -260,16 +259,16 @@ module ActiveRecord
260259
# end
261260
# end
262261
#
263-
# In this case the +do_something_else+ gets executed before +log_children+.
262+
# In this case the +do_something_else+ is executed before +log_children+.
264263
#
265264
# == \Transactions
266265
#
267266
# The entire callback chain of a {#save}[rdoc-ref:Persistence#save], {#save!}[rdoc-ref:Persistence#save!],
268267
# or {#destroy}[rdoc-ref:Persistence#destroy] call runs within a transaction. That includes <tt>after_*</tt> hooks.
269-
# If everything goes fine a COMMIT is executed once the chain has been completed.
268+
# If everything goes fine a +COMMIT+ is executed once the chain has been completed.
270269
#
271-
# If a <tt>before_*</tt> callback cancels the action a ROLLBACK is issued. You
272-
# can also trigger a ROLLBACK raising an exception in any of the callbacks,
270+
# If a <tt>before_*</tt> callback cancels the action a +ROLLBACK+ is issued. You
271+
# can also trigger a +ROLLBACK+ raising an exception in any of the callbacks,
273272
# including <tt>after_*</tt> hooks. Note, however, that in that case the client
274273
# needs to be aware of it because an ordinary {#save}[rdoc-ref:Persistence#save] will raise such exception
275274
# instead of quietly returning +false+.
@@ -280,17 +279,17 @@ module ActiveRecord
280279
# <tt>:before</tt>, <tt>:after</tt> and <tt>:around</tt> as values for the <tt>kind</tt> property. The <tt>kind</tt> property
281280
# defines what part of the chain the callback runs in.
282281
#
283-
# To find all callbacks in the before_save callback chain:
282+
# To find all callbacks in the +before_save+ callback chain:
284283
#
285284
# Topic._save_callbacks.select { |cb| cb.kind.eql?(:before) }
286285
#
287-
# Returns an array of callback objects that form the before_save chain.
286+
# Returns an array of callback objects that form the +before_save+ chain.
288287
#
289288
# 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:
290289
#
291290
# Topic._save_callbacks.select { |cb| cb.kind.eql?(:before) }.collect(&:filter).include?(:rest_when_dead)
292291
#
293-
# Returns true or false depending on whether the proc is contained in the before_save callback chain on a Topic model.
292+
# Returns true or false depending on whether the proc is contained in the +before_save+ callback chain on a Topic model.
294293
#
295294
module Callbacks
296295
extend ActiveSupport::Concern

0 commit comments

Comments
 (0)