Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Specify type of singular association during serialization

When serialising a class, specify the type of any singular associations, if
necessary. Rails already correctly specifies the :type of any enumerable
association (e.g. a has_many association), but made no attempt to do so for
non-enumerables (e.g. a has_one association).
We must specify the :type of any STI association. A has_one
association to a class which uses single-table inheritance is an example of
this type of association.

Fixes #7471
  • Loading branch information...
commit 9504b44cf60c1d69519cef32465f46a73719bc17 1 parent 3e965e2
Steve Klabnik authored October 05, 2012
2  activemodel/CHANGELOG.md
Source Rendered
... ...
@@ -1,5 +1,7 @@
1 1
 ## Rails 4.0.0 (unreleased) ##
2 2
 
  3
+*   Specify type of singular association during serialization *Steve Klabnik*
  4
+
3 5
 *   Fixed length validator to correctly handle nil values. Fixes #7180.
4 6
 
5 7
     *Michal Zima*
7  activemodel/lib/active_model/serializers/xml.rb
@@ -149,7 +149,12 @@ def add_associations(association, records, opts)
149 149
             end
150 150
           else
151 151
             merged_options[:root] = association.to_s
152  
-            records.to_xml(merged_options)
  152
+
  153
+            unless records.class.to_s.underscore == association.to_s
  154
+              merged_options[:type] = records.class.name
  155
+            end
  156
+
  157
+            records.to_xml merged_options
153 158
           end
154 159
         end
155 160
 
12  activemodel/test/cases/serializers/xml_serialization_test.rb
@@ -6,12 +6,12 @@
6 6
 class Contact
7 7
   include ActiveModel::Serializers::Xml
8 8
 
9  
-  attr_accessor :address, :friends
  9
+  attr_accessor :address, :friends, :contact
10 10
 
11 11
   remove_method :attributes if method_defined?(:attributes)
12 12
 
13 13
   def attributes
14  
-    instance_values.except("address", "friends")
  14
+    instance_values.except("address", "friends", "contact")
15 15
   end
16 16
 end
17 17
 
@@ -56,6 +56,9 @@ def setup
56 56
     @contact.address.zip = 11111
57 57
     @contact.address.apt_number = 35
58 58
     @contact.friends = [Contact.new, Contact.new]
  59
+    @related_contact = SerializableContact.new
  60
+    @related_contact.name = "related"
  61
+    @contact.contact = @related_contact
59 62
   end
60 63
 
61 64
   test "should serialize default root" do
@@ -256,4 +259,9 @@ def to_ary
256 259
     assert_match %r{<address>}, xml
257 260
     assert_match %r{<apt-number type="integer">}, xml
258 261
   end
  262
+
  263
+  test "association with sti" do
  264
+    xml = @contact.to_xml(include: :contact)
  265
+    assert xml.include?(%(<contact type="SerializableContact">))
  266
+  end
259 267
 end

0 notes on commit 9504b44

Please sign in to comment.
Something went wrong with that request. Please try again.