Skip to content

Commit 91235ba

Browse files
committed
Add configuration option to set resource type to singular/plural with jsonapi.
1 parent 87c47f8 commit 91235ba

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

lib/active_model/serializer.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,11 @@ def id
135135
end
136136

137137
def json_api_type
138-
object.class.model_name.plural
138+
if config.jsonapi_resource_type == :plural
139+
object.class.model_name.plural
140+
else
141+
object.class.model_name.singular
142+
end
139143
end
140144

141145
def attributes(options = {})

lib/active_model/serializer/configuration.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module Configuration
77
included do |base|
88
base.config.array_serializer = ActiveModel::Serializer::ArraySerializer
99
base.config.adapter = :flatten_json
10+
base.config.jsonapi_resource_type = :plural
1011
end
1112
end
1213
end
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
require 'test_helper'
2+
3+
module ActiveModel
4+
class Serializer
5+
class Adapter
6+
class JsonApi
7+
class ResourceTypeConfigTest < Minitest::Test
8+
def setup
9+
@author = Author.new(id: 1, name: 'Steve K.')
10+
@author.bio = nil
11+
@author.roles = []
12+
@blog = Blog.new(id: 23, name: 'AMS Blog')
13+
@post = Post.new(id: 42, title: 'New Post', body: 'Body')
14+
@anonymous_post = Post.new(id: 43, title: 'Hello!!', body: 'Hello, world!!')
15+
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
16+
@post.comments = [@comment]
17+
@post.blog = @blog
18+
@anonymous_post.comments = []
19+
@anonymous_post.blog = nil
20+
@comment.post = @post
21+
@comment.author = nil
22+
@post.author = @author
23+
@anonymous_post.author = nil
24+
@blog = Blog.new(id: 1, name: "My Blog!!")
25+
@blog.writer = @author
26+
@blog.articles = [@post, @anonymous_post]
27+
@author.posts = []
28+
end
29+
30+
def with_jsonapi_resource_type type
31+
old_type = ActiveModel::Serializer.config[:jsonapi_resource_type]
32+
ActiveModel::Serializer.config[:jsonapi_resource_type] = type
33+
yield
34+
ensure
35+
ActiveModel::Serializer.config[:jsonapi_resource_type] = old_type
36+
end
37+
38+
def test_config_plural
39+
with_jsonapi_resource_type :plural do
40+
serializer = CommentSerializer.new(@comment)
41+
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
42+
ActionController::Base.cache_store.clear
43+
assert_equal('comments', adapter.serializable_hash[:data][:type])
44+
end
45+
end
46+
47+
def test_config_singular
48+
with_jsonapi_resource_type :singular do
49+
serializer = CommentSerializer.new(@comment)
50+
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
51+
ActionController::Base.cache_store.clear
52+
assert_equal('comment', adapter.serializable_hash[:data][:type])
53+
end
54+
end
55+
end
56+
end
57+
end
58+
end
59+
end

0 commit comments

Comments
 (0)