From 86e3b047ba0349bd88952d4e54504327c3f7e59c Mon Sep 17 00:00:00 2001
From: Dmitry Polushkin <dmitry.polushkin@gmail.com>
Date: Thu, 30 Jul 2015 09:58:46 +0100
Subject: Validate multiple contexts on `valid?` and `invalid?` at once.

Example:

```ruby
class Person
  include ActiveModel::Validations

  attr_reader :name, :title
  validates_presence_of :name, on: :create
  validates_presence_of :title, on: :update
end

person = Person.new
person.valid?([:create, :update])    # => true
person.errors.messages               # => {:name=>["can't be blank"], :title=>["can't be blank"]}
```
---
 activemodel/CHANGELOG.md                              | 18 ++++++++++++++++++
 activemodel/lib/active_model/validations.rb           |  2 +-
 .../cases/validations/validations_context_test.rb     | 19 +++++++++++++++++++
 activerecord/lib/active_record/validations.rb         |  6 +++++-
 activerecord/test/cases/validations_test.rb           |  7 +++++++
 5 files changed, 50 insertions(+), 2 deletions(-)

diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md
index dddfd940bb..625dfa404b 100644
--- a/activemodel/CHANGELOG.md
+++ b/activemodel/CHANGELOG.md
@@ -1,3 +1,21 @@
+*   Validate multiple contexts on `valid?` and `invalid?` at once.
+
+    Example:
+    
+        class Person
+          include ActiveModel::Validations
+    
+          attr_reader :name, :title
+          validates_presence_of :name, on: :create
+          validates_presence_of :title, on: :update
+        end
+    
+        person = Person.new
+        person.valid?([:create, :update])    # => true
+        person.errors.messages               # => {:name=>["can't be blank"], :title=>["can't be blank"]}
+
+    *Dmitry Polushkin*
+
 *   Ensure `method_missing` is called for methods passed to
     `ActiveModel::Serialization#serializable_hash` that don't exist.
 
diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb
index 5f1dde4aa3..f23c920d87 100644
--- a/activemodel/lib/active_model/validations.rb
+++ b/activemodel/lib/active_model/validations.rb
@@ -162,7 +162,7 @@ module ActiveModel
           options = options.dup
           options[:if] = Array(options[:if])
           options[:if].unshift ->(o) {
-            Array(options[:on]).include?(o.validation_context)
+            !(Array(options[:on]) & Array(o.validation_context)).empty?
           }
         end
 
diff --git a/activemodel/test/cases/validations/validations_context_test.rb b/activemodel/test/cases/validations/validations_context_test.rb
index 150dce379f..b901a1523e 100644
--- a/activemodel/test/cases/validations/validations_context_test.rb
+++ b/activemodel/test/cases/validations/validations_context_test.rb
@@ -8,6 +8,7 @@ class ValidationsContextTest < ActiveModel::TestCase
   end
 
   ERROR_MESSAGE = "Validation error from validator"
+  ANOTHER_ERROR_MESSAGE = "Another validation error from validator"
 
   class ValidatorThatAddsErrors < ActiveModel::Validator
     def validate(record)
@@ -15,6 +16,12 @@ class ValidationsContextTest < ActiveModel::TestCase
     end
   end
 
+  class AnotherValidatorThatAddsErrors < ActiveModel::Validator
+    def validate(record)
+      record.errors[:base] << ANOTHER_ERROR_MESSAGE
+    end
+  end
+
   test "with a class that adds errors on create and validating a new model with no arguments" do
     Topic.validates_with(ValidatorThatAddsErrors, on: :create)
     topic = Topic.new
@@ -46,4 +53,16 @@ class ValidationsContextTest < ActiveModel::TestCase
     assert topic.invalid?(:context2), "Validation did not run on context2 when 'on' is set to context1 and context2"
     assert topic.errors[:base].include?(ERROR_MESSAGE)
   end
+
+  test "with a class that validating a model for a multiple contexts" do
+    Topic.validates_with(ValidatorThatAddsErrors, on: :context1)
+    Topic.validates_with(AnotherValidatorThatAddsErrors, on: :context2)
+
+    topic = Topic.new
+    assert topic.valid?, "Validation ran with no context given when 'on' is set to context1 and context2"
+
+    assert topic.invalid?([:context1, :context2]), "Validation did not run on context1 when 'on' is set to context1 and context2"
+    assert topic.errors[:base].include?(ERROR_MESSAGE)
+    assert topic.errors[:base].include?(ANOTHER_ERROR_MESSAGE)
+  end
 end
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb
index 34d96b19fe..108fe548c8 100644
--- a/activerecord/lib/active_record/validations.rb
+++ b/activerecord/lib/active_record/validations.rb
@@ -54,7 +54,7 @@ module ActiveRecord
     # Validations with no <tt>:on</tt> option will run no matter the context. Validations with
     # some <tt>:on</tt> option will only run in the specified context.
     def valid?(context = nil)
-      context ||= (new_record? ? :create : :update)
+      context ||= default_validation_context
       output = super(context)
       errors.empty? && output
     end
@@ -63,6 +63,10 @@ module ActiveRecord
 
   protected
 
+    def default_validation_context
+      [new_record? ? :create : :update]
+    end
+
     def raise_validation_error
       raise(RecordInvalid.new(self))
     end
diff --git a/activerecord/test/cases/validations_test.rb b/activerecord/test/cases/validations_test.rb
index f4f316f393..a429d06aad 100644
--- a/activerecord/test/cases/validations_test.rb
+++ b/activerecord/test/cases/validations_test.rb
@@ -52,6 +52,13 @@ class ValidationsTest < ActiveRecord::TestCase
     assert r.valid?(:special_case)
   end
 
+  def test_invalid_using_multiple_contexts
+    r = WrongReply.new(:title => 'Wrong Create')
+    assert r.invalid?([:special_case, :create])
+    assert_equal "Invalid", r.errors[:author_name].join
+    assert_equal "is Wrong Create", r.errors[:title].join
+  end
+
   def test_validate
     r = WrongReply.new
 
-- 
cgit v1.2.3