diff --git a/test/adapter/json/has_many_test.rb b/test/adapter/json/has_many_test.rb index 19fe16cd..5e10358a 100644 --- a/test/adapter/json/has_many_test.rb +++ b/test/adapter/json/has_many_test.rb @@ -17,6 +17,8 @@ module ActiveModel @second_comment.post = @post @blog = Blog.new(id: 1, name: "My Blog!!") @post.blog = @blog + @tag = Tag.new(id: 1, name: "#hash_tag") + @post.tags = [@tag] @serializer = PostSerializer.new(@post) @adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer) @@ -28,9 +30,14 @@ module ActiveModel {id: 2, body: 'ZOMG ANOTHER COMMENT'} ], @adapter.serializable_hash[:post][:comments]) end + + def test_has_many_with_no_serializer + serializer = PostWithTagsSerializer.new(@post) + adapter = ActiveModel::Serializer::Adapter::Json.new(serializer) + assert_includes(adapter.as_json, :tags) + end end end end end end - diff --git a/test/adapter/json_api/has_many_test.rb b/test/adapter/json_api/has_many_test.rb index a544fc80..9104e839 100644 --- a/test/adapter/json_api/has_many_test.rb +++ b/test/adapter/json_api/has_many_test.rb @@ -27,6 +27,8 @@ module ActiveModel @blog.articles = [@post] @post.blog = @blog @post_without_comments.blog = nil + @tag = Tag.new(id: 1, name: "#hash_tag") + @post.tags = [@tag] @serializer = PostSerializer.new(@post) @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer) @@ -103,6 +105,12 @@ module ActiveModel } assert_equal expected, actual end + + def test_has_many_with_no_serializer + serializer = PostWithTagsSerializer.new(@post) + adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) + assert_includes(adapter.serializable_hash, :tags) + end end end end diff --git a/test/fixtures/poro.rb b/test/fixtures/poro.rb index ee1913ec..4f0b513a 100644 --- a/test/fixtures/poro.rb +++ b/test/fixtures/poro.rb @@ -76,6 +76,7 @@ Role = Class.new(Model) User = Class.new(Model) Location = Class.new(Model) Place = Class.new(Model) +Tag = Class.new(Model) Comment = Class.new(Model) do # Uses a custom non-time-based cache key def cache_key @@ -224,6 +225,12 @@ PostPreviewSerializer = Class.new(ActiveModel::Serializer) do belongs_to :author, serializer: AuthorPreviewSerializer end +PostWithTagsSerializer = Class.new(ActiveModel::Serializer) do + attributes :id + + has_many :tags +end + Spam::UnrelatedLinkSerializer = Class.new(ActiveModel::Serializer) do attributes :id end diff --git a/test/serializers/associations_test.rb b/test/serializers/associations_test.rb index ab481de7..d87ebae7 100644 --- a/test/serializers/associations_test.rb +++ b/test/serializers/associations_test.rb @@ -29,8 +29,10 @@ module ActiveModel @author.roles = [] @blog = Blog.new({ name: 'AMS Blog' }) @post = Post.new({ title: 'New Post', body: 'Body' }) + @tag = Tag.new({name: '#hashtagged'}) @comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' }) @post.comments = [@comment] + @post.tags = [@tag] @post.blog = @blog @comment.post = @post @comment.author = nil @@ -65,6 +67,12 @@ module ActiveModel end end + def test_has_many_with_no_serializer + PostWithTagsSerializer.new(@post).each_association do |name, serializer, options| + puts "The line above will crash this test" + end + end + def test_serializer_options_are_passed_into_associations_serializers @post_serializer.each_association do |name, association| if name == :comments