From c0f6eb37f89131cc92238ed075de7ce27cd90b9c Mon Sep 17 00:00:00 2001 From: Klaas Pieter Annema Date: Wed, 13 Nov 2024 09:31:15 +0100 Subject: [PATCH] Allow customizing the type of polymorphic association --- lib/active_model/serializer/association.rb | 2 +- test/adapter/polymorphic_test.rb | 27 ++++++++++++++++++---- test/fixtures/active_record.rb | 10 ++++++++ 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/lib/active_model/serializer/association.rb b/lib/active_model/serializer/association.rb index 7aeee338a..395573c12 100644 --- a/lib/active_model/serializer/association.rb +++ b/lib/active_model/serializer/association.rb @@ -58,7 +58,7 @@ def serializable_hash(adapter_options, adapter_instance) serialization = association_serializer.serializable_hash(adapter_options, {}, adapter_instance) if polymorphic? && serialization - polymorphic_type = association_object.class.name.underscore + polymorphic_type = association_serializer.json_key || association_object.class.name.underscore serialization = { type: polymorphic_type, polymorphic_type.to_sym => serialization } end diff --git a/test/adapter/polymorphic_test.rb b/test/adapter/polymorphic_test.rb index 48bcd979e..558e5165f 100644 --- a/test/adapter/polymorphic_test.rb +++ b/test/adapter/polymorphic_test.rb @@ -12,8 +12,8 @@ class PolymorphicTest < ActiveSupport::TestCase @picture.imageable = @employee end - def serialization(resource, adapter = :attributes) - serializable(resource, adapter: adapter, serializer: PolymorphicBelongsToSerializer).as_json + def serialization(resource, adapter: :attributes, serializer: PolymorphicBelongsToSerializer) + serializable(resource, adapter: adapter, serializer: serializer).as_json end def tag_serialization(adapter = :attributes) @@ -40,6 +40,23 @@ def test_attributes_serialization assert_equal(expected, serialization(@picture)) end + def test_polymorphic_belongs_to_with_custom_type + expected = + { + id: 1, + title: 'headshot-1.jpg', + imageable: { + type: 'custom', + custom: { + id: 42, + name: 'Zoop Zoopler' + } + } + } + + assert_equal(expected, serialization(@picture, serializer: PolymorphicBelongsToSerializerWithCustomBelongsToType)) + end + def test_attributes_serialization_without_polymorphic_association expected = { @@ -97,7 +114,7 @@ def test_json_serialization } } - assert_equal(expected, serialization(@picture, :json)) + assert_equal(expected, serialization(@picture, adapter: :json)) end def test_json_serialization_without_polymorphic_association @@ -111,7 +128,7 @@ def test_json_serialization_without_polymorphic_association } simple_picture = Picture.new(id: 2, title: 'headshot-2.jpg') - assert_equal(expected, serialization(simple_picture, :json)) + assert_equal(expected, serialization(simple_picture, adapter: :json)) end def test_json_serialization_with_polymorphic_has_many @@ -165,7 +182,7 @@ def test_json_api_serialization } } - assert_equal(expected, serialization(@picture, :json_api)) + assert_equal(expected, serialization(@picture, adapter: :json_api)) end def test_json_api_serialization_with_polymorphic_belongs_to diff --git a/test/fixtures/active_record.rb b/test/fixtures/active_record.rb index 132a43b16..9afd1b5ca 100644 --- a/test/fixtures/active_record.rb +++ b/test/fixtures/active_record.rb @@ -113,3 +113,13 @@ class PolymorphicBelongsToSerializer < ActiveModel::Serializer attributes :id, :title belongs_to :imageable, serializer: PolymorphicHasManySerializer, polymorphic: true end + +class PolymorphicHasManySerializerWithCustomType < ActiveModel::Serializer + attributes :id, :name + + type :custom +end +class PolymorphicBelongsToSerializerWithCustomBelongsToType < ActiveModel::Serializer + attributes :id, :title + belongs_to :imageable, serializer: PolymorphicHasManySerializerWithCustomType, polymorphic: true +end