This project is archived and is in readonly mode.

#1534 ✓committed
Bruce Krysiak

Allow ActiveRecord XML Serialization to use CamelCase names

Reported by Bruce Krysiak | December 9th, 2008 @ 01:07 AM | in 2.x

ActiveRecord's to_xml method has a :dasherize option to control name formatting - this patch allows passing a :camelize option to CamelCase names as well.


>> c = Contact.new({:my_name => "Bob"})
=> #<Contact id: nil, my_name: "Bob", created_at: nil, updated_at: nil>
>> c.to_xml :camelize => true
=> "<?xml version="1.0" encoding="UTF-8"?>
<Contact>
  <CreatedAt type="datetime" nil="true"></CreatedAt>
  <MyName>Bob</MyName>
  <UpdatedAt type="datetime" nil="true"></UpdatedAt>
</Contact>
"
>>

This works just like the :dasherize option. :camelize is false by default - setting it to true will also override any :dasherize settings (since it's not clear that you would ever want a Camel-Cased name).

Docs and tests are in the patch - to make this work I applied the change into ActiveSupport's Hash Conversions (for ActiveRecord.from_xml to work) and applied it to Hash.to_xml (in order to get tests to work).

I needed this on a project which needed CamelCased XML output names so I figured there's probably others who need the same thing. This is my first patch so please let me know if I'm missing anything else I need to think about/include.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Attachments

Pages