Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configuration option to set resource type to singular/plural #1081

Merged
merged 1 commit into from
Aug 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ def id
end

def json_api_type
object.class.model_name.plural
if config.jsonapi_resource_type == :plural
object.class.model_name.plural
else
object.class.model_name.singular
end
end

def attributes(options = {})
Expand Down
1 change: 1 addition & 0 deletions lib/active_model/serializer/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Configuration
included do |base|
base.config.array_serializer = ActiveModel::Serializer::ArraySerializer
base.config.adapter = :flatten_json
base.config.jsonapi_resource_type = :plural
end
end
end
Expand Down
59 changes: 59 additions & 0 deletions test/adapter/json_api/resource_type_config_test.rb
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SerializableResource? :(

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, we could use SerializableResource through all those serializer + adapter in the tests, glad that yyou pointed that out@bf4. @beauby could you make a PR updating it?

assert_equal('comment', adapter.serializable_hash[:data][:type])
end
end
end
end
end
end
end