Skip to content

Commit 0ab4c89

Browse files
author
Lee Richmond
committed
Add include_data :via_include_parameter
For JSONAPI, `include_data` currently means, "should we populate the 'data'" key for this relationship. Current options are true/false. This adds the `:via_include_parameter` 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 #1555
1 parent a52189c commit 0ab4c89

File tree

4 files changed

+202
-28
lines changed

4 files changed

+202
-28
lines changed

lib/active_model/serializer/associations.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,17 @@ def associate(reflection)
8282
# +default_include_directive+ config value when not provided)
8383
# @return [Enumerator<Association>]
8484
#
85-
def associations(include_directive = ActiveModelSerializers.default_include_directive)
85+
def associations(include_directive = ActiveModelSerializers.default_include_directive, include_slice = nil)
86+
include_slice ||= include_directive
8687
return unless object
8788

8889
Enumerator.new do |y|
8990
self.class._reflections.each do |reflection|
9091
next if reflection.excluded?(self)
9192
key = reflection.options.fetch(:key, reflection.name)
9293
next unless include_directive.key?(key)
93-
y.yield reflection.build_association(self, instance_options)
94+
95+
y.yield reflection.build_association(self, instance_options, include_slice)
9496
end
9597
end
9698
end

lib/active_model/serializer/reflection.rb

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,17 @@ def include_data(value = true)
6969
# Blog.find(object.blog_id)
7070
# end
7171
# end
72-
def value(serializer)
72+
def value(serializer, include_slice)
7373
@object = serializer.object
7474
@scope = serializer.scope
7575

76-
if block
77-
block_value = instance_exec(serializer, &block)
78-
if block_value != :nil
79-
block_value
80-
elsif @_include_data
81-
serializer.read_attribute_for_serialization(name)
82-
end
83-
else
76+
block_value = instance_exec(serializer, &block) if block
77+
return unless include_data?(include_slice)
78+
79+
if [nil, :nil].include?(block_value)
8480
serializer.read_attribute_for_serialization(name)
81+
else
82+
block_value
8583
end
8684
end
8785

@@ -106,11 +104,11 @@ def value(serializer)
106104
#
107105
# @api private
108106
#
109-
def build_association(subject, parent_serializer_options)
110-
association_value = value(subject)
107+
def build_association(subject, parent_serializer_options, include_slice = {})
108+
association_value = value(subject, include_slice)
111109
reflection_options = options.dup
112110
serializer_class = subject.class.serializer_for(association_value, reflection_options)
113-
reflection_options[:include_data] = @_include_data
111+
reflection_options[:include_data] = include_data?(include_slice)
114112

115113
if serializer_class
116114
begin
@@ -134,6 +132,14 @@ def build_association(subject, parent_serializer_options)
134132

135133
private
136134

135+
def include_data?(include_slice)
136+
if @_include_data == :via_include_parameter
137+
include_slice.key?(name)
138+
else
139+
@_include_data
140+
end
141+
end
142+
137143
def serializer_options(subject, parent_serializer_options, reflection_options)
138144
serializer = reflection_options.fetch(:serializer, nil)
139145

lib/active_model_serializers/adapter/json_api.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -235,17 +235,17 @@ def resource_objects_for(serializers)
235235
@primary = []
236236
@included = []
237237
@resource_identifiers = Set.new
238-
serializers.each { |serializer| process_resource(serializer, true) }
238+
serializers.each { |serializer| process_resource(serializer, true, @include_directive) }
239239
serializers.each { |serializer| process_relationships(serializer, @include_directive) }
240240

241241
[@primary, @included]
242242
end
243243

244-
def process_resource(serializer, primary)
244+
def process_resource(serializer, primary, include_slice = {})
245245
resource_identifier = ResourceIdentifier.new(serializer, instance_options).as_json
246246
return false unless @resource_identifiers.add?(resource_identifier)
247247

248-
resource_object = resource_object_for(serializer)
248+
resource_object = resource_object_for(serializer, include_slice)
249249
if primary
250250
@primary << resource_object
251251
else
@@ -255,21 +255,21 @@ def process_resource(serializer, primary)
255255
true
256256
end
257257

258-
def process_relationships(serializer, include_directive)
259-
serializer.associations(include_directive).each do |association|
260-
process_relationship(association.serializer, include_directive[association.key])
258+
def process_relationships(serializer, include_slice)
259+
serializer.associations(include_slice).each do |association|
260+
process_relationship(association.serializer, include_slice[association.key])
261261
end
262262
end
263263

264-
def process_relationship(serializer, include_directive)
264+
def process_relationship(serializer, include_slice)
265265
if serializer.respond_to?(:each)
266-
serializer.each { |s| process_relationship(s, include_directive) }
266+
serializer.each { |s| process_relationship(s, include_slice) }
267267
return
268268
end
269269
return unless serializer && serializer.object
270-
return unless process_resource(serializer, false)
270+
return unless process_resource(serializer, false, include_slice)
271271

272-
process_relationships(serializer, include_directive)
272+
process_relationships(serializer, include_slice)
273273
end
274274

275275
# {http://jsonapi.org/format/#document-resource-object-attributes Document Resource Object Attributes}
@@ -293,7 +293,7 @@ def attributes_for(serializer, fields)
293293
end
294294

295295
# {http://jsonapi.org/format/#document-resource-objects Document Resource Objects}
296-
def resource_object_for(serializer)
296+
def resource_object_for(serializer, include_slice = {})
297297
resource_object = serializer.fetch(self) do
298298
resource_object = ResourceIdentifier.new(serializer, instance_options).as_json
299299

@@ -304,7 +304,7 @@ def resource_object_for(serializer)
304304
end
305305

