Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Allow ActiveRecord XML Serialization to use CamelCase names

#1534

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.

Reported by Bruce Krysiak · December 9th, 2008 @ 01:07 AM

State: committed
Milestone: 2.x
Assigned to: nobody
Importance: none

Activity

  1. Michael Koziarski
    Michael Koziarski
    • 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

    December 10th, 2008 @ 07:28 PM

  2. Michael Koziarski
    Michael Koziarski

    changeset is aa5cdb0d47fb5484bfdde8244df7efeb2175bf3a

    December 10th, 2008 @ 07:32 PM