Skip to content

Commit

Permalink
Merge pull request rails#14456 from henrik/add-validate-method
Browse files Browse the repository at this point in the history
ActiveRecord/ActiveModel '#validate' alias for 'valid?'
  • Loading branch information
rafaelfranca committed Mar 27, 2014
2 parents 5bf38ff + 2e70f44 commit 56dabd8
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions activemodel/lib/active_model/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ def errors
# Runs all the specified validations and returns +true+ if no errors were
# added otherwise +false+.
#
# Aliased as validate.
#
# class Person
# include ActiveModel::Validations
#
Expand Down Expand Up @@ -319,6 +321,8 @@ def valid?(context = nil)
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.
#
Expand Down
9 changes: 9 additions & 0 deletions activemodel/test/cases/validations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,15 @@ def test_validations_on_the_instance_level
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
Expand Down
6 changes: 6 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
* Introduce `validate` as an alias for `valid?`.

This is more intuitive when you want to run validations but don't care about the return value.

*Henrik Nyh*

* Create indexes inline in CREATE TABLE for MySQL.

This is important, because adding an index on a temporary table after it has been created
Expand Down
4 changes: 4 additions & 0 deletions activerecord/lib/active_record/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ def save!(options={})
# Runs all the validations within the specified context. Returns +true+ if
# no errors are found, +false+ otherwise.
#
# Aliased as validate.
#
# If the argument is +false+ (default is +nil+), the context is set to <tt>:create</tt> if
# <tt>new_record?</tt> is +true+, and to <tt>:update</tt> if it is not.
#
Expand All @@ -71,6 +73,8 @@ def valid?(context = nil)
errors.empty? && output
end

alias_method :validate, :valid?

protected

def perform_validations(options={}) # :nodoc:
Expand Down
15 changes: 15 additions & 0 deletions activerecord/test/cases/validations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ def test_error_on_given_context
assert r.save(:context => :special_case)
end

def test_validate
r = WrongReply.new

r.validate
assert_empty r.errors[:author_name]

r.validate(:special_case)
assert_not_empty r.errors[:author_name]

r.author_name = "secret"

r.validate(:special_case)
assert_empty r.errors[:author_name]
end

def test_invalid_record_exception
assert_raise(ActiveRecord::RecordInvalid) { WrongReply.create! }
assert_raise(ActiveRecord::RecordInvalid) { WrongReply.new.save! }
Expand Down

0 comments on commit 56dabd8

Please sign in to comment.