aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2015-09-08 13:12:05 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2015-09-08 13:12:05 -0300
commitff83868b10001af74c6f258e878bb29be6425c8d (patch)
treea49cbce351c97c695532d11dd3d105e0d89d69ef /activerecord
parent8d1af2b10c02ab5ecd32be0d2a5b1f937ddd25e1 (diff)
parente3d99e239dd1b59d31ac90f74186ded8b55de599 (diff)
downloadrails-ff83868b10001af74c6f258e878bb29be6425c8d.tar.gz
rails-ff83868b10001af74c6f258e878bb29be6425c8d.tar.bz2
rails-ff83868b10001af74c6f258e878bb29be6425c8d.zip
Merge pull request #21535 from dmitry/feature/validate-multiple-contexts
Validate multiple contexts on `valid?` and `invalid?` at once
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/validations.rb6
-rw-r--r--activerecord/test/cases/validations_test.rb7
2 files changed, 12 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb
index 74daece4e8..4113ca4561 100644
--- a/activerecord/lib/active_record/validations.rb
+++ b/activerecord/lib/active_record/validations.rb
@@ -60,7 +60,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
@@ -69,6 +69,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