mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Simplify association call signature
This commit is contained in:
parent
c32558248a
commit
fb45aa4838
@ -91,8 +91,9 @@ module ActiveModel
|
|||||||
|
|
||||||
self.options = {}
|
self.options = {}
|
||||||
|
|
||||||
def initialize(name, options={})
|
def initialize(name, source, options={})
|
||||||
@name = name
|
@name = name
|
||||||
|
@source = source
|
||||||
@options = options
|
@options = options
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -108,6 +109,10 @@ module ActiveModel
|
|||||||
option(:serializer)
|
option(:serializer)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def source_serializer
|
||||||
|
@source
|
||||||
|
end
|
||||||
|
|
||||||
def key
|
def key
|
||||||
option(:key) || @name
|
option(:key) || @name
|
||||||
end
|
end
|
||||||
@ -116,17 +121,21 @@ module ActiveModel
|
|||||||
option(:name) || @name
|
option(:name) || @name
|
||||||
end
|
end
|
||||||
|
|
||||||
def associated_object(serializer)
|
def associated_object
|
||||||
option(:value) || serializer.send(name)
|
option(:value) || source_serializer.send(name)
|
||||||
|
end
|
||||||
|
|
||||||
|
def scope
|
||||||
|
source_serializer.scope
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def find_serializable(object, scope, serializer)
|
def find_serializable(object)
|
||||||
if target_serializer
|
if target_serializer
|
||||||
target_serializer.new(object, scope, serializer.options)
|
target_serializer.new(object, scope, source_serializer.options)
|
||||||
elsif object.respond_to?(:active_model_serializer) && (ams = object.active_model_serializer)
|
elsif object.respond_to?(:active_model_serializer) && (ams = object.active_model_serializer)
|
||||||
ams.new(object, scope, serializer.options)
|
ams.new(object, scope, source_serializer.options)
|
||||||
else
|
else
|
||||||
object
|
object
|
||||||
end
|
end
|
||||||
@ -136,18 +145,18 @@ module ActiveModel
|
|||||||
class HasMany < Config #:nodoc:
|
class HasMany < Config #:nodoc:
|
||||||
alias plural_key key
|
alias plural_key key
|
||||||
|
|
||||||
def serialize(serializer, scope)
|
def serialize
|
||||||
associated_object(serializer).map do |item|
|
associated_object.map do |item|
|
||||||
find_serializable(item, scope, serializer).serializable_hash
|
find_serializable(item).serializable_hash
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
alias serialize_many serialize
|
alias serialize_many serialize
|
||||||
|
|
||||||
def serialize_ids(serializer, scope)
|
def serialize_ids
|
||||||
# Use pluck or select_columns if available
|
# Use pluck or select_columns if available
|
||||||
# return collection.ids if collection.respond_to?(:ids)
|
# return collection.ids if collection.respond_to?(:ids)
|
||||||
|
|
||||||
associated_object(serializer).map do |item|
|
associated_object.map do |item|
|
||||||
item.read_attribute_for_serialization(:id)
|
item.read_attribute_for_serialization(:id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -158,19 +167,19 @@ module ActiveModel
|
|||||||
key.to_s.pluralize.to_sym
|
key.to_s.pluralize.to_sym
|
||||||
end
|
end
|
||||||
|
|
||||||
def serialize(serializer, scope)
|
def serialize
|
||||||
object = associated_object(serializer)
|
object = associated_object
|
||||||
object && find_serializable(object, scope, serializer).serializable_hash
|
object && find_serializable(object).serializable_hash
|
||||||
end
|
end
|
||||||
|
|
||||||
def serialize_many(serializer, scope)
|
def serialize_many
|
||||||
object = associated_object(serializer)
|
object = associated_object
|
||||||
value = object && find_serializable(object, scope, serializer).serializable_hash
|
value = object && find_serializable(object).serializable_hash
|
||||||
value ? [value] : []
|
value ? [value] : []
|
||||||
end
|
end
|
||||||
|
|
||||||
def serialize_ids(serializer, scope)
|
def serialize_ids
|
||||||
if object = associated_object(serializer)
|
if object = associated_object
|
||||||
object.read_attribute_for_serialization(:id)
|
object.read_attribute_for_serialization(:id)
|
||||||
else
|
else
|
||||||
nil
|
nil
|
||||||
@ -279,7 +288,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
associations = _associations.inject({}) do |hash, (attr,association_class)|
|
associations = _associations.inject({}) do |hash, (attr,association_class)|
|
||||||
association = association_class.new(attr)
|
association = association_class.new(attr, self)
|
||||||
|
|
||||||
model_association = klass.reflect_on_association(association.name)
|
model_association = klass.reflect_on_association(association.name)
|
||||||
hash.merge association.key => { model_association.macro => model_association.name }
|
hash.merge association.key => { model_association.macro => model_association.name }
|
||||||
@ -371,16 +380,16 @@ module ActiveModel
|
|||||||
Associations::HasOne
|
Associations::HasOne
|
||||||
end
|
end
|
||||||
|
|
||||||
association = association_class.new(name, options)
|
association = association_class.new(name, self, options)
|
||||||
|
|
||||||
if embed == :ids
|
if embed == :ids
|
||||||
node[association.key] = association.serialize_ids(self, scope)
|
node[association.key] = association.serialize_ids
|
||||||
|
|
||||||
if root_embed
|
if root_embed
|
||||||
merge_association hash, association.plural_key, association.serialize_many(self, scope)
|
merge_association hash, association.plural_key, association.serialize_many
|
||||||
end
|
end
|
||||||
elsif embed == :objects
|
elsif embed == :objects
|
||||||
node[association.key] = association.serialize(self, scope)
|
node[association.key] = association.serialize
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user