提交 0c960602 编写于 作者: M Michael Koziarski

Change Base#to_xml to take the name for a container element for an association...

Change Base#to_xml to take the name for a container element for an association from the element name, not the first record's class name.  Closes #7004

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6654 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
上级 b8e74466
*SVN*
* Use association name for the wrapper element when using .to_xml. Previous behaviour lead to non-deterministic situations with STI and polymorphic associations. [Koz, jstrachan]
* Improve performance of calling .create on has_many :through associations. [evan]
* Improved cloning performance by relying less on exception raising #8159 [Blaine]
......
......@@ -204,11 +204,11 @@ def add_includes
when :has_many, :has_and_belongs_to_many
records = @record.send(association).to_a
unless records.empty?
tag = records.first.class.to_s.underscore.pluralize
tag = association.to_s
tag = tag.dasherize if dasherize?
builder.tag!(tag) do
records.each { |r| r.to_xml(opts.merge(:root => association.to_s.singularize)) }
records.each { |r| r.to_xml(opts.merge(:root=>r.class.to_s.underscore)) }
end
end
when :has_one, :belongs_to
......
......@@ -133,7 +133,8 @@ def test_should_serialize_yaml
class DatabaseConnectedXmlSerializationTest < Test::Unit::TestCase
fixtures :authors, :posts
# to_xml used to mess with the hash the user provided which
# caused the builder to be reused
# caused the builder to be reused. This meant the document kept
# getting appended to.
def test_passing_hash_shouldnt_reuse_builder
options = {:include=>:posts}
david = authors(:david)
......@@ -141,4 +142,10 @@ def test_passing_hash_shouldnt_reuse_builder
second_xml_size = david.to_xml(options).size
assert_equal first_xml_size, second_xml_size
end
def test_include_uses_association_name
xml = authors(:david).to_xml :include=>:hello_posts, :indent=>0
assert(xml.include?(%(<hello_posts><post>)) || xml.include?(%(</post></hello-posts>)))
end
end
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册