Specify type of singular assication during serialization #8352

Merged
merged 1 commit into from Nov 28, 2012

Conversation

Projects
None yet
3 participants
Member

steveklabnik commented Nov 28, 2012

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

Member

steveklabnik commented Nov 28, 2012

@rafaelfranca I did a different refactoring than you did, mine is even simpler, I think. I used the 1.9 hash syntax, let me know if you want it in 1.8 so you can cherry-pick a backport.

activemodel/lib/active_model/serializers/xml.rb
- records.to_xml(merged_options)
+
+ unless records.class.to_s.underscore == association.to_s
+ merged_options[:type] = records.class.name
@rafaelfranca

rafaelfranca Nov 28, 2012

Owner

wrong, indentation

Owner

rafaelfranca commented Nov 28, 2012

Why you removed all these tests?

activemodel/test/cases/serializers/xml_serialization_test.rb
- assert_match %r{<address>}, xml
- assert_match %r{<apt-number type="integer">}, xml
+ test "association with sti" do
+ xml = @contact.to_xml(:include => :contact)
@rafaelfranca

rafaelfranca Nov 28, 2012

Owner

Ruby 1.9 syntax

Member

steveklabnik commented Nov 28, 2012

Because I suck at git. Ugh. I'll fix that now.

activemodel/test/cases/serializers/xml_serialization_test.rb
@@ -238,7 +241,6 @@ def to_ary
xml = @contact.to_xml :dasherize => false, :include => :address, :indent => 0
assert_match %r{<apt_number type="integer">}, xml
end
-
@rafaelfranca

rafaelfranca Nov 28, 2012

Owner

This line should not be removed

@steveklabnik

steveklabnik Nov 28, 2012

Member

Already fixed

Owner

rafaelfranca commented Nov 28, 2012

Sorry, I forgot to ask the CHANGELOG entry

Member

steveklabnik commented Nov 28, 2012

Doing it now. I figured I'd wait till you were happy with the patch itself.

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
Member

steveklabnik commented Nov 28, 2012

Done.

rafaelfranca added a commit that referenced this pull request Nov 28, 2012

Merge pull request #8352 from steveklabnik/update_7858
Specify type of singular assication during serialization

@rafaelfranca rafaelfranca merged commit 4c99d08 into rails:master Nov 28, 2012

Owner

rafaelfranca commented Nov 28, 2012

❤️ 💚 💙 💛 💜

Member

steveklabnik commented Nov 28, 2012

🤘 ❤️

Member

steveklabnik commented Nov 28, 2012

Should I open a PR to backport are are you going to?

Contributor

cliochris commented Nov 29, 2012

Thank you guys very much for your work here. I appreciate it.

Member

steveklabnik commented Nov 29, 2012

Thanks for kicking it off!

rafaelfranca added a commit that referenced this pull request Nov 29, 2012

Merge pull request #8352 from steveklabnik/update_7858
Specify type of singular assication during serialization
Conflicts:
	activemodel/CHANGELOG.md
	activemodel/test/cases/serializers/xml_serialization_test.rb
Owner

rafaelfranca commented Nov 29, 2012

I already did yesterday but forget to push. Done now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment