From b626ec8f145c72a1df599a961548aa2863232141 Mon Sep 17 00:00:00 2001 From: Robbie Pitts Date: Sun, 11 Jan 2015 16:10:02 -0500 Subject: [PATCH] Spec for linked resource type name demodulization --- test/adapter/json_api/belongs_to_test.rb | 2 +- test/adapter/json_api/has_many_test.rb | 9 +++++++-- test/adapter/json_api/linked_test.rb | 15 +++++++++++++++ test/fixtures/poro.rb | 15 +++++++++++++++ 4 files changed, 38 insertions(+), 3 deletions(-) diff --git a/test/adapter/json_api/belongs_to_test.rb b/test/adapter/json_api/belongs_to_test.rb index 578896b1..30397a30 100644 --- a/test/adapter/json_api/belongs_to_test.rb +++ b/test/adapter/json_api/belongs_to_test.rb @@ -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] diff --git a/test/adapter/json_api/has_many_test.rb b/test/adapter/json_api/has_many_test.rb index c824d847..34ba7669 100644 --- a/test/adapter/json_api/has_many_test.rb +++ b/test/adapter/json_api/has_many_test.rb @@ -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 diff --git a/test/adapter/json_api/linked_test.rb b/test/adapter/json_api/linked_test.rb index 8e896460..efdcbd49 100644 --- a/test/adapter/json_api/linked_test.rb +++ b/test/adapter/json_api/linked_test.rb @@ -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 diff --git a/test/fixtures/poro.rb b/test/fixtures/poro.rb index 6b30387c..3c306841 100644 --- a/test/fixtures/poro.rb +++ b/test/fixtures/poro.rb @@ -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