From 047f11fbf7b6637127100af43d04fd2285d71d63 Mon Sep 17 00:00:00 2001 From: Ben Woosley Date: Thu, 7 Jan 2016 11:32:10 -0800 Subject: [PATCH] Don't pluralize the CollectionSerializer#root for #json_key One of three constituents is used to provide the CollectionSerializer's #json_key: 1) the :root option - controlled by the caller 2) the #name of the first resource serializer - the root or underscored model name 3) the underscored #name of the resources object - generally equivalent to the underscored model name of #2 Of the three, only the latter 2 are out of the callers control, and only the latter two are expected to be singular by default. Not pluralizing the root gives the caller additional flexibility in defining the desired root, whether conventionally plural, unconventionally plural (e.g. objects_received:) or singular. --- .../serializer/collection_serializer.rb | 7 +++++-- test/action_controller/serialization_test.rb | 4 ++-- test/collection_serializer_test.rb | 15 +++++++++------ 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/active_model/serializer/collection_serializer.rb b/lib/active_model/serializer/collection_serializer.rb index a3c9dc476..6f568953a 100644 --- a/lib/active_model/serializer/collection_serializer.rb +++ b/lib/active_model/serializer/collection_serializer.rb @@ -23,8 +23,11 @@ def initialize(resources, options = {}) end def json_key - key = root || serializers.first.try(:json_key) || object.try(:name).try(:underscore) - key.try(:pluralize) + root || begin + singular_key = serializers.first.try(:json_key) || + object.try(:name).try(:underscore) + singular_key.try(:pluralize) + end end def paginated? diff --git a/test/action_controller/serialization_test.rb b/test/action_controller/serialization_test.rb index a3b761981..d2fe3959e 100644 --- a/test/action_controller/serialization_test.rb +++ b/test/action_controller/serialization_test.rb @@ -171,7 +171,7 @@ def test_render_array_using_custom_root with_adapter :json do get :render_array_using_custom_root end - expected = { custom_roots: [{ name: 'Name 1', description: 'Description 1' }] } + expected = { custom_root: [{ name: 'Name 1', description: 'Description 1' }] } assert_equal 'application/json', @response.content_type assert_equal expected.to_json, @response.body end @@ -181,7 +181,7 @@ def test_render_array_that_is_empty_using_custom_root get :render_array_that_is_empty_using_custom_root end - expected = { custom_roots: [] } + expected = { custom_root: [] } assert_equal 'application/json', @response.content_type assert_equal expected.to_json, @response.body end diff --git a/test/collection_serializer_test.rb b/test/collection_serializer_test.rb index 662aa1ee4..a7c0fa021 100644 --- a/test/collection_serializer_test.rb +++ b/test/collection_serializer_test.rb @@ -62,8 +62,9 @@ def test_root_with_no_serializers assert_equal expected, @serializer.root end - def test_json_key - assert_equal 'comments', @serializer.json_key + def test_json_key_with_resource_with_serializer + singular_key = @serializer.send(:serializers).first.json_key + assert_equal singular_key.pluralize, @serializer.json_key end def test_json_key_with_resource_with_name_and_no_serializers @@ -84,13 +85,15 @@ def test_json_key_with_resource_without_name_and_no_serializers end def test_json_key_with_root - serializer = collection_serializer.new(@resource, root: 'custom_root') - assert_equal 'custom_roots', serializer.json_key + expected = 'custom_root' + serializer = collection_serializer.new(@resource, root: expected) + assert_equal expected, serializer.json_key end def test_json_key_with_root_and_no_serializers - serializer = collection_serializer.new(build_named_collection, root: 'custom_root') - assert_equal 'custom_roots', serializer.json_key + expected = 'custom_root' + serializer = collection_serializer.new(build_named_collection, root: expected) + assert_equal expected, serializer.json_key end end end