Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

to_xml dasherize option should be passed to included associations #7184

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions activemodel/CHANGELOG.md
@@ -1,5 +1,7 @@
## Rails 4.0.0 (unreleased) ##

* Changed `ActiveModel::Serializers::Xml::Serializer#add_associations` to by default propagate `:skip_types, :dasherize, :camelize` keys to included associations. It can be overriden on each association by explicitly specifying the option on one or more associations *Anthony Alberto*

* Changed `AM::Serializers::JSON.include_root_in_json' default value to false.
Now, AM Serializers and AR objects have the same default behaviour. Fixes #6578.

Expand Down
4 changes: 4 additions & 0 deletions activemodel/lib/active_model/serializers/xml.rb 100644 → 100755
Expand Up @@ -115,6 +115,10 @@ def add_associations(association, records, opts)
merged_options = opts.merge(options.slice(:builder, :indent))
merged_options[:skip_instruct] = true

[:skip_types, :dasherize, :camelize].each do |key|
merged_options[key] = options[key] if merged_options[key].nil? && !options[key].nil?
end

if records.respond_to?(:to_ary)
records = records.to_ary

Expand Down
38 changes: 37 additions & 1 deletion activemodel/test/cases/serializers/xml_serialization_test.rb 100644 → 100755
Expand Up @@ -28,7 +28,7 @@ class Address
extend ActiveModel::Naming
include ActiveModel::Serializers::Xml

attr_accessor :street, :city, :state, :zip
attr_accessor :street, :city, :state, :zip, :apt_number

def attributes
instance_values
Expand Down Expand Up @@ -56,6 +56,7 @@ def setup
@contact.address.city = "Springfield"
@contact.address.state = "CA"
@contact.address.zip = 11111
@contact.address.apt_number = 35
@contact.friends = [Contact.new, Contact.new]
end

Expand Down Expand Up @@ -222,4 +223,39 @@ def to_ary
assert_match %r{<friends>}, xml
assert_match %r{<friend>}, xml
end

test "propagates skip-types option to included associations and attributes" do
xml = @contact.to_xml :skip_types => true, :include => :address, :indent => 0
assert_match %r{<address>}, xml
assert_match %r{<apt-number>}, xml
end

test "propagates camelize option to included associations and attributes" do
xml = @contact.to_xml :camelize => true, :include => :address, :indent => 0
assert_match %r{<Address>}, xml
assert_match %r{<AptNumber type="integer">}, xml
end

test "propagates dasherize option to included associations and attributes" do
xml = @contact.to_xml :dasherize => false, :include => :address, :indent => 0
assert_match %r{<apt_number type="integer">}, xml
end

test "don't propagate skip_types if skip_types is defined at the included association level" do
xml = @contact.to_xml :skip_types => true, :include => { :address => { :skip_types => false } }, :indent => 0
assert_match %r{<address>}, xml
assert_match %r{<apt-number type="integer">}, xml
end

test "don't propagate camelize if camelize is defined at the included association level" do
xml = @contact.to_xml :camelize => true, :include => { :address => { :camelize => false } }, :indent => 0
assert_match %r{<address>}, xml
assert_match %r{<apt-number type="integer">}, xml
end

test "don't propagate dasherize if dasherize is defined at the included association level" do
xml = @contact.to_xml :dasherize => false, :include => { :address => { :dasherize => true } }, :indent => 0
assert_match %r{<address>}, xml
assert_match %r{<apt-number type="integer">}, xml
end
end