Add Serializer 'type' directive to control type field, for use by the JsonApi adapter

This commit is contained in:
Ivan Yurov
2015-10-01 22:48:57 -04:00
parent 1f08865a10
commit 2dd569ae51
3 changed files with 28 additions and 10 deletions

View File

@@ -5,6 +5,11 @@ module ActiveModel
module Adapter
class JsonApi
class ResourceTypeConfigTest < Minitest::Test
class ProfileTypeSerializer < ActiveModel::Serializer
attributes :name
type 'profile'
end
def setup
@author = Author.new(id: 1, name: 'Steve K.')
@author.bio = nil
@@ -36,22 +41,29 @@ module ActiveModel
end
def test_config_plural
with_adapter :json_api do
with_jsonapi_resource_type :plural do
hash = ActiveModel::SerializableResource.new(@comment).serializable_hash
assert_equal('comments', hash[:data][:type])
end
with_jsonapi_resource_type :plural do
hash = serializable(@comment, adapter: :json_api).serializable_hash
assert_equal('comments', hash[:data][:type])
end
end
def test_config_singular
with_adapter :json_api do
with_jsonapi_resource_type :singular do
hash = ActiveModel::SerializableResource.new(@comment).serializable_hash
assert_equal('comment', hash[:data][:type])
end
with_jsonapi_resource_type :singular do
hash = serializable(@comment, adapter: :json_api).serializable_hash
assert_equal('comment', hash[:data][:type])
end
end
def test_explicit_type_value
hash = serializable(@author, serializer: ProfileTypeSerializer, adapter: :json_api).serializable_hash
assert_equal('profile', hash.fetch(:data).fetch(:type))
end
private
def serializable(resource, options = {})
ActiveModel::SerializableResource.new(resource, options)
end
end
end
end