From 4d0dd11e340f7cb9e6ff9fb2a462e031f1dccbee Mon Sep 17 00:00:00 2001 From: Ludo van den Boom Date: Sun, 20 Feb 2011 12:46:41 +0100 Subject: [PATCH] ActiveRecord to_xml with :include associations should not singularize has_one/belongs_to association names --- .../active_record/serializers/xml_serializer.rb | 8 +++++--- activerecord/test/cases/serialization_test.rb | 6 +++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/activerecord/lib/active_record/serializers/xml_serializer.rb b/activerecord/lib/active_record/serializers/xml_serializer.rb index 8c4adf7..87f505a 100644 --- a/activerecord/lib/active_record/serializers/xml_serializer.rb +++ b/activerecord/lib/active_record/serializers/xml_serializer.rb @@ -196,8 +196,7 @@ module ActiveRecord #:nodoc: # TODO This can likely be cleaned up to simple use ActiveSupport::XmlMini.to_tag as well. def add_associations(association, records, opts) - association_name = association.to_s.singularize - merged_options = options.merge(opts).merge!(:root => association_name, :skip_instruct => true) + merged_options = options.merge(opts).merge!(:skip_instruct => true) if records.is_a?(Enumerable) tag = ActiveSupport::XmlMini.rename_key(association.to_s, options) @@ -206,6 +205,9 @@ module ActiveRecord #:nodoc: if records.empty? @builder.tag!(tag, type) else + association_name = association.to_s.singularize + merged_options.merge!(:root => association_name) + @builder.tag!(tag, type) do records.each do |record| if options[:skip_types] @@ -220,7 +222,7 @@ module ActiveRecord #:nodoc: end end elsif record = @serializable.send(association) - record.to_xml(merged_options) + record.to_xml(merged_options.merge(:root => association.to_s)) end end diff --git a/activerecord/test/cases/serialization_test.rb b/activerecord/test/cases/serialization_test.rb index 677d659..dbd7d1d 100644 --- a/activerecord/test/cases/serialization_test.rb +++ b/activerecord/test/cases/serialization_test.rb @@ -1,4 +1,5 @@ require "cases/helper" +require 'models/author' require 'models/contact' require 'models/topic' require 'models/reply' @@ -6,7 +7,7 @@ require 'models/company' class SerializationTest < ActiveRecord::TestCase - fixtures :topics, :companies, :accounts + fixtures :topics, :companies, :accounts, :authors, :author_addresses FORMATS = [ :xml, :json ] @@ -122,6 +123,9 @@ class SerializationTest < ActiveRecord::TestCase xml = companies(:second_client).to_xml(:indent => 0, :skip_instruct => true, :include => :firm) assert xml.include?("") + + xml = authors(:david).to_xml(:indent => 0, :skip_instruct => true, :include => :author_address) + assert xml.include?(%()) end def test_to_xml_including_multiple_associations -- 1.7.4.1