From a89e78c6555d31df6f7ac9bfac693dff87eb0680 Mon Sep 17 00:00:00 2001 From: Christian Date: Thu, 18 May 2017 13:59:14 -0700 Subject: [PATCH] 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. --- CHANGELOG.md | 1 + lib/active_model/serializer/reflection.rb | 2 +- .../include_data_if_sideloaded_test.rb | 34 +++++++++++++++++-- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9313e61f..7f0fe113 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ Breaking changes: Features: +- [#2136](https://github.com/rails-api/active_model_serializers/pull/2136) Enable inclusion of sideloaded relationship objects by `key`. (@caomania) - [#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) - [#2130](https://github.com/rails-api/active_model_serializers/pull/2130) Allow serialized ID to be overwritten for belongs-to relationships. (@greysteil) diff --git a/lib/active_model/serializer/reflection.rb b/lib/active_model/serializer/reflection.rb index 2e5cc2a1..a64aa3c4 100644 --- a/lib/active_model/serializer/reflection.rb +++ b/lib/active_model/serializer/reflection.rb @@ -140,7 +140,7 @@ module ActiveModel def include_data?(include_slice) include_data_setting = options[:include_data_setting] case include_data_setting - when :if_sideloaded then include_slice.key?(name) + when :if_sideloaded then include_slice.key?(options.fetch(:key, name)) when true then true when false then false else fail ArgumentError, "Unknown include_data_setting '#{include_data_setting.inspect}'" diff --git a/test/adapter/json_api/include_data_if_sideloaded_test.rb b/test/adapter/json_api/include_data_if_sideloaded_test.rb index c0da9488..059ed41b 100644 --- a/test/adapter/json_api/include_data_if_sideloaded_test.rb +++ b/test/adapter/json_api/include_data_if_sideloaded_test.rb @@ -6,7 +6,7 @@ module ActiveModel class JsonApi class IncludeParamTest < ActiveSupport::TestCase IncludeParamAuthor = Class.new(::Model) do - associations :tags, :posts + associations :tags, :posts, :roles end class CustomCommentLoader @@ -51,14 +51,19 @@ module ActiveModel include_data :if_sideloaded IncludeParamAuthorSerializer.comment_loader.all end + has_many :roles, key: :granted_roles do + include_data :if_sideloaded + end end def setup IncludeParamAuthorSerializer.comment_loader = Class.new(CustomCommentLoader).new @tag = Tag.new(id: 1337, name: 'mytag') + @role = Role.new(id: 1337, name: 'myrole') @author = IncludeParamAuthor.new( id: 1337, - tags: [@tag] + tags: [@tag], + roles: [@role] ) end @@ -105,6 +110,31 @@ module ActiveModel assert_equal(expected, hash[:included]) end + def test_sideloads_included_when_using_key + expected = [ + { + id: '1337', + type: 'roles', + attributes: { + name: 'myrole', + description: nil, + slug: 'myrole-1337' + }, + relationships: { + author: { data: nil } + } + } + ] + + hash = result(include: :granted_roles) + assert_equal(expected, hash[:included]) + end + + def test_sideloads_not_included_when_using_name_when_key_defined + hash = result(include: :roles) + assert_nil(hash[:included]) + end + def test_nested_relationship expected = { data: [