mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Spec for linked resource type name demodulization
This commit is contained in:
parent
90023b1af7
commit
b626ec8f14
@ -64,7 +64,7 @@ module ActiveModel
|
||||
assert_equal({comments: [], author: nil}, adapter.serializable_hash[:posts][:links])
|
||||
end
|
||||
|
||||
def test_include_type_for_association_when_is_different_than_name
|
||||
def test_include_type_for_association_when_different_than_name
|
||||
serializer = BlogSerializer.new(@blog)
|
||||
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
||||
links = adapter.serializable_hash[:blogs][:links]
|
||||
|
||||
@ -78,10 +78,15 @@ module ActiveModel
|
||||
assert_nil adapter.serializable_hash[:linked]
|
||||
end
|
||||
|
||||
def test_include_type_for_association_when_is_different_than_name
|
||||
def test_include_type_for_association_when_different_than_name
|
||||
serializer = BlogSerializer.new(@blog)
|
||||
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
||||
assert_equal({type: "posts", ids: ["1"]}, adapter.serializable_hash[:blogs][:links][:articles])
|
||||
actual = adapter.serializable_hash[:blogs][:links][:articles]
|
||||
expected = {
|
||||
type: "posts",
|
||||
ids: ["1"]
|
||||
}
|
||||
assert_equal(expected, actual)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -138,6 +138,21 @@ module ActiveModel
|
||||
}
|
||||
assert_equal expected, @adapter.serializable_hash[:linked]
|
||||
end
|
||||
|
||||
def test_ignore_model_namespace_for_linked_resource_type
|
||||
spammy_post = Post.new(id: 123)
|
||||
spammy_post.related = [Spam::UnrelatedLink.new(id: 456)]
|
||||
serializer = SpammyPostSerializer.new(spammy_post)
|
||||
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
|
||||
links = adapter.serializable_hash[:posts][:links]
|
||||
expected = {
|
||||
related: {
|
||||
type: 'unrelated_links',
|
||||
ids: ['456']
|
||||
}
|
||||
}
|
||||
assert_equal expected, links
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
15
test/fixtures/poro.rb
vendored
15
test/fixtures/poro.rb
vendored
@ -47,6 +47,8 @@ Author = Class.new(Model)
|
||||
Bio = Class.new(Model)
|
||||
Blog = Class.new(Model)
|
||||
Role = Class.new(Model)
|
||||
module Spam; end
|
||||
Spam::UnrelatedLink = Class.new(Model)
|
||||
|
||||
PostSerializer = Class.new(ActiveModel::Serializer) do
|
||||
attributes :title, :body, :id
|
||||
@ -56,6 +58,15 @@ PostSerializer = Class.new(ActiveModel::Serializer) do
|
||||
url :comments
|
||||
end
|
||||
|
||||
SpammyPostSerializer = Class.new(ActiveModel::Serializer) do
|
||||
attributes :id
|
||||
has_many :related
|
||||
|
||||
def self.root_name
|
||||
'posts'
|
||||
end
|
||||
end
|
||||
|
||||
CommentSerializer = Class.new(ActiveModel::Serializer) do
|
||||
attributes :id, :body
|
||||
|
||||
@ -123,3 +134,7 @@ PostPreviewSerializer = Class.new(ActiveModel::Serializer) do
|
||||
has_many :comments, serializer: CommentPreviewSerializer
|
||||
belongs_to :author, serializer: AuthorPreviewSerializer
|
||||
end
|
||||
|
||||
Spam::UnrelatedLinkSerializer = Class.new(ActiveModel::Serializer) do
|
||||
attributes :id
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user