mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Breaking change: - Adapters now inherit Adapter::Base - 'Adapter' is now a module, no longer a class Why? - using a class as a namespace that you also inherit from is complicated and circular at time i.e. buggy (see https://github.com/rails-api/active_model_serializers/pull/1177) - The class methods on Adapter aren't necessarily related to the instance methods, they're more Adapter functions - named `Base` because it's a Rails-ism - It helps to isolate and highlight what the Adapter interface actually is
48 lines
1.8 KiB
Ruby
48 lines
1.8 KiB
Ruby
require 'test_helper'
|
|
|
|
module ActiveModel
|
|
class Serializer
|
|
module Adapter
|
|
class Json
|
|
class BelongsToTest < Minitest::Test
|
|
def setup
|
|
@post = Post.new(id: 42, title: 'New Post', body: 'Body')
|
|
@anonymous_post = Post.new(id: 43, title: 'Hello!!', body: 'Hello, world!!')
|
|
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
|
@post.comments = [@comment]
|
|
@anonymous_post.comments = []
|
|
@comment.post = @post
|
|
@comment.author = nil
|
|
@anonymous_post.author = nil
|
|
@blog = Blog.new(id: 1, name: 'My Blog!!')
|
|
@post.blog = @blog
|
|
@anonymous_post.blog = nil
|
|
|
|
@serializer = CommentSerializer.new(@comment)
|
|
@adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
|
|
ActionController::Base.cache_store.clear
|
|
end
|
|
|
|
def test_includes_post
|
|
assert_equal({ id: 42, title: 'New Post', body: 'Body' }, @adapter.serializable_hash[:comment][:post])
|
|
end
|
|
|
|
def test_include_nil_author
|
|
serializer = PostSerializer.new(@anonymous_post)
|
|
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
|
|
|
assert_equal({ post: { title: 'Hello!!', body: 'Hello, world!!', id: 43, comments: [], blog: { id: 999, name: 'Custom blog' }, author: nil } }, adapter.serializable_hash)
|
|
end
|
|
|
|
def test_include_nil_author_with_specified_serializer
|
|
serializer = PostPreviewSerializer.new(@anonymous_post)
|
|
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
|
|
|
assert_equal({ post: { title: 'Hello!!', body: 'Hello, world!!', id: 43, comments: [], author: nil } }, adapter.serializable_hash)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|