From 87eadd09b9a988bc1d9b30d9a501ef7e3fc6bb87 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Tue, 14 May 2013 16:20:51 -0700 Subject: [PATCH] serialize_ids doesn't use source serializer and it's object --- lib/active_model/serializer.rb | 7 ++++++- lib/active_model/serializer/associations.rb | 23 +++++++-------------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index c1cd49900..8aa8b7192 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -384,7 +384,12 @@ def include!(name, options={}) association = association_class.new(name, self, options) if association.embed_ids? - node[association.key] = association.serialize_ids + node[association.key] = + if options[:embed_key] || self.respond_to?(name) || !self.object.respond_to?(association.id_key) + association.serialize_ids + else + self.object.read_attribute_for_serialization(association.id_key) + end if association.embed_in_root? && hash.nil? raise IncludeError.new(self.class, association.name) diff --git a/lib/active_model/serializer/associations.rb b/lib/active_model/serializer/associations.rb index 6b4460e6b..667774fb7 100644 --- a/lib/active_model/serializer/associations.rb +++ b/lib/active_model/serializer/associations.rb @@ -100,12 +100,8 @@ def serializables end def serialize_ids - if !options[:embed_key] && !source_serializer.respond_to?(@name.to_s) && source_serializer.object.respond_to?(id_key) - source_serializer.object.read_attribute_for_serialization(id_key) - else - associated_object.map do |item| - item.read_attribute_for_serialization(embed_key) - end + associated_object.map do |item| + item.read_attribute_for_serialization(embed_key) end end end @@ -179,21 +175,16 @@ def serializables end def serialize_ids - if polymorphic? - if associated_object + if associated_object + id = associated_object.read_attribute_for_serialization(embed_key) + if polymorphic? { :type => polymorphic_key, - :id => associated_object.read_attribute_for_serialization(embed_key) + :id => id } else - nil + id end - elsif !options[:embed_key] && !source_serializer.respond_to?(@name.to_s) && source_serializer.object.respond_to?(id_key) - source_serializer.object.read_attribute_for_serialization(id_key) - elsif associated_object - associated_object.read_attribute_for_serialization(embed_key) - else - nil end end end