306306
requested_associations = fieldset.fields_for(resource_object[:type]) || '*'
307-
relationships = relationships_for(serializer, requested_associations)
307+
relationships = relationships_for(serializer, requested_associations, include_slice)
308308
resource_object[:relationships] = relationships if relationships.any?
309309

310310
links = links_for(serializer)
@@ -432,12 +432,12 @@ def resource_object_for(serializer)
432432
# id: 'required-id',
433433
# meta: meta
434434
# }.reject! {|_,v| v.nil? }
435-
def relationships_for(serializer, requested_associations)
435+
def relationships_for(serializer, requested_associations, include_slice)
436436
include_directive = JSONAPI::IncludeDirective.new(
437437
requested_associations,
438438
allow_wildcard: true
439439
)
440-
serializer.associations(include_directive).each_with_object({}) do |association, hash|
440+
serializer.associations(include_directive, include_slice).each_with_object({}) do |association, hash|
441441
hash[association.key] = Relationship.new(
442442
serializer,
443443
association.serializer,
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
require 'test_helper'
2+
3+
module ActiveModel
4+
class Serializer
5+
module Adapter
6+
class JsonApi
7+
class IncludeParamTest < ActiveSupport::TestCase
8+
IncludeParamAuthor = Class.new(::Model)
9+
10+
class CustomCommentLoader
11+
def all
12+
[{ foo: 'bar' }]
13+
end
14+
end
15+
16+
class TagSerializer < ActiveModel::Serializer
17+
attributes :id, :name
18+
end
19+
20+
class IncludeParamAuthorSerializer < ActiveModel::Serializer
21+
class_attribute :comment_loader
22+
23+
has_many :tags, serializer: TagSerializer do
24+
link :self, '//example.com/link_author/relationships/tags'
25+
include_data :via_include_parameter
26+
end
27+
28+
has_many :unlinked_tags, serializer: TagSerializer do
29+
include_data :via_include_parameter
30+
end
31+
32+
has_many :posts, serializer: PostWithTagsSerializer do
33+
include_data :via_include_parameter
34+
end
35+
has_many :locations do
36+
include_data :via_include_parameter
37+
end
38+
has_many :comments do
39+
include_data :via_include_parameter
40+
IncludeParamAuthorSerializer.comment_loader.all
41+
end
42+
end
43+
44+
def setup
45+
IncludeParamAuthorSerializer.comment_loader = Class.new(CustomCommentLoader).new
46+
@tag = Tag.new(id: 1337, name: 'mytag')
47+
@author = IncludeParamAuthor.new(
48+
id: 1337,
49+
tags: [@tag]
50+
)
51+
end
52+
53+
def test_relationship_not_loaded_when_not_included
54+
expected = {
55+
links: {
56+
self: '//example.com/link_author/relationships/tags'
57+
}
58+
}
59+
60+
@author.define_singleton_method(:read_attribute_for_serialization) do |attr|
61+
fail 'should not be called' if attr == :tags
62+
super(attr)
63+
end
64+
65+
assert_relationship(:tags, expected)
66+
end
67+
68+
def test_relationship_included
69+
expected = {
70+
data: [
71+
{
72+
id: '1337',
73+
type: 'tags'
74+
}
75+
],
76+
links: {
77+
self: '//example.com/link_author/relationships/tags'
78+
}
79+
}
80+
81+
assert_relationship(:tags, expected, include: :tags)
82+
end
83+
84+
def test_sideloads_included
85+
expected = [
86+
{
87+
id: '1337',
88+
type: 'tags',
89+
attributes: { name: 'mytag' }
90+
}
91+
]
92+
hash = result(include: :tags)
93+
assert_equal(expected, hash[:included])
94+
end
95+
96+
def test_nested_relationship
97+
expected = {
98+
data: [
99+
{
100+
id: '1337',
101+
type: 'tags'
102+
}
103+
],
104+
links: {
105+
self: '//example.com/link_author/relationships/tags'
106+
}
107+
}
108+
109+
expected_no_data = {
110+
links: {
111+
self: '//example.com/link_author/relationships/tags'
112+
}
113+
}
114+
115+
assert_relationship(:tags, expected, include: [:tags, { posts: :tags }])
116+
117+
@author.define_singleton_method(:read_attribute_for_serialization) do |attr|
118+
fail 'should not be called' if attr == :tags
119+
super(attr)
120+
end
121+
122+
assert_relationship(:tags, expected_no_data, include: { posts: :tags })
123+
end
124+
125+
def test_include_params_with_no_block
126+
@author.define_singleton_method(:read_attribute_for_serialization) do |attr|
127+
fail 'should not be called' if attr == :locations
128+
super(attr)
129+
end
130+
131+
expected = {}
132+
133+
assert_relationship(:locations, expected)
134+
end
135+
136+
def test_block_relationship
137+
expected = {
138+
data: [
139+
{ 'foo' => 'bar' }
140+
]
141+
}
142+
143+
assert_relationship(:comments, expected, include: [:comments])
144+
end
145+
146+
def test_node_not_included_when_no_link
147+
expected = nil
148+
assert_relationship(:unlinked_tags, expected)
149+
end
150+
151+
private
152+
153+
def result(opts)
154+
opts = { adapter: :json_api }.merge(opts)
155+
serializable(@author, opts).serializable_hash
156+
end
157+
158+
def assert_relationship(relationship_name, expected, opts = {})
159+
hash = result(opts)
160+
assert_equal(expected, hash[:data][:relationships][relationship_name])
161+
end
162+
end
163+
end
164+
end
165+
end
166+
end

0 commit comments

Comments
 (0)