ActiveModel Xml Serialization patch #1058

Closed
lighthouse-import opened this Issue May 16, 2011 · 1 comment

2 participants

@lighthouse-import

Imported from Lighthouse. Original ticket at: http://rails.lighthouseapp.com/projects/8994/tickets/6744
Created by jimmiesh - 2011-04-26 01:22:27 UTC

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>

``` xml

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:

``` ruby
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.

@joshk joshk added a commit that referenced this issue May 18, 2011
@jimherz jimherz Attributes with :string type should not be given the type passed in m…
…odel serialization options. Closes #1058
fbf9965
@josevalim josevalim pushed a commit that closed this issue May 18, 2011
@jimherz jimherz Attributes with :string type should not be given the type passed in m…
…odel serialization options. Closes #1058
fbf9965
@josevalim josevalim closed this in fbf9965 May 18, 2011
@josevalim josevalim closed this in 200708e May 18, 2011
@jake3030 jake3030 pushed a commit to jake3030/rails that referenced this issue Jun 28, 2011
@lifo lifo Fix duplicate test name [#1058 state:resolved] [Dave Rothlisberger, P…
…ratik Naik]
462c75b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment