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.

ActiveModel Xml Serialization patch

#6744

When passing a :type option to to_xml, the passed type is propagated to all string attributes in the ActiveModel. For instance:


class Person
  include ActiveModel::Serializers::Xml
  extend ActiveModel::Naming

  attr_accessor :attributes
  def initialize(attributes)
    @attributes = attributes
  end
end    

mp = Person.new('foo' => 'string', 'bar' => 1)
puts mp.to_xml(:type => 'Person')

produces:


<?xml version="1.0" encoding="UTF-8"?>
<person type="Person">
  <foo type="Person">string</foo>
  <bar type="integer">1</bar>
</person>

We ran into this because ActiveRecord adds a type option when serializing has_many associations where the association class does not match the association name. For instance:

has_many :cars, :class_name => 'Automobile'

In this case, we wanted the type attribute on each car in the xml, but we don't want it on the car's string attributes :).

Please consider the attached patch for inclusion in the next release of Rails. Thanks.

Reported by jimmiesh · April 26th, 2011 @ 02:22 AM

State: new
Milestone: none
Assigned to: nobody
Importance: none

Activity

No comments yet.