Add ActiveRecord::Base#invalid? as the opposite of #valid? [#2159 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
This commit is contained in:
Jeffrey Hardy 2009-03-06 17:43:00 -05:00 committed by Pratik Naik
parent 7a26a67b42
commit 96eaeee446
2 changed files with 17 additions and 1 deletions

@ -1040,6 +1040,11 @@ def valid?
errors.empty? errors.empty?
end end
# Performs the opposite of <tt>valid?</tt>. Returns true if errors were added, false otherwise.
def invalid?
!valid?
end
# Returns the Errors object that holds all information about attribute error messages. # Returns the Errors object that holds all information about attribute error messages.
def errors def errors
@errors ||= Errors.new(self) @errors ||= Errors.new(self)

@ -1430,6 +1430,17 @@ def test_validation_order
assert_equal "can't be blank", t.errors.on("title").first assert_equal "can't be blank", t.errors.on("title").first
end end
def test_invalid_should_be_the_opposite_of_valid
Topic.validates_presence_of :title
t = Topic.new
assert t.invalid?
assert t.errors.invalid?(:title)
t.title = 'Things are going to change'
assert !t.invalid?
end
# previous implementation of validates_presence_of eval'd the # previous implementation of validates_presence_of eval'd the
# string with the wrong binding, this regression test is to # string with the wrong binding, this regression test is to
# ensure that it works correctly # ensure that it works correctly