Extract IncludeTree. (#1685)

This commit is contained in:
Lucas Hosseini
2016-05-28 16:07:11 +02:00
committed by L. Preston Sego III
parent f2cb497fe3
commit f48fd2a327
14 changed files with 49 additions and 227 deletions

View File

@@ -78,18 +78,18 @@ module ActiveModel
end
end
# @param [IncludeTree] include_tree (defaults to the
# default_includes config value when not provided)
# @param [JSONAPI::IncludeDirective] include_directive (defaults to the
# +default_include_directive+ config value when not provided)
# @return [Enumerator<Association>]
#
def associations(include_tree = ActiveModelSerializers.default_include_tree)
def associations(include_directive = ActiveModelSerializers.default_include_directive)
return unless object
Enumerator.new do |y|
self.class._reflections.each do |reflection|
next if reflection.excluded?(self)
key = reflection.options.fetch(:key, reflection.name)
next unless include_tree.key?(key)
next unless include_directive.key?(key)
y.yield reflection.build_association(self, instance_options)
end
end

View File

@@ -163,10 +163,10 @@ module ActiveModel
# Read cache from cache_store
# @return [Hash]
def cache_read_multi(collection_serializer, adapter_instance, include_tree)
def cache_read_multi(collection_serializer, adapter_instance, include_directive)
return {} if ActiveModelSerializers.config.cache_store.blank?
keys = object_cache_keys(collection_serializer, adapter_instance, include_tree)
keys = object_cache_keys(collection_serializer, adapter_instance, include_directive)
return {} if keys.blank?
@@ -176,15 +176,15 @@ module ActiveModel
# Find all cache_key for the collection_serializer
# @param serializers [ActiveModel::Serializer::CollectionSerializer]
# @param adapter_instance [ActiveModelSerializers::Adapter::Base]
# @param include_tree [ActiveModel::Serializer::IncludeTree]
# @param include_directive [JSONAPI::IncludeDirective]
# @return [Array] all cache_key of collection_serializer
def object_cache_keys(collection_serializer, adapter_instance, include_tree)
def object_cache_keys(collection_serializer, adapter_instance, include_directive)
cache_keys = []
collection_serializer.each do |serializer|
cache_keys << object_cache_key(serializer, adapter_instance)
serializer.associations(include_tree).each do |association|
serializer.associations(include_directive).each do |association|
if association.serializer.respond_to?(:each)
association.serializer.each do |sub_serializer|
cache_keys << object_cache_key(sub_serializer, adapter_instance)