Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add ActiveRecord::Base#invalid? as the opposite of #valid? [#2159 sta…

…te:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information...
commit 96eaeee4467a03cba3c4c30aeb6fc6afe545ae1d 1 parent 7a26a67
@packagethief packagethief authored lifo committed
View
5 activerecord/lib/active_record/validations.rb
@@ -1040,6 +1040,11 @@ def valid?
errors.empty?
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.
def errors
@errors ||= Errors.new(self)
View
13 activerecord/test/cases/validations_test.rb
@@ -170,7 +170,7 @@ def test_create_with_exceptions_using_scope_and_empty_attributes
assert_equal person.first_name, "Mary", "should be ok when no attributes are passed to create!"
end
end
- end
+ end
def test_single_error_per_attr_iteration
r = Reply.new
@@ -1430,6 +1430,17 @@ def test_validation_order
assert_equal "can't be blank", t.errors.on("title").first
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
# string with the wrong binding, this regression test is to
# ensure that it works correctly

2 comments on commit 96eaeee

@skaes

how about assert_invalid?

@lifo
Collaborator

Well, we’ve already deprecated assert_valid. So probably best not to add that.

Please sign in to comment.
Something went wrong with that request. Please try again.