mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
Encapsulate more concerns in the associations.
This commit is contained in:
parent
fb45aa4838
commit
573be9302d
@ -97,11 +97,13 @@ module ActiveModel
|
|||||||
@options = options
|
@options = options
|
||||||
end
|
end
|
||||||
|
|
||||||
def option(key)
|
def option(key, default=nil)
|
||||||
if @options.key?(key)
|
if @options.key?(key)
|
||||||
@options[key]
|
@options[key]
|
||||||
elsif self.class.options[key]
|
elsif self.class.options.key?(key)
|
||||||
self.class.options[key]
|
self.class.options[key]
|
||||||
|
else
|
||||||
|
default
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -126,7 +128,19 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def scope
|
def scope
|
||||||
source_serializer.scope
|
option(:scope) || source_serializer.scope
|
||||||
|
end
|
||||||
|
|
||||||
|
def embed_ids?
|
||||||
|
option(:embed, source_serializer._embed) == :ids
|
||||||
|
end
|
||||||
|
|
||||||
|
def embed_objects?
|
||||||
|
option(:embed, source_serializer._embed) == :objects
|
||||||
|
end
|
||||||
|
|
||||||
|
def embed_in_root?
|
||||||
|
option(:include, source_serializer._root_embed)
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
@ -363,13 +377,9 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def include!(name, options={})
|
def include!(name, options={})
|
||||||
embed = options[:embed] || _embed
|
|
||||||
root_embed = options[:include] || _root_embed
|
|
||||||
hash = options[:hash] || @options[:hash]
|
hash = options[:hash] || @options[:hash]
|
||||||
node = options[:node]
|
node = options[:node]
|
||||||
value = options[:value]
|
value = options[:value]
|
||||||
serializer = options[:serializer]
|
|
||||||
scope = options[:scope] || self.scope
|
|
||||||
|
|
||||||
association_class =
|
association_class =
|
||||||
if klass = _associations[name]
|
if klass = _associations[name]
|
||||||
@ -382,13 +392,13 @@ module ActiveModel
|
|||||||
|
|
||||||
association = association_class.new(name, self, options)
|
association = association_class.new(name, self, options)
|
||||||
|
|
||||||
if embed == :ids
|
if association.embed_ids?
|
||||||
node[association.key] = association.serialize_ids
|
node[association.key] = association.serialize_ids
|
||||||
|
|
||||||
if root_embed
|
if association.embed_in_root?
|
||||||
merge_association hash, association.plural_key, association.serialize_many
|
merge_association hash, association.plural_key, association.serialize_many
|
||||||
end
|
end
|
||||||
elsif embed == :objects
|
elsif association.embed_objects?
|
||||||
node[association.key] = association.serialize
|
node[association.key] = association.serialize
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user