-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1081 from beauby/jsonapi-singular-plural-config
Add configuration option to set resource type to singular/plural
- Loading branch information
Showing
3 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
require 'test_helper' | ||
|
||
module ActiveModel | ||
class Serializer | ||
class Adapter | ||
class JsonApi | ||
class ResourceTypeConfigTest < Minitest::Test | ||
def setup | ||
@author = Author.new(id: 1, name: 'Steve K.') | ||
@author.bio = nil | ||
@author.roles = [] | ||
@blog = Blog.new(id: 23, name: 'AMS Blog') | ||
@post = Post.new(id: 42, title: 'New Post', body: 'Body') | ||
@anonymous_post = Post.new(id: 43, title: 'Hello!!', body: 'Hello, world!!') | ||
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT') | ||
@post.comments = [@comment] | ||
@post.blog = @blog | ||
@anonymous_post.comments = [] | ||
@anonymous_post.blog = nil | ||
@comment.post = @post | ||
@comment.author = nil | ||
@post.author = @author | ||
@anonymous_post.author = nil | ||
@blog = Blog.new(id: 1, name: "My Blog!!") | ||
@blog.writer = @author | ||
@blog.articles = [@post, @anonymous_post] | ||
@author.posts = [] | ||
end | ||
|
||
def with_jsonapi_resource_type type | ||
old_type = ActiveModel::Serializer.config[:jsonapi_resource_type] | ||
ActiveModel::Serializer.config[:jsonapi_resource_type] = type | ||
yield | ||
ensure | ||
ActiveModel::Serializer.config[:jsonapi_resource_type] = old_type | ||
end | ||
|
||
def test_config_plural | ||
with_jsonapi_resource_type :plural do | ||
serializer = CommentSerializer.new(@comment) | ||
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) | ||
ActionController::Base.cache_store.clear | ||
assert_equal('comments', adapter.serializable_hash[:data][:type]) | ||
end | ||
end | ||
|
||
def test_config_singular | ||
with_jsonapi_resource_type :singular do | ||
serializer = CommentSerializer.new(@comment) | ||
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) | ||
ActionController::Base.cache_store.clear | ||
assert_equal('comment', adapter.serializable_hash[:data][:type]) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |