Skip to content

Commit 532828b

Browse files
committed
Merge pull request #1515 from groyoh/symbol_type
[FEATURE] Add symbol support for Serializer.type method
2 parents f02f084 + 727d763 commit 532828b

File tree

5 files changed

+83
-73
lines changed

5 files changed

+83
-73
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
Breaking changes:
44

55
Features:
6+
- [#1515](https://github.com/rails-api/active_model_serializers/pull/1515) Adds support for symbols to the
7+
`ActiveModel::Serializer.type` method. (@groyoh)
68
- [#1504](https://github.com/rails-api/active_model_serializers/pull/1504) Adds the changes missing from #1454
79
and add more tests for resource identifier and relationship objects. Fix association block with link
810
returning `data: nil`.(@groyoh)

docs/general/serializers.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,30 @@ end
107107

108108
#### ::type
109109

110-
e.g.
110+
The `::type` method defines the JSONAPI [type](http://jsonapi.org/format/#document-resource-object-identification) that will be rendered for this serializer.
111+
It either takes a `String` or `Symbol` as parameter.
112+
113+
Note: This method is useful only when using the `:json_api` adapter.
111114

115+
Examples:
112116
```ruby
113117
class UserProfileSerializer < ActiveModel::Serializer
114118
type 'profile'
115119
end
120+
class AuthorProfileSerializer < ActiveModel::Serializer
121+
type :profile
122+
end
123+
```
124+
125+
With the `:json_api` adapter, the previous serializers would be rendered as:
126+
127+
``` json
128+
{
129+
"data": {
130+
"id": "1",
131+
"type": "profile"
132+
}
133+
}
116134
```
117135

118136
#### ::link

lib/active_model/serializer/type.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module ClassMethods
1717
# class AdminAuthorSerializer < ActiveModel::Serializer
1818
# type 'authors'
1919
def type(type)
20-
self._type = type
20+
self._type = type && type.to_s
2121
end
2222
end
2323
end

test/adapter/json_api/resource_type_config_test.rb

-71
This file was deleted.

test/adapter/json_api/type_test.rb

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
require 'test_helper'
2+
3+
module ActiveModel
4+
class Serializer
5+
module Adapter
6+
class JsonApi
7+
class TypeTest < ActiveSupport::TestCase
8+
class StringTypeSerializer < ActiveModel::Serializer
9+
attribute :name
10+
type 'profile'
11+
end
12+
13+
class SymbolTypeSerializer < ActiveModel::Serializer
14+
attribute :name
15+
type :profile
16+
end
17+
18+
setup do
19+
@author = Author.new(id: 1, name: 'Steve K.')
20+
end
21+
22+
def test_config_plural
23+
with_jsonapi_resource_type :plural do
24+
assert_type(@author, 'authors')
25+
end
26+
end
27+
28+
def test_config_singular
29+
with_jsonapi_resource_type :singular do
30+
assert_type(@author, 'author')
31+
end
32+
end
33+
34+
def test_explicit_string_type_value
35+
assert_type(@author, 'profile', serializer: StringTypeSerializer)
36+
end
37+
38+
def test_explicit_symbol_type_value
39+
assert_type(@author, 'profile', serializer: SymbolTypeSerializer)
40+
end
41+
42+
private
43+
44+
def assert_type(resource, expected_type, opts = {})
45+
opts = opts.reverse_merge(adapter: :json_api)
46+
hash = serializable(resource, opts).serializable_hash
47+
assert_equal(expected_type, hash.fetch(:data).fetch(:type))
48+
end
49+
50+
def with_jsonapi_resource_type inflection
51+
old_inflection = ActiveModelSerializers.config.jsonapi_resource_type
52+
ActiveModelSerializers.config.jsonapi_resource_type = inflection
53+
yield
54+
ensure
55+
ActiveModelSerializers.config.jsonapi_resource_type = old_inflection
56+
end
57+
end
58+
end
59+
end
60+
end
61+
end

0 commit comments

Comments
 (0)