mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Ensure ActiveRecord#to_xml respects :skip_types for included associations [#1632 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
This commit is contained in:
parent
0c9bbf8c9d
commit
8272630ce8
2 changed files with 24 additions and 7 deletions
|
@ -231,16 +231,22 @@ module ActiveRecord #:nodoc:
|
|||
def add_associations(association, records, opts)
|
||||
if records.is_a?(Enumerable)
|
||||
tag = reformat_name(association.to_s)
|
||||
type = options[:skip_types] ? {} : {:type => "array"}
|
||||
|
||||
if records.empty?
|
||||
builder.tag!(tag, :type => :array)
|
||||
builder.tag!(tag, type)
|
||||
else
|
||||
builder.tag!(tag, :type => :array) do
|
||||
builder.tag!(tag, type) do
|
||||
association_name = association.to_s.singularize
|
||||
records.each do |record|
|
||||
record.to_xml opts.merge(
|
||||
:root => association_name,
|
||||
:type => (record.class.to_s.underscore == association_name ? nil : record.class.name)
|
||||
)
|
||||
if options[:skip_types]
|
||||
record_type = {}
|
||||
else
|
||||
record_class = (record.class.to_s.underscore == association_name) ? nil : record.class.name
|
||||
record_type = {:type => record_class}
|
||||
end
|
||||
|
||||
record.to_xml opts.merge(:root => association_name).merge(record_type)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -38,11 +38,15 @@ class XmlSerializationTest < ActiveRecord::TestCase
|
|||
assert_match %r{<CreatedAt}, @xml
|
||||
end
|
||||
|
||||
def test_should_allow_skipped_types
|
||||
@xml = Contact.new(:age => 25).to_xml :skip_types => true
|
||||
assert %r{<age>25</age>}.match(@xml)
|
||||
end
|
||||
|
||||
def test_should_include_yielded_additions
|
||||
@xml = Contact.new.to_xml do |xml|
|
||||
xml.creator "David"
|
||||
end
|
||||
|
||||
assert_match %r{<creator>David</creator>}, @xml
|
||||
end
|
||||
end
|
||||
|
@ -145,6 +149,13 @@ class DatabaseConnectedXmlSerializationTest < ActiveRecord::TestCase
|
|||
assert_match %r{<hello-post type="StiPost">}, xml
|
||||
end
|
||||
|
||||
def test_included_associations_should_skip_types
|
||||
xml = authors(:david).to_xml :include=>:hello_posts, :indent => 0, :skip_types => true
|
||||
assert_match %r{<hello-posts>}, xml
|
||||
assert_match %r{<hello-post>}, xml
|
||||
assert_match %r{<hello-post>}, xml
|
||||
end
|
||||
|
||||
def test_methods_are_called_on_object
|
||||
xml = authors(:david).to_xml :methods => :label, :indent => 0
|
||||
assert_match %r{<label>.*</label>}, xml
|
||||
|
|
Loading…
Reference in a new issue