aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/CHANGELOG2
-rwxr-xr-xactiverecord/lib/active_record/validations.rb12
-rwxr-xr-xactiverecord/test/validations_test.rb9
3 files changed, 23 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index a8141cd9d7..dd5dbdc71c 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Add ActiveRecord::Errors#to_xml [Jamis Buck]
+
* Properly quote index names in migrations (closes #4764) [John Long]
* Fix the HasManyAssociation#count method so it uses the new ActiveRecord::Base#count syntax, while maintaining backwards compatibility. [Rick]
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb
index d31b53b941..9e58599f81 100755
--- a/activerecord/lib/active_record/validations.rb
+++ b/activerecord/lib/active_record/validations.rb
@@ -156,6 +156,18 @@ module ActiveRecord
alias_method :count, :size
alias_method :length, :size
+
+ # Return an XML representation of this error object.
+ def to_xml(options={})
+ options[:root] ||= "errors"
+ options[:indent] ||= 2
+ options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])
+
+ options[:builder].instruct! unless options.delete(:skip_instruct)
+ options[:builder].errors do |e|
+ full_messages.each { |msg| e.error(msg) }
+ end
+ end
end
diff --git a/activerecord/test/validations_test.rb b/activerecord/test/validations_test.rb
index b73058a64b..6ef455459b 100755
--- a/activerecord/test/validations_test.rb
+++ b/activerecord/test/validations_test.rb
@@ -995,6 +995,15 @@ class ValidationsTest < Test::Unit::TestCase
r.topic = Topic.find :first
assert r.valid?
end
+
+ def test_errors_to_xml
+ r = Reply.new :title => "Wrong Create"
+ assert !r.valid?
+ xml = r.errors.to_xml(:skip_instruct => true)
+ assert_equal "<errors>", xml.first(8)
+ assert xml.include?("<error>Title is Wrong Create</error>")
+ assert xml.include?("<error>Content Empty</error>")
+ end
end