Skip to content
This repository
Browse code

Merge pull request #7184 from anthonyalberto/xml_dasherize_include

to_xml dasherize option should be passed to included associations
  • Loading branch information...
commit b3641967b8f8a69edd1d1c7d801c0184d35c3af6 2 parents 89cc4f1 + 78f5874
Carlos Antonio da Silva carlosantoniodasilva authored
2  activemodel/CHANGELOG.md
Source Rendered
... ... @@ -1,5 +1,7 @@
1 1 ## Rails 4.0.0 (unreleased) ##
2 2
  3 +* 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*
  4 +
3 5 * Changed `AM::Serializers::JSON.include_root_in_json' default value to false.
4 6 Now, AM Serializers and AR objects have the same default behaviour. Fixes #6578.
5 7
4 activemodel/lib/active_model/serializers/xml.rb 100644 → 100755
@@ -115,6 +115,10 @@ def add_associations(association, records, opts)
115 115 merged_options = opts.merge(options.slice(:builder, :indent))
116 116 merged_options[:skip_instruct] = true
117 117
  118 + [:skip_types, :dasherize, :camelize].each do |key|
  119 + merged_options[key] = options[key] if merged_options[key].nil? && !options[key].nil?
  120 + end
  121 +
118 122 if records.respond_to?(:to_ary)
119 123 records = records.to_ary
120 124
38 activemodel/test/cases/serializers/xml_serialization_test.rb 100644 → 100755
@@ -28,7 +28,7 @@ class Address
28 28 extend ActiveModel::Naming
29 29 include ActiveModel::Serializers::Xml
30 30
31   - attr_accessor :street, :city, :state, :zip
  31 + attr_accessor :street, :city, :state, :zip, :apt_number
32 32
33 33 def attributes
34 34 instance_values
@@ -56,6 +56,7 @@ def setup
56 56 @contact.address.city = "Springfield"
57 57 @contact.address.state = "CA"
58 58 @contact.address.zip = 11111
  59 + @contact.address.apt_number = 35
59 60 @contact.friends = [Contact.new, Contact.new]
60 61 end
61 62
@@ -222,4 +223,39 @@ def to_ary
222 223 assert_match %r{<friends>}, xml
223 224 assert_match %r{<friend>}, xml
224 225 end
  226 +
  227 + test "propagates skip-types option to included associations and attributes" do
  228 + xml = @contact.to_xml :skip_types => true, :include => :address, :indent => 0
  229 + assert_match %r{<address>}, xml
  230 + assert_match %r{<apt-number>}, xml
  231 + end
  232 +
  233 + test "propagates camelize option to included associations and attributes" do
  234 + xml = @contact.to_xml :camelize => true, :include => :address, :indent => 0
  235 + assert_match %r{<Address>}, xml
  236 + assert_match %r{<AptNumber type="integer">}, xml
  237 + end
  238 +
  239 + test "propagates dasherize option to included associations and attributes" do
  240 + xml = @contact.to_xml :dasherize => false, :include => :address, :indent => 0
  241 + assert_match %r{<apt_number type="integer">}, xml
  242 + end
  243 +
  244 + test "don't propagate skip_types if skip_types is defined at the included association level" do
  245 + xml = @contact.to_xml :skip_types => true, :include => { :address => { :skip_types => false } }, :indent => 0
  246 + assert_match %r{<address>}, xml
  247 + assert_match %r{<apt-number type="integer">}, xml
  248 + end
  249 +
  250 + test "don't propagate camelize if camelize is defined at the included association level" do
  251 + xml = @contact.to_xml :camelize => true, :include => { :address => { :camelize => false } }, :indent => 0
  252 + assert_match %r{<address>}, xml
  253 + assert_match %r{<apt-number type="integer">}, xml
  254 + end
  255 +
  256 + test "don't propagate dasherize if dasherize is defined at the included association level" do
  257 + xml = @contact.to_xml :dasherize => false, :include => { :address => { :dasherize => true } }, :indent => 0
  258 + assert_match %r{<address>}, xml
  259 + assert_match %r{<apt-number type="integer">}, xml
  260 + end
225 261 end

0 comments on commit b364196

Please sign in to comment.
Something went wrong with that request. Please try again.