mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Improve documentation of Procs as :if / :unless options for callbacks
This commit is contained in:
parent
103b02f152
commit
ec6089995d
2 changed files with 16 additions and 0 deletions
|
@ -657,9 +657,17 @@ module ActiveSupport
|
|||
# * <tt>:if</tt> - A symbol or an array of symbols, each naming an instance
|
||||
# method or a proc; the callback will be called only when they all return
|
||||
# a true value.
|
||||
#
|
||||
# If a proc is given, its body is evaluated in the context of the
|
||||
# current object. It can also optionally accept the current object as
|
||||
# an argument.
|
||||
# * <tt>:unless</tt> - A symbol or an array of symbols, each naming an
|
||||
# instance method or a proc; the callback will be called only when they
|
||||
# all return a false value.
|
||||
#
|
||||
# If a proc is given, its body is evaluated in the context of the
|
||||
# current object. It can also optionally accept the current object as
|
||||
# an argument.
|
||||
# * <tt>:prepend</tt> - If +true+, the callback will be prepended to the
|
||||
# existing chain rather than appended.
|
||||
def set_callback(name, *filter_list, &block)
|
||||
|
|
|
@ -319,6 +319,14 @@ class Order < ApplicationRecord
|
|||
end
|
||||
```
|
||||
|
||||
As the proc is evaluated in the context of the object, it is also possible to write this as:
|
||||
|
||||
```ruby
|
||||
class Order < ApplicationRecord
|
||||
before_save :normalize_card_number, if: Proc.new { paid_with_card? }
|
||||
end
|
||||
```
|
||||
|
||||
### Multiple Conditions for Callbacks
|
||||
|
||||
When writing conditional callbacks, it is possible to mix both `:if` and `:unless` in the same callback declaration:
|
||||
|
|
Loading…
Reference in a new issue