From 78f5874c82ed50d405cbe1ae9a9e75b7b2ae8ef5 Mon Sep 17 00:00:00 2001 From: Anthony Date: Fri, 27 Jul 2012 15:13:05 -0400 Subject: Following the false issue reporting I did here : https://github.com/rails/rails/issues/6958 - Enable propagation of :skip_types, :dasherize and :camelize on included models by default - Adding the option to override this propagation on a per-include basis (:include => { :model => { :dasherize => false } } - Enough tests to prove it works - Updated activemodel CHANGELOG.md Squashed my commits --- activemodel/CHANGELOG.md | 2 ++ activemodel/lib/active_model/serializers/xml.rb | 4 +++ .../cases/serializers/xml_serialization_test.rb | 38 +++++++++++++++++++++- 3 files changed, 43 insertions(+), 1 deletion(-) mode change 100644 => 100755 activemodel/lib/active_model/serializers/xml.rb mode change 100644 => 100755 activemodel/test/cases/serializers/xml_serialization_test.rb diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md index 9188719b78..12e0956d3c 100644 --- a/activemodel/CHANGELOG.md +++ b/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. diff --git a/activemodel/lib/active_model/serializers/xml.rb b/activemodel/lib/active_model/serializers/xml.rb old mode 100644 new mode 100755 index 016d821fdf..cf742d0569 --- a/activemodel/lib/active_model/serializers/xml.rb +++ b/activemodel/lib/active_model/serializers/xml.rb @@ -115,6 +115,10 @@ module ActiveModel 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 diff --git a/activemodel/test/cases/serializers/xml_serialization_test.rb b/activemodel/test/cases/serializers/xml_serialization_test.rb old mode 100644 new mode 100755 index a93323a3a8..8c5a3c5efd --- a/activemodel/test/cases/serializers/xml_serialization_test.rb +++ b/activemodel/test/cases/serializers/xml_serialization_test.rb @@ -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 @@ -56,6 +56,7 @@ class XmlSerializationTest < ActiveModel::TestCase @contact.address.city = "Springfield" @contact.address.state = "CA" @contact.address.zip = 11111 + @contact.address.apt_number = 35 @contact.friends = [Contact.new, Contact.new] end @@ -222,4 +223,39 @@ class XmlSerializationTest < ActiveModel::TestCase assert_match %r{}, xml assert_match %r{}, 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{
}, xml + assert_match %r{}, 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{
}, xml + assert_match %r{}, 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{}, 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{
}, xml + assert_match %r{}, 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{
}, xml + assert_match %r{}, 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{
}, xml + assert_match %r{}, xml + end end -- cgit v1.2.3