mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Remove deprecate passing string to :if
and :unless
conditional options [ci skip]
Follow up to #27608
This commit is contained in:
parent
6dccceef9c
commit
53ff5fc62f
2 changed files with 1 additions and 23 deletions
|
@ -288,7 +288,7 @@ Article destroyed
|
||||||
Conditional Callbacks
|
Conditional Callbacks
|
||||||
---------------------
|
---------------------
|
||||||
|
|
||||||
As with validations, we can also make the calling of a callback method conditional on the satisfaction of a given predicate. We can do this using the `:if` and `:unless` options, which can take a symbol, a string, a `Proc` or an `Array`. You may use the `:if` option when you want to specify under which conditions the callback **should** be called. If you want to specify the conditions under which the callback **should not** be called, then you may use the `:unless` option.
|
As with validations, we can also make the calling of a callback method conditional on the satisfaction of a given predicate. We can do this using the `:if` and `:unless` options, which can take a symbol, a `Proc` or an `Array`. You may use the `:if` option when you want to specify under which conditions the callback **should** be called. If you want to specify the conditions under which the callback **should not** be called, then you may use the `:unless` option.
|
||||||
|
|
||||||
### Using `:if` and `:unless` with a `Symbol`
|
### Using `:if` and `:unless` with a `Symbol`
|
||||||
|
|
||||||
|
@ -300,16 +300,6 @@ class Order < ApplicationRecord
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
### Using `:if` and `:unless` with a String
|
|
||||||
|
|
||||||
You can also use a string that will be evaluated using `eval` and hence needs to contain valid Ruby code. You should use this option only when the string represents a really short condition:
|
|
||||||
|
|
||||||
```ruby
|
|
||||||
class Order < ApplicationRecord
|
|
||||||
before_save :normalize_card_number, if: "paid_with_card?"
|
|
||||||
end
|
|
||||||
```
|
|
||||||
|
|
||||||
### Using `:if` and `:unless` with a `Proc`
|
### Using `:if` and `:unless` with a `Proc`
|
||||||
|
|
||||||
Finally, it is possible to associate `:if` and `:unless` with a `Proc` object. This option is best suited when writing short validation methods, usually one-liners:
|
Finally, it is possible to associate `:if` and `:unless` with a `Proc` object. This option is best suited when writing short validation methods, usually one-liners:
|
||||||
|
|
|
@ -916,18 +916,6 @@ class Order < ApplicationRecord
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
### Using a String with `:if` and `:unless`
|
|
||||||
|
|
||||||
You can also use a string that will be evaluated using `eval` and needs to
|
|
||||||
contain valid Ruby code. You should use this option only when the string
|
|
||||||
represents a really short condition.
|
|
||||||
|
|
||||||
```ruby
|
|
||||||
class Person < ApplicationRecord
|
|
||||||
validates :surname, presence: true, if: "name.nil?"
|
|
||||||
end
|
|
||||||
```
|
|
||||||
|
|
||||||
### Using a Proc with `:if` and `:unless`
|
### Using a Proc with `:if` and `:unless`
|
||||||
|
|
||||||
Finally, it's possible to associate `:if` and `:unless` with a `Proc` object
|
Finally, it's possible to associate `:if` and `:unless` with a `Proc` object
|
||||||
|
|
Loading…
Reference in a new issue