Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add configuration options for :dasherize and :camelize calls to Hash#…

…to_xml

People using ActiveResource & REST to integrate with other systems need to be able to control the default dasherize behavior of Hash.to_xml.
Currently there is no test for a default value, but existing code asssumes it's true. This patch adds tests for the default value and adds
mattr_accessor to ActiveSupport for :dasherize_xml and :camelize_xml. These module attributes set the defaults for :dasherize and :camelize in
rename_keys inside Hash#to_xml. The tests have been changed to separate out the testing of the parameter options for :camelize
and :dasherize so that we only test one thing at a time. We also test default values for :camelize_xml and :dasherize_xml.

The module attribute dasherize_xml is set to true in this patch to maintain existing code. But at some point in the future it should be set to
false because Hash#to_xml probably should not set underscores to dashes by default.

Changed documentation on ActiveResource#to_xml to correctly describe the behaviour of Hash#to_xml. The previous documentation said that
the default for :dasherize was false, in fact it was and still is true, but we now have a way to change the default. I've also added
documentation for the :camelize option.

Signed-off-by: Michael Koziarski <michael@koziarski.com>
  • Loading branch information...
commit 7bf9bf3dd6b4c4d78214917f0877536d222098bb 1 parent 6dec3c4
@JohnSmall JohnSmall authored NZKoz committed
View
11 activeresource/lib/active_resource/base.rb
@@ -832,7 +832,7 @@ def exists?
!new? && self.class.exists?(to_param, :params => prefix_options)
end
- # A method to convert the the resource to an XML string.
+ # Converts the resource to an XML string representation.
#
# ==== Options
# The +options+ parameter is handed off to the +to_xml+ method on each
@@ -841,7 +841,14 @@ def exists?
#
# * <tt>:indent</tt> - Set the indent level for the XML output (default is +2+).
# * <tt>:dasherize</tt> - Boolean option to determine whether or not element names should
- # replace underscores with dashes (default is <tt>false</tt>).
+ # replace underscores with dashes. Default is <tt>true</tt>. The default can be set to <tt>false</tt>
+ # by setting the module attribute <tt>ActiveSupport.dasherize_xml = false</tt> in an initializer. Because save
+ # uses this method, and there are no options on save, then you will have to set the default if you don't
+ # want underscores in element names to become dashes when the resource is saved. This is important when
+ # integrating with non-Rails applications.
+ # * <tt>:camelize</tt> - Boolean option to determine whether or not element names should be converted
+ # to camel case, e.g some_name to SomeName. Default is <tt>false</tt>. Like <tt>:dasherize</tt> you can
+ # change the default by setting the module attribute <tt>ActiveSupport.camelise_xml = true</tt> in an initializer.
# * <tt>:skip_instruct</tt> - Toggle skipping the +instruct!+ call on the XML builder
# that generates the XML declaration (default is <tt>false</tt>).
#
View
15 activesupport/lib/active_support/core_ext/hash/conversions.rb
@@ -1,6 +1,14 @@
require 'date'
+require 'active_support/core_ext/module/attribute_accessors'
module ActiveSupport #:nodoc:
+ # these accessors are here because people using ActiveResource and REST to integrate with other systems
+ # have to be able to control the default behavior of rename_key. dasherize_xml is set to true to emulate
+ # existing behavior. In a future version it should be set to false by default.
+ mattr_accessor :dasherize_xml
+ mattr_accessor :camelize_xml
+ self.dasherize_xml = true
+ self.camelize_xml = false
module CoreExtensions #:nodoc:
module Hash #:nodoc:
module Conversions
@@ -143,10 +151,11 @@ def to_xml(options = {})
end
def rename_key(key, options = {})
- camelize = options.has_key?(:camelize) && options[:camelize]
- dasherize = !options.has_key?(:dasherize) || options[:dasherize]
+ camelize = options.has_key?(:camelize) ? options[:camelize] : ActiveSupport.camelize_xml
+ dasherize = options.has_key?(:dasherize) ? options[:dasherize] : ActiveSupport.dasherize_xml
key = key.camelize if camelize
- dasherize ? key.dasherize : key
+ key = key.dasherize if dasherize
+ key
end
module ClassMethods
View
66 activesupport/test/core_ext/hash_ext_test.rb
@@ -403,29 +403,87 @@ def setup
@xml_options = { :root => :person, :skip_instruct => true, :indent => 0 }
end
+ def test_default_values_for_rename_keys
+ assert_equal true,ActiveSupport.dasherize_xml
+ assert_equal false,ActiveSupport.camelize_xml
+ end
+
def test_one_level
xml = { :name => "David", :street => "Paulina" }.to_xml(@xml_options)
assert_equal "<person>", xml.first(8)
assert xml.include?(%(<street>Paulina</street>))
assert xml.include?(%(<name>David</name>))
end
-
+ # we add :camelize => false because otherwise we'd be accidentally testing the default value for :camelize
def test_one_level_dasherize_false
- xml = { :name => "David", :street_name => "Paulina" }.to_xml(@xml_options.merge(:dasherize => false))
+ xml = { :name => "David", :street_name => "Paulina" }.to_xml(@xml_options.merge(:dasherize => false,:camelize=>false))
assert_equal "<person>", xml.first(8)
assert xml.include?(%(<street_name>Paulina</street_name>))
assert xml.include?(%(<name>David</name>))
end
def test_one_level_dasherize_true
- xml = { :name => "David", :street_name => "Paulina" }.to_xml(@xml_options.merge(:dasherize => true))
+ xml = { :name => "David", :street_name => "Paulina" }.to_xml(@xml_options.merge(:dasherize => true,:camelize=>false))
+ assert_equal "<person>", xml.first(8)
+ assert xml.include?(%(<street-name>Paulina</street-name>))
+ assert xml.include?(%(<name>David</name>))
+ end
+
+ def test_one_level_dasherize_default_false
+ current_default = ActiveSupport.dasherize_xml
+ ActiveSupport.dasherize_xml = false
+ xml = { :name => "David", :street_name => "Paulina" }.to_xml(@xml_options.merge(:camelize=>false))
+ assert_equal "<person>", xml.first(8)
+ assert xml.include?(%(<street_name>Paulina</street_name>))
+ assert xml.include?(%(<name>David</name>))
+ ensure
+ ActiveSupport.dasherize_xml = current_default
+ end
+
+ def test_one_level_dasherize_default_true
+ current_default = ActiveSupport.dasherize_xml
+ ActiveSupport.dasherize_xml = true
+ xml = { :name => "David", :street_name => "Paulina" }.to_xml(@xml_options.merge(:camelize=>false))
assert_equal "<person>", xml.first(8)
assert xml.include?(%(<street-name>Paulina</street-name>))
assert xml.include?(%(<name>David</name>))
+ ensure
+ ActiveSupport.dasherize_xml = current_default
end
def test_one_level_camelize_true
- xml = { :name => "David", :street_name => "Paulina" }.to_xml(@xml_options.merge(:camelize => true))
+ xml = { :name => "David", :street_name => "Paulina" }.to_xml(@xml_options.merge(:camelize => true,:dasherize => false))
+ assert_equal "<Person>", xml.first(8)
+ assert xml.include?(%(<StreetName>Paulina</StreetName>))
+ assert xml.include?(%(<Name>David</Name>))
+ end
+
+ #camelize=>false is already tested above
+
+ def test_one_level_camelize_default_false
+ current_default = ActiveSupport.camelize_xml
+ ActiveSupport.camelize_xml = false
+ xml = { :name => "David", :street_name => "Paulina" }.to_xml(@xml_options.merge(:dasherize => false))
+ assert_equal "<person>", xml.first(8)
+ assert xml.include?(%(<street_name>Paulina</street_name>))
+ assert xml.include?(%(<name>David</name>))
+ ensure
+ ActiveSupport.camelize_xml = current_default
+ end
+
+ def test_one_level_camelize_default_true
+ current_default = ActiveSupport.camelize_xml
+ ActiveSupport.camelize_xml = true
+ xml = { :name => "David", :street_name => "Paulina" }.to_xml(@xml_options.merge(:dasherize => false))
+ assert_equal "<Person>", xml.first(8)
+ assert xml.include?(%(<StreetName>Paulina</StreetName>))
+ assert xml.include?(%(<Name>David</Name>))
+ ensure
+ ActiveSupport.camelize_xml = current_default
+ end
+
+ def test_one_level_camelize_true_dasherize_true
+ xml = { :name => "David", :street_name => "Paulina" }.to_xml(@xml_options.merge(:dasherize => true,:camelize=>true))
assert_equal "<Person>", xml.first(8)
assert xml.include?(%(<StreetName>Paulina</StreetName>))
assert xml.include?(%(<Name>David</Name>))
Please sign in to comment.
Something went wrong with that request. Please try again.