mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Add Serializer 'type' directive to control type field, for use by the JsonApi adapter
This commit is contained in:
parent
1f08865a10
commit
2dd569ae51
@ -46,6 +46,10 @@ module ActiveModel
|
|||||||
super
|
super
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.type(type)
|
||||||
|
self._type = type
|
||||||
|
end
|
||||||
|
|
||||||
def self.attributes(*attrs)
|
def self.attributes(*attrs)
|
||||||
attrs = attrs.first if attrs.first.class == Array
|
attrs = attrs.first if attrs.first.class == Array
|
||||||
|
|
||||||
@ -122,6 +126,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
attr_accessor :object, :root, :meta, :meta_key, :scope
|
attr_accessor :object, :root, :meta, :meta_key, :scope
|
||||||
|
class_attribute :_type, instance_writer: false
|
||||||
|
|
||||||
def initialize(object, options = {})
|
def initialize(object, options = {})
|
||||||
self.object = object
|
self.object = object
|
||||||
|
|||||||
@ -71,6 +71,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def resource_identifier_type_for(serializer)
|
def resource_identifier_type_for(serializer)
|
||||||
|
return serializer._type if serializer._type
|
||||||
if ActiveModel::Serializer.config.jsonapi_resource_type == :singular
|
if ActiveModel::Serializer.config.jsonapi_resource_type == :singular
|
||||||
serializer.object.class.model_name.singular
|
serializer.object.class.model_name.singular
|
||||||
else
|
else
|
||||||
|
|||||||
@ -5,6 +5,11 @@ module ActiveModel
|
|||||||
module Adapter
|
module Adapter
|
||||||
class JsonApi
|
class JsonApi
|
||||||
class ResourceTypeConfigTest < Minitest::Test
|
class ResourceTypeConfigTest < Minitest::Test
|
||||||
|
class ProfileTypeSerializer < ActiveModel::Serializer
|
||||||
|
attributes :name
|
||||||
|
type 'profile'
|
||||||
|
end
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@author = Author.new(id: 1, name: 'Steve K.')
|
@author = Author.new(id: 1, name: 'Steve K.')
|
||||||
@author.bio = nil
|
@author.bio = nil
|
||||||
@ -36,22 +41,29 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_config_plural
|
def test_config_plural
|
||||||
with_adapter :json_api do
|
with_jsonapi_resource_type :plural do
|
||||||
with_jsonapi_resource_type :plural do
|
hash = serializable(@comment, adapter: :json_api).serializable_hash
|
||||||
hash = ActiveModel::SerializableResource.new(@comment).serializable_hash
|
assert_equal('comments', hash[:data][:type])
|
||||||
assert_equal('comments', hash[:data][:type])
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_config_singular
|
def test_config_singular
|
||||||
with_adapter :json_api do
|
with_jsonapi_resource_type :singular do
|
||||||
with_jsonapi_resource_type :singular do
|
hash = serializable(@comment, adapter: :json_api).serializable_hash
|
||||||
hash = ActiveModel::SerializableResource.new(@comment).serializable_hash
|
assert_equal('comment', hash[:data][:type])
|
||||||
assert_equal('comment', hash[:data][:type])
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user