Skip to content

Commit a819da6

Browse files
committed
Merge pull request #1213 from AutoCloud/type_setting
Add Serializer 'type' directive to control type field, for use by the JsonApi adapter
2 parents c5fdfb4 + 2dd569a commit a819da6

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

lib/active_model/serializer.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ def self.inherited(base)
4646
super
4747
end
4848

49+
def self.type(type)
50+
self._type = type
51+
end
52+
4953
def self.attributes(*attrs)
5054
attrs = attrs.first if attrs.first.class == Array
5155

@@ -122,6 +126,7 @@ def self.get_serializer_for(klass)
122126
end
123127

124128
attr_accessor :object, :root, :meta, :meta_key, :scope
129+
class_attribute :_type, instance_writer: false
125130

126131
def initialize(object, options = {})
127132
self.object = object

lib/active_model/serializer/adapter/json_api.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def serializable_hash_for_single_resource(options)
7171
end
7272

7373
def resource_identifier_type_for(serializer)
74+
return serializer._type if serializer._type
7475
if ActiveModel::Serializer.config.jsonapi_resource_type == :singular
7576
serializer.object.class.model_name.singular
7677
else

test/adapter/json_api/resource_type_config_test.rb

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ class Serializer
55
module Adapter
66
class JsonApi
77
class ResourceTypeConfigTest < Minitest::Test
8+
class ProfileTypeSerializer < ActiveModel::Serializer
9+
attributes :name
10+
type 'profile'
11+
end
12+
813
def setup
914
@author = Author.new(id: 1, name: 'Steve K.')
1015
@author.bio = nil
@@ -36,22 +41,29 @@ def with_jsonapi_resource_type type
3641
end
3742

3843
def test_config_plural
39-
with_adapter :json_api do
40-
with_jsonapi_resource_type :plural do
41-
hash = ActiveModel::SerializableResource.new(@comment).serializable_hash
42-
assert_equal('comments', hash[:data][:type])
43-
end
44+
with_jsonapi_resource_type :plural do
45+
hash = serializable(@comment, adapter: :json_api).serializable_hash
46+
assert_equal('comments', hash[:data][:type])
4447
end
4548
end
4649

4750
def test_config_singular
48-
with_adapter :json_api do
49-
with_jsonapi_resource_type :singular do
50-
hash = ActiveModel::SerializableResource.new(@comment).serializable_hash
51-
assert_equal('comment', hash[:data][:type])
52-
end
51+
with_jsonapi_resource_type :singular do
52+
hash = serializable(@comment, adapter: :json_api).serializable_hash
53+
assert_equal('comment', hash[:data][:type])
5354
end
5455
end
56+
57+
def test_explicit_type_value
58+
hash = serializable(@author, serializer: ProfileTypeSerializer, adapter: :json_api).serializable_hash
59+
assert_equal('profile', hash.fetch(:data).fetch(:type))
60+
end
61+
62+
private
63+
64+
def serializable(resource, options = {})
65+
ActiveModel::SerializableResource.new(resource, options)
66+
end
5567
end
5668
end
5769
end

0 commit comments

Comments
 (0)