Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

docs + test for each_error

[#3185 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information...
commit 0706bdce603897a869b9af753fddb81a9c9dc7ec 1 parent 5d7ad7b
@lawrencepit lawrencepit authored jeremy committed
View
12 activerecord/lib/active_record/validations.rb
@@ -239,6 +239,18 @@ def each
@errors.each_key { |attr| @errors[attr].each { |error| yield attr, error.message } }
end
+ # Yields each attribute and associated error per error added.
+ #
+ # class Company < ActiveRecord::Base
+ # validates_presence_of :name, :address, :email
+ # validates_length_of :name, :in => 5..30
+ # end
+ #
+ # company = Company.create(:address => '123 First St.')
+ # company.errors.each_error{|attr,err| puts "#{attr} - #{err.type}" }
+ # # => name - :too_short
+ # # name - :blank
+ # # address - :blank
def each_error
@errors.each_key { |attr| @errors[attr].each { |error| yield attr, error } }
end
View
13 activerecord/test/cases/validations_test.rb
@@ -188,7 +188,7 @@ def test_values_are_not_retrieved_unless_needed
end
end
- def test_single_error_per_attr_iteration
+ def test_single_error_string_per_attr_iteration
r = Reply.new
r.save
@@ -199,6 +199,17 @@ def test_single_error_per_attr_iteration
assert errors.include?(["content", "Empty"])
end
+ def test_single_error_object_per_attr_iteration
+ r = Reply.new
+ r.save
+
+ errors = []
+ r.errors.each_error { |attr, error| errors << [attr, error.attribute] }
+
+ assert errors.include?(["title", "title"])
+ assert errors.include?(["content", "content"])
+ end
+
def test_multiple_errors_per_attr_iteration_with_full_error_composition
r = Reply.new
r.title = "Wrong Create"
Please sign in to comment.
Something went wrong with that request. Please try again.