This project is archived and is in readonly mode.
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
-
Michael Koziarski December 10th, 2008 @ 07:28 PM
- Tag changed from activerecord, activesupport, hash, serialization, to_xml, xml to activerecord, activesupport, hash, patch, serialization, to_xml, xml
- State changed from new to committed
-
Michael Koziarski December 10th, 2008 @ 07:32 PM
changeset is aa5cdb0d47fb5484bfdde8244df7efeb2175bf3a
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>