mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Simplify Relationship
This commit is contained in:
parent
d66e272994
commit
abb15b9622
@ -438,14 +438,7 @@ module ActiveModelSerializers
|
|||||||
allow_wildcard: true
|
allow_wildcard: true
|
||||||
)
|
)
|
||||||
serializer.associations(include_directive).each_with_object({}) do |association, hash|
|
serializer.associations(include_directive).each_with_object({}) do |association, hash|
|
||||||
hash[association.key] = Relationship.new(
|
hash[association.key] = Relationship.new(serializer, instance_options, association).as_json
|
||||||
serializer,
|
|
||||||
association.serializer,
|
|
||||||
instance_options,
|
|
||||||
options: association.options,
|
|
||||||
links: association.links,
|
|
||||||
meta: association.meta
|
|
||||||
).as_json
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -6,17 +6,20 @@ module ActiveModelSerializers
|
|||||||
# {http://jsonapi.org/format/#document-links Document Links}
|
# {http://jsonapi.org/format/#document-links Document Links}
|
||||||
# {http://jsonapi.org/format/#document-resource-object-linkage Document Resource Relationship Linkage}
|
# {http://jsonapi.org/format/#document-resource-object-linkage Document Resource Relationship Linkage}
|
||||||
# {http://jsonapi.org/format/#document-meta Document Meta}
|
# {http://jsonapi.org/format/#document-meta Document Meta}
|
||||||
def initialize(parent_serializer, serializer, serializable_resource_options, args = {})
|
def initialize(parent_serializer, serializable_resource_options, association)
|
||||||
|
serializer = association.serializer
|
||||||
|
options = association.options
|
||||||
|
links = association.links
|
||||||
|
meta = association.meta
|
||||||
@object = parent_serializer.object
|
@object = parent_serializer.object
|
||||||
@scope = parent_serializer.scope
|
@scope = parent_serializer.scope
|
||||||
@association_options = args.fetch(:options, {})
|
@association_options = options || {}
|
||||||
@serializable_resource_options = serializable_resource_options
|
@serializable_resource_options = serializable_resource_options
|
||||||
@data = data_for(serializer)
|
@data = data_for(serializer)
|
||||||
@links = args.fetch(:links, {}).each_with_object({}) do |(key, value), hash|
|
@links = (links || {}).each_with_object({}) do |(key, value), hash|
|
||||||
result = Link.new(parent_serializer, value).as_json
|
result = Link.new(parent_serializer, value).as_json
|
||||||
hash[key] = result if result
|
hash[key] = result if result
|
||||||
end
|
end
|
||||||
meta = args.fetch(:meta, nil)
|
|
||||||
@meta = meta.respond_to?(:call) ? parent_serializer.instance_eval(&meta) : meta
|
@meta = meta.respond_to?(:call) ? parent_serializer.instance_eval(&meta) : meta
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -150,9 +150,23 @@ module ActiveModelSerializers
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def test_relationship(expected, params = {})
|
def test_relationship(expected, test_options = {})
|
||||||
parent_serializer = AuthorSerializer.new(@author)
|
parent_serializer = AuthorSerializer.new(@author)
|
||||||
relationship = Relationship.new(parent_serializer, @serializer, nil, params)
|
|
||||||
|
serializable_resource_options = {} # adapter.instance_options
|
||||||
|
|
||||||
|
meta = test_options.delete(:meta)
|
||||||
|
options = test_options.delete(:options)
|
||||||
|
links = test_options.delete(:links)
|
||||||
|
association_serializer = @serializer
|
||||||
|
if association_serializer && association_serializer.object
|
||||||
|
association_name = association_serializer.json_key.to_sym
|
||||||
|
association = ::ActiveModel::Serializer::Association.new(association_name, association_serializer, options, links, meta)
|
||||||
|
else
|
||||||
|
association = ::ActiveModel::Serializer::Association.new(:association_name_not_used, association, options, links, meta)
|
||||||
|
end
|
||||||
|
|
||||||
|
relationship = Relationship.new(parent_serializer, serializable_resource_options, association)
|
||||||
assert_equal(expected, relationship.as_json)
|
assert_equal(expected, relationship.as_json)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user