aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorDmitry Polushkin <dmitry.polushkin@gmail.com>2015-07-30 09:58:46 +0100
committerDmitry Polushkin <dmitry.polushkin@gmail.com>2015-07-30 10:05:29 +0100
commit86e3b047ba0349bd88952d4e54504327c3f7e59c (patch)
treef2dea38df5f5f4c92427a8fc0613b14b80221356 /activerecord/lib
parent64c1264419f766a306eba0418c1030b87489ea67 (diff)
downloadrails-86e3b047ba0349bd88952d4e54504327c3f7e59c.tar.gz
rails-86e3b047ba0349bd88952d4e54504327c3f7e59c.tar.bz2
rails-86e3b047ba0349bd88952d4e54504327c3f7e59c.zip
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"]} ```
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/validations.rb6
1 files changed, 5 insertions, 1 deletions
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