Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow customizing the type of polymorphic association #2484

Open
wants to merge 1 commit into
base: 0-10-stable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/active_model/serializer/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
27 changes: 22 additions & 5 deletions test/adapter/polymorphic_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 =
{
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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