Skip to content

Commit a89e78c

Browse files
caomaniabf4
authored andcommitted
Allow referencing sideloaded include by key. (#2136)
* If a `key` is set on the reflection use the `key` instead of `name`. This ensures that associations with a key set are still included.
1 parent a5ab62f commit a89e78c

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
Breaking changes:
66

77
Features:
8+
- [#2136](https://github.com/rails-api/active_model_serializers/pull/2136) Enable inclusion of sideloaded relationship objects by `key`. (@caomania)
89

910
- [#2021](https://github.com/rails-api/active_model_serializers/pull/2021) ActiveModelSerializers::Model#attributes. Originally in [#1982](https://github.com/rails-api/active_model_serializers/pull/1982). (@bf4)
1011
- [#2130](https://github.com/rails-api/active_model_serializers/pull/2130) Allow serialized ID to be overwritten for belongs-to relationships. (@greysteil)

lib/active_model/serializer/reflection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def collection?
140140
def include_data?(include_slice)
141141
include_data_setting = options[:include_data_setting]
142142
case include_data_setting
143-
when :if_sideloaded then include_slice.key?(name)
143+
when :if_sideloaded then include_slice.key?(options.fetch(:key, name))
144144
when true then true
145145
when false then false
146146
else fail ArgumentError, "Unknown include_data_setting '#{include_data_setting.inspect}'"

test/adapter/json_api/include_data_if_sideloaded_test.rb

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Adapter
66
class JsonApi
77
class IncludeParamTest < ActiveSupport::TestCase
88
IncludeParamAuthor = Class.new(::Model) do
9-
associations :tags, :posts
9+
associations :tags, :posts, :roles
1010
end
1111

1212
class CustomCommentLoader
@@ -51,14 +51,19 @@ class IncludeParamAuthorSerializer < ActiveModel::Serializer
5151
include_data :if_sideloaded
5252
IncludeParamAuthorSerializer.comment_loader.all
5353
end
54+
has_many :roles, key: :granted_roles do
55+
include_data :if_sideloaded
56+
end
5457
end
5558

5659
def setup
5760
IncludeParamAuthorSerializer.comment_loader = Class.new(CustomCommentLoader).new
5861
@tag = Tag.new(id: 1337, name: 'mytag')
62+
@role = Role.new(id: 1337, name: 'myrole')
5963
@author = IncludeParamAuthor.new(
6064
id: 1337,
61-
tags: [@tag]
65+
tags: [@tag],
66+
roles: [@role]
6267
)
6368
end
6469

@@ -105,6 +110,31 @@ def test_sideloads_included
105110
assert_equal(expected, hash[:included])
106111
end
107112

113+
def test_sideloads_included_when_using_key
114+
expected = [
115+
{
116+
id: '1337',
117+
type: 'roles',
118+
attributes: {
119+
name: 'myrole',
120+
description: nil,
121+
slug: 'myrole-1337'
122+
},
123+
relationships: {
124+
author: { data: nil }
125+
}
126+
}
127+
]
128+
129+
hash = result(include: :granted_roles)
130+
assert_equal(expected, hash[:included])
131+
end
132+
133+
def test_sideloads_not_included_when_using_name_when_key_defined
134+
hash = result(include: :roles)
135+
assert_nil(hash[:included])
136+
end
137+
108138
def test_nested_relationship
109139
expected = {
110140
data: [

0 commit comments

Comments
 (0)