aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
authorHenrik Nyh <henrik@nyh.se>2014-03-23 12:09:55 +0100
committerHenrik Nyh <henrik@nyh.se>2014-03-27 17:56:14 +0100
commit2e70f44123cb6fab9124b52a6cc02ff453b8397f (patch)
treed2fe1fcc48ea68276cb41918249fc20b95b3252c /activemodel
parent5bf38ffc4f853a6ccc5812f0ed8341776b117aea (diff)
downloadrails-2e70f44123cb6fab9124b52a6cc02ff453b8397f.tar.gz
rails-2e70f44123cb6fab9124b52a6cc02ff453b8397f.tar.bz2
rails-2e70f44123cb6fab9124b52a6cc02ff453b8397f.zip
ActiveRecord/ActiveModel '#validate' alias for 'valid?'
It's unintuitive to call '#valid?' when you want to run validations but don't care about the return value. The alias in ActiveRecord isn't strictly necessary (the ActiveModel alias is still in effect), but it clarifies.
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/lib/active_model/validations.rb4
-rw-r--r--activemodel/test/cases/validations_test.rb9
2 files changed, 13 insertions, 0 deletions
diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb
index e9674d5143..cf97f45dba 100644
--- a/activemodel/lib/active_model/validations.rb
+++ b/activemodel/lib/active_model/validations.rb
@@ -285,6 +285,8 @@ module ActiveModel
# Runs all the specified validations and returns +true+ if no errors were
# added otherwise +false+.
#
+ # Aliased as validate.
+ #
# class Person
# include ActiveModel::Validations
#
@@ -319,6 +321,8 @@ module ActiveModel
self.validation_context = current_context
end
+ alias_method :validate, :valid?
+
# Performs the opposite of <tt>valid?</tt>. Returns +true+ if errors were
# added, +false+ otherwise.
#
diff --git a/activemodel/test/cases/validations_test.rb b/activemodel/test/cases/validations_test.rb
index 4ea5670d32..6a74ee353d 100644
--- a/activemodel/test/cases/validations_test.rb
+++ b/activemodel/test/cases/validations_test.rb
@@ -295,6 +295,15 @@ class ValidationsTest < ActiveModel::TestCase
assert auto.valid?
end
+ def test_validate
+ auto = Automobile.new
+
+ assert_empty auto.errors
+
+ auto.validate
+ assert_not_empty auto.errors
+ end
+
def test_strict_validation_in_validates
Topic.validates :title, strict: true, presence: true
assert_raises ActiveModel::StrictValidationFailed do