From 91235ba7bc7397df1893698c099e3bad54bad78a Mon Sep 17 00:00:00 2001 From: Lucas Hosseini Date: Mon, 24 Aug 2015 22:46:11 +0200 Subject: [PATCH] Add configuration option to set resource type to singular/plural with jsonapi. --- lib/active_model/serializer.rb | 6 +- lib/active_model/serializer/configuration.rb | 1 + .../json_api/resource_type_config_test.rb | 59 +++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 test/adapter/json_api/resource_type_config_test.rb diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index 738743007..96433465e 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -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 = {}) diff --git a/lib/active_model/serializer/configuration.rb b/lib/active_model/serializer/configuration.rb index 86df706e1..102c821e1 100644 --- a/lib/active_model/serializer/configuration.rb +++ b/lib/active_model/serializer/configuration.rb @@ -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 diff --git a/test/adapter/json_api/resource_type_config_test.rb b/test/adapter/json_api/resource_type_config_test.rb new file mode 100644 index 000000000..e389b9b4f --- /dev/null +++ b/test/adapter/json_api/resource_type_config_test.rb @@ -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