Skip to content

Commit 2e70f44

Browse files
committed
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.
1 parent 5bf38ff commit 2e70f44

File tree

5 files changed

+38
-0
lines changed

5 files changed

+38
-0
lines changed

activemodel/lib/active_model/validations.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,8 @@ def errors
285285
# Runs all the specified validations and returns +true+ if no errors were
286286
# added otherwise +false+.
287287
#
288+
# Aliased as validate.
289+
#
288290
# class Person
289291
# include ActiveModel::Validations
290292
#
@@ -319,6 +321,8 @@ def valid?(context = nil)
319321
self.validation_context = current_context
320322
end
321323

324+
alias_method :validate, :valid?
325+
322326
# Performs the opposite of <tt>valid?</tt>. Returns +true+ if errors were
323327
# added, +false+ otherwise.
324328
#

activemodel/test/cases/validations_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,15 @@ def test_validations_on_the_instance_level
295295
assert auto.valid?
296296
end
297297

298+
def test_validate
299+
auto = Automobile.new
300+
301+
assert_empty auto.errors
302+
303+
auto.validate
304+
assert_not_empty auto.errors
305+
end
306+
298307
def test_strict_validation_in_validates
299308
Topic.validates :title, strict: true, presence: true
300309
assert_raises ActiveModel::StrictValidationFailed do

activerecord/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* Introduce `validate` as an alias for `valid?`.
2+
3+
This is more intuitive when you want to run validations but don't care about the return value.
4+
5+
*Henrik Nyh*
6+
17
* Create indexes inline in CREATE TABLE for MySQL.
28

39
This is important, because adding an index on a temporary table after it has been created

activerecord/lib/active_record/validations.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ def save!(options={})
6060
# Runs all the validations within the specified context. Returns +true+ if
6161
# no errors are found, +false+ otherwise.
6262
#
63+
# Aliased as validate.
64+
#
6365
# If the argument is +false+ (default is +nil+), the context is set to <tt>:create</tt> if
6466
# <tt>new_record?</tt> is +true+, and to <tt>:update</tt> if it is not.
6567
#
@@ -71,6 +73,8 @@ def valid?(context = nil)
7173
errors.empty? && output
7274
end
7375

76+
alias_method :validate, :valid?
77+
7478
protected
7579

7680
def perform_validations(options={}) # :nodoc:

activerecord/test/cases/validations_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,21 @@ def test_error_on_given_context
5252
assert r.save(:context => :special_case)
5353
end
5454

55+
def test_validate
56+
r = WrongReply.new
57+
58+
r.validate
59+
assert_empty r.errors[:author_name]
60+
61+
r.validate(:special_case)
62+
assert_not_empty r.errors[:author_name]
63+
64+
r.author_name = "secret"
65+
66+
r.validate(:special_case)
67+
assert_empty r.errors[:author_name]
68+
end
69+
5570
def test_invalid_record_exception
5671
assert_raise(ActiveRecord::RecordInvalid) { WrongReply.create! }
5772
assert_raise(ActiveRecord::RecordInvalid) { WrongReply.new.save! }

0 commit comments

Comments
 (0)