Add include_data :if_sideloaded (#1931)

For JSONAPI, `include_data` currently means, "should we populate the
'data'" key for this relationship. Current options are true/false.

This adds the `:if_sideloaded` option. This means "only
populate the 'data' key when we are sideloading this relationship." This
is because 'data' is often only relevant to sideloading, and causes a
database hit.

Addresses https://github.com/rails-api/active_model_serializers/issues/1555
This commit is contained in:
Lee Richmond
2016-09-25 12:57:19 -04:00
committed by L. Preston Sego III
parent 6ed499f38e
commit 2145540795
6 changed files with 204 additions and 28 deletions

View File

@@ -83,7 +83,8 @@ module ActiveModel
# +default_include_directive+ config value when not provided)
# @return [Enumerator<Association>]
#
def associations(include_directive = ActiveModelSerializers.default_include_directive)
def associations(include_directive = ActiveModelSerializers.default_include_directive, include_slice = nil)
include_slice ||= include_directive
return unless object
Enumerator.new do |y|
@@ -91,7 +92,8 @@ module ActiveModel
next if reflection.excluded?(self)
key = reflection.options.fetch(:key, reflection.name)
next unless include_directive.key?(key)
y.yield reflection.build_association(self, instance_options)
y.yield reflection.build_association(self, instance_options, include_slice)
end
end
end

View File

@@ -30,6 +30,7 @@ module ActiveModel
# Make JSON API top-level jsonapi member opt-in
# ref: http://jsonapi.org/format/#document-top-level
config.jsonapi_include_toplevel_object = false
config.include_data_default = true
config.schema_path = 'test/support/schemas'
end

View File

@@ -37,7 +37,7 @@ module ActiveModel
def initialize(*)
super
@_links = {}
@_include_data = true
@_include_data = Serializer.config.include_data_default
@_meta = nil
end
@@ -69,17 +69,15 @@ module ActiveModel
# Blog.find(object.blog_id)
# end
# end
def value(serializer)
def value(serializer, include_slice)
@object = serializer.object
@scope = serializer.scope
if block
block_value = instance_exec(serializer, &block)
if block_value != :nil
block_value
elsif @_include_data
serializer.read_attribute_for_serialization(name)
end
block_value = instance_exec(serializer, &block) if block
return unless include_data?(include_slice)
if block && block_value != :nil
block_value
else
serializer.read_attribute_for_serialization(name)
end
@@ -106,11 +104,11 @@ module ActiveModel
#
# @api private
#
def build_association(parent_serializer, parent_serializer_options)
association_value = value(parent_serializer)
def build_association(parent_serializer, parent_serializer_options, include_slice = {})
association_value = value(parent_serializer, include_slice)
reflection_options = options.dup
serializer_class = parent_serializer.class.serializer_for(association_value, reflection_options)
reflection_options[:include_data] = @_include_data
reflection_options[:include_data] = include_data?(include_slice)
reflection_options[:links] = @_links
reflection_options[:meta] = @_meta
@@ -137,6 +135,14 @@ module ActiveModel
private
def include_data?(include_slice)
if @_include_data == :if_sideloaded
include_slice.key?(name)
else
@_include_data
end
end
def serializer_options(parent_serializer, parent_serializer_options, reflection_options)
serializer = reflection_options.fetch(:serializer, nil)