aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorBruce Krysiak <bruce@socialpl.us>2008-12-08 16:38:04 -0800
committerMichael Koziarski <michael@koziarski.com>2008-12-10 20:28:05 +0100
commitaa5cdb0d47fb5484bfdde8244df7efeb2175bf3a (patch)
treeb0b0309c7f6e700c70e2fbbe3185724479a7434f /activerecord
parent96b815d7e81b9cef912ef94c96dca923fe43b0ba (diff)
downloadrails-aa5cdb0d47fb5484bfdde8244df7efeb2175bf3a.tar.gz
rails-aa5cdb0d47fb5484bfdde8244df7efeb2175bf3a.tar.bz2
rails-aa5cdb0d47fb5484bfdde8244df7efeb2175bf3a.zip
Added a :camelize option to ActiveRecord and Hash to_xml serialization and from_xml deserialization
Signed-off-by: Michael Koziarski <michael@koziarski.com>
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/serializers/xml_serializer.rb23
-rw-r--r--activerecord/test/cases/xml_serialization_test.rb7
2 files changed, 23 insertions, 7 deletions
diff --git a/activerecord/lib/active_record/serializers/xml_serializer.rb b/activerecord/lib/active_record/serializers/xml_serializer.rb
index d171b742f5..4749823b94 100644
--- a/activerecord/lib/active_record/serializers/xml_serializer.rb
+++ b/activerecord/lib/active_record/serializers/xml_serializer.rb
@@ -23,11 +23,12 @@ module ActiveRecord #:nodoc:
# </topic>
#
# This behavior can be controlled with <tt>:only</tt>, <tt>:except</tt>,
- # <tt>:skip_instruct</tt>, <tt>:skip_types</tt> and <tt>:dasherize</tt>.
+ # <tt>:skip_instruct</tt>, <tt>:skip_types</tt>, <tt>:dasherize</tt> and <tt>:camelize</tt> .
# The <tt>:only</tt> and <tt>:except</tt> options are the same as for the
# +attributes+ method. The default is to dasherize all column names, but you
- # can disable this setting <tt>:dasherize</tt> to +false+. To not have the
- # column type included in the XML output set <tt>:skip_types</tt> to +true+.
+ # can disable this setting <tt>:dasherize</tt> to +false+. Setting <tt>:camelize</tt>
+ # to +true+ will camelize all column names - this also overrides <tt>:dasherize</tt>.
+ # To not have the column type included in the XML output set <tt>:skip_types</tt> to +true+.
#
# For instance:
#
@@ -178,13 +179,22 @@ module ActiveRecord #:nodoc:
def root
root = (options[:root] || @record.class.to_s.underscore).to_s
- dasherize? ? root.dasherize : root
+ reformat_name(root)
end
def dasherize?
!options.has_key?(:dasherize) || options[:dasherize]
end
+ def camelize?
+ options.has_key?(:camelize) && options[:camelize]
+ end
+
+ def reformat_name(name)
+ name = name.camelize if camelize?
+ dasherize? ? name.dasherize : name
+ end
+
def serializable_attributes
serializable_attribute_names.collect { |name| Attribute.new(name, @record) }
end
@@ -212,7 +222,7 @@ module ActiveRecord #:nodoc:
def add_tag(attribute)
builder.tag!(
- dasherize? ? attribute.name.dasherize : attribute.name,
+ reformat_name(attribute.name),
attribute.value.to_s,
attribute.decorations(!options[:skip_types])
)
@@ -220,8 +230,7 @@ module ActiveRecord #:nodoc:
def add_associations(association, records, opts)
if records.is_a?(Enumerable)
- tag = association.to_s
- tag = tag.dasherize if dasherize?
+ tag = reformat_name(association.to_s)
if records.empty?
builder.tag!(tag, :type => :array)
else
diff --git a/activerecord/test/cases/xml_serialization_test.rb b/activerecord/test/cases/xml_serialization_test.rb
index 63f48865cc..39c6ea820d 100644
--- a/activerecord/test/cases/xml_serialization_test.rb
+++ b/activerecord/test/cases/xml_serialization_test.rb
@@ -31,6 +31,13 @@ class XmlSerializationTest < ActiveRecord::TestCase
assert_match %r{<created_at}, @xml
end
+ def test_should_allow_camelized_tags
+ @xml = Contact.new.to_xml :root => 'xml_contact', :camelize => true
+ assert_match %r{^<XmlContact>}, @xml
+ assert_match %r{</XmlContact>$}, @xml
+ assert_match %r{<CreatedAt}, @xml
+ end
+
def test_should_include_yielded_additions
@xml = Contact.new.to_xml do |xml|
xml.creator "David"