Merge pull request #97 from twinturbo/include-bug

Include Related Bugs
This commit is contained in:
José Valim
2012-07-21 00:36:04 -07:00
2 changed files with 122 additions and 1 deletions

View File

@@ -108,6 +108,18 @@ module ActiveModel
# end
#
class Serializer
class IncludeError < StandardError
attr_reader :source, :association
def initialize(source, association)
@source, @association = source, association
end
def to_s
"Cannot serialize #{association} when #{source} does not have a root!"
end
end
module Associations #:nodoc:
class Config #:nodoc:
class_attribute :options
@@ -182,6 +194,10 @@ module ActiveModel
option(:include, source_serializer._root_embed)
end
def embeddable?
!associated_object.nil?
end
protected
def find_serializable(object)
@@ -216,6 +232,14 @@ module ActiveModel
end
class HasOne < Config #:nodoc:
def embeddable?
if polymorphic? && associated_object.nil?
false
else
true
end
end
def polymorphic?
option :polymorphic
end
@@ -502,7 +526,9 @@ module ActiveModel
if association.embed_ids?
node[association.key] = association.serialize_ids
if association.embed_in_root?
if association.embed_in_root? && hash.nil?
raise IncludeError.new(self.class, association.name)
elsif association.embed_in_root? && association.embeddable?
merge_association hash, association.root, association.serialize_many, unique_values
end
elsif association.embed_objects?