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