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
commit 6f07a0297e
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?

View File

@ -1169,4 +1169,99 @@ class SerializerTest < ActiveModel::TestCase
}
}, actual)
end
def test_raises_an_error_when_a_child_serializer_includes_associations_when_the_source_doesnt
attachment_serializer = Class.new(ActiveModel::Serializer) do
attributes :name
end
fruit_serializer = Class.new(ActiveModel::Serializer) do
embed :ids, :include => true
has_one :attachment, :serializer => attachment_serializer
attribute :color
end
banana_class = Class.new Model do
def self.to_s
'banana'
end
def attachment
@attributes[:attachment]
end
define_method :active_model_serializer do
fruit_serializer
end
end
strawberry_class = Class.new Model do
def self.to_s
'strawberry'
end
def attachment
@attributes[:attachment]
end
define_method :active_model_serializer do
fruit_serializer
end
end
smoothie = Class.new do
attr_reader :base, :flavor
def initialize(base, flavor)
@base, @flavor = base, flavor
end
end
smoothie_serializer = Class.new(ActiveModel::Serializer) do
root false
embed :ids, :include => true
has_one :base, :polymorphic => true
has_one :flavor, :polymorphic => true
end
banana_attachment = Attachment.new({
:name => 'banana_blending.md',
:id => 3,
})
strawberry_attachment = Attachment.new({
:name => 'strawberry_cleaning.doc',
:id => 4
})
banana = banana_class.new :color => "yellow", :id => 1, :attachment => banana_attachment
strawberry = strawberry_class.new :color => "red", :id => 2, :attachment => strawberry_attachment
smoothie = smoothie_serializer.new(smoothie.new(banana, strawberry))
assert_raise ActiveModel::Serializer::IncludeError do
smoothie.as_json
end
end
def tests_includes_does_not_include_nil_polymoprhic_associations
post_serializer = Class.new(ActiveModel::Serializer) do
root :post
embed :ids, :include => true
has_one :author, :polymorphic => true
attributes :title
end
post = Post.new(:title => 'Foo')
actual = post_serializer.new(post).as_json
assert_equal({
:post => {
:title => 'Foo',
:author => nil
}
}, actual)
end
end