mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 15:23:06 +00:00
@@ -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?
|
||||
|
||||
Reference in New Issue
Block a user