From 13e8c5cb0ee33a504984ab0866778679a399b31f Mon Sep 17 00:00:00 2001 From: Scott Becker Date: Tue, 23 Dec 2008 17:16:13 -0800 Subject: [PATCH] Add an "extended" option to the to_xml method for active record errors. This breaks out the attribute and message into individual tags for machine readability --- activerecord/lib/active_record/validations.rb | 34 ++++++++++++++++++++++++- activerecord/test/cases/validations_test.rb | 9 ++++++ 2 files changed, 42 insertions(+), 1 deletions(-) diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb index 6a9690b..e358dd6 100644 --- a/activerecord/lib/active_record/validations.rb +++ b/activerecord/lib/active_record/validations.rb @@ -244,14 +244,46 @@ module ActiveRecord # # Name can't be blank # # Address can't be blank # # + # + # You may also return an 'extended' version which breaks out the attribute + # and message into individual tags for machine readability. + # + # company = Company.create(:address => '123 First St.') + # company.errors.to_xml(:extended => true) + # # => + # # + # # + # # name + # # is too short (minimum is 5 characters) + # # + # # + # # name + # # can't be blank + # # + # # + # # address + # # can't be blank + # # + # # + # def to_xml(options={}) options[:root] ||= "errors" options[:indent] ||= 2 + options[:extended] ||= false 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) } + if options[:extended] + each { |attribute, message| + e.error { + e.attribute(attribute) + e.message(message) + } + } + else + full_messages.each { |msg| e.error(msg) } + end end end diff --git a/activerecord/test/cases/validations_test.rb b/activerecord/test/cases/validations_test.rb index 380d8ac..ca99e2a 100644 --- a/activerecord/test/cases/validations_test.rb +++ b/activerecord/test/cases/validations_test.rb @@ -1399,6 +1399,15 @@ class ValidationsTest < ActiveRecord::TestCase assert xml.include?("Content Empty") end + def test_errors_to_xml_extended + r = Reply.new :title => "Wrong Create" + assert !r.valid? + xml = r.errors.to_xml(:skip_instruct => true, :extended => true) + assert_equal "", xml.first(8) + assert xml.include?("\n title\n is Wrong Create\n ") + assert xml.include?("\n content\n Empty\n ") + end + def test_validation_order Topic.validates_presence_of :title Topic.validates_length_of :title, :minimum => 2 -- 1.6.0.6 From 00e0b65556546065e5df0602319aaa8d84e9ba86 Mon Sep 17 00:00:00 2001 From: SCOTT BECKER Date: Mon, 5 Jan 2009 18:06:54 -0800 Subject: [PATCH] Added tests verifying the expected behavior for XML serialization of errors added to "base" --- activerecord/test/cases/validations_test.rb | 16 ++++++++++++++++ 1 files changed, 16 insertions(+), 0 deletions(-) diff --git a/activerecord/test/cases/validations_test.rb b/activerecord/test/cases/validations_test.rb index ca99e2a..e2e850e 100644 --- a/activerecord/test/cases/validations_test.rb +++ b/activerecord/test/cases/validations_test.rb @@ -1399,6 +1399,14 @@ class ValidationsTest < ActiveRecord::TestCase assert xml.include?("Content Empty") end + def test_errors_added_to_base_to_xml + r = Reply.new :title => "Wrong Create" + r.errors.add_to_base("is invalid") + xml = r.errors.to_xml(:skip_instruct => true) + assert_equal "", xml.first(8) + assert xml.include?("is invalid") + end + def test_errors_to_xml_extended r = Reply.new :title => "Wrong Create" assert !r.valid? @@ -1408,6 +1416,14 @@ class ValidationsTest < ActiveRecord::TestCase assert xml.include?("\n content\n Empty\n ") end + def test_errors_added_to_base_to_xml_extended + r = Reply.new :title => "Wrong Create" + r.errors.add_to_base("is invalid") + xml = r.errors.to_xml(:skip_instruct => true, :extended => true) + assert_equal "", xml.first(8) + assert xml.include?("\n base\n is invalid\n ") + end + def test_validation_order Topic.validates_presence_of :title Topic.validates_length_of :title, :minimum => 2 -- 1.6.0.6