Skip to content

Commit

Permalink
Add ActiveRecord::Base#invalid? as the opposite of #valid? [#2159 sta…
Browse files Browse the repository at this point in the history
…te:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information
packagethief authored and lifo committed Mar 8, 2009
1 parent 7a26a67 commit 96eaeee
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 5 additions & 0 deletions activerecord/lib/active_record/validations.rb
Expand Up @@ -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)
Expand Down
13 changes: 12 additions & 1 deletion activerecord/test/cases/validations_test.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

2 comments on commit 96eaeee

@skaes
Copy link

@skaes skaes commented on 96eaeee Mar 13, 2009

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about assert_invalid?

@lifo
Copy link
Member

@lifo lifo commented on 96eaeee Mar 13, 2009

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Please sign in to comment.