diff options
author | Yoshiyuki Hirano <yhirano@me.com> | 2017-12-16 10:55:52 +0900 |
---|---|---|
committer | Yoshiyuki Hirano <yhirano@me.com> | 2017-12-20 00:27:19 +0900 |
commit | 470d0e459f2d1cb8e6d5c0a8a2fe3282ca65690e (patch) | |
tree | 100a0689006c2b7c59b95d9150702169345eef8f /activemodel/lib | |
parent | 011f76e57b145b9e5ef1fd520df7d7096cf8896a (diff) | |
download | rails-470d0e459f2d1cb8e6d5c0a8a2fe3282ca65690e.tar.gz rails-470d0e459f2d1cb8e6d5c0a8a2fe3282ca65690e.tar.bz2 rails-470d0e459f2d1cb8e6d5c0a8a2fe3282ca65690e.zip |
Fix validation callbacks on multiple context
I found a bug that validation callbacks don't fire on multiple context.
So I've fixed it.
Example:
```ruby
class Dog
include ActiveModel::Validations
include ActiveModel::Validations::Callbacks
attr_accessor :history
def initialize
@history = []
end
before_validation :set_before_validation_on_a, on: :a
before_validation :set_before_validation_on_b, on: :b
after_validation :set_after_validation_on_a, on: :a
after_validation :set_after_validation_on_b, on: :b
def set_before_validation_on_a; history << "before_validation on a"; end
def set_before_validation_on_b; history << "before_validation on b"; end
def set_after_validation_on_a; history << "after_validation on a" ; end
def set_after_validation_on_b; history << "after_validation on b" ; end
end
```
Before:
```
d = Dog.new
d.valid?([:a, :b])
d.history # []
```
After:
```
d = Dog.new
d.valid?([:a, :b])
d.history # ["before_validation on a", "before_validation on b", "after_validation on a", "after_validation on b"]
```
Diffstat (limited to 'activemodel/lib')
-rw-r--r-- | activemodel/lib/active_model/validations/callbacks.rb | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/activemodel/lib/active_model/validations/callbacks.rb b/activemodel/lib/active_model/validations/callbacks.rb index 4d0ab2a2fe..11a8b2b229 100644 --- a/activemodel/lib/active_model/validations/callbacks.rb +++ b/activemodel/lib/active_model/validations/callbacks.rb @@ -54,14 +54,16 @@ module ActiveModel # person.valid? # => true # person.name # => "bob" def before_validation(*args, &block) - options = args.last - if options.is_a?(Hash) && options[:on] - options[:if] = Array(options[:if]) - options[:on] = Array(options[:on]) + options = args.extract_options! + options[:if] = Array(options[:if]) + + if options.key?(:on) options[:if].unshift ->(o) { - options[:on].include? o.validation_context + !(Array(options[:on]) & Array(o.validation_context)).empty? } end + + args << options set_callback(:validation, :before, *args, &block) end @@ -95,13 +97,15 @@ module ActiveModel options = args.extract_options! options[:prepend] = true options[:if] = Array(options[:if]) - if options[:on] - options[:on] = Array(options[:on]) + + if options.key?(:on) options[:if].unshift ->(o) { - options[:on].include? o.validation_context + !(Array(options[:on]) & Array(o.validation_context)).empty? } end - set_callback(:validation, :after, *(args << options), &block) + + args << options + set_callback(:validation, :after, *args, &block) end end |