mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Merge pull request #1418 from brigade/collection-pluralize
Don't pluralize the CollectionSerializer#root for #json_key
This commit is contained in:
commit
ef58efdf73
@ -13,6 +13,9 @@ Breaking changes:
|
|||||||
Adapter functions.
|
Adapter functions.
|
||||||
* named `Base` because it's a Rails-ism.
|
* named `Base` because it's a Rails-ism.
|
||||||
* It helps to isolate and highlight what the Adapter interface actually is.
|
* It helps to isolate and highlight what the Adapter interface actually is.
|
||||||
|
- [#1418](https://github.com/rails-api/active_model_serializers/pull/1418)
|
||||||
|
serialized collections now use the root option as is; now, only the
|
||||||
|
root derived from the serializer or object is always pluralized.
|
||||||
|
|
||||||
Features:
|
Features:
|
||||||
|
|
||||||
|
|||||||
@ -23,8 +23,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def json_key
|
def json_key
|
||||||
key = root || serializers.first.try(:json_key) || object.try(:name).try(:underscore)
|
root || derived_root
|
||||||
key.try(:pluralize)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def paginated?
|
def paginated?
|
||||||
@ -36,6 +35,13 @@ module ActiveModel
|
|||||||
protected
|
protected
|
||||||
|
|
||||||
attr_reader :serializers
|
attr_reader :serializers
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def derived_root
|
||||||
|
key = serializers.first.try(:json_key) || object.try(:name).try(:underscore)
|
||||||
|
key.try(:pluralize)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -171,7 +171,7 @@ module ActionController
|
|||||||
with_adapter :json do
|
with_adapter :json do
|
||||||
get :render_array_using_custom_root
|
get :render_array_using_custom_root
|
||||||
end
|
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 'application/json', @response.content_type
|
||||||
assert_equal expected.to_json, @response.body
|
assert_equal expected.to_json, @response.body
|
||||||
end
|
end
|
||||||
@ -181,7 +181,7 @@ module ActionController
|
|||||||
get :render_array_that_is_empty_using_custom_root
|
get :render_array_that_is_empty_using_custom_root
|
||||||
end
|
end
|
||||||
|
|
||||||
expected = { custom_roots: [] }
|
expected = { custom_root: [] }
|
||||||
assert_equal 'application/json', @response.content_type
|
assert_equal 'application/json', @response.content_type
|
||||||
assert_equal expected.to_json, @response.body
|
assert_equal expected.to_json, @response.body
|
||||||
end
|
end
|
||||||
|
|||||||
@ -62,8 +62,9 @@ module ActiveModel
|
|||||||
assert_equal expected, @serializer.root
|
assert_equal expected, @serializer.root
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_json_key
|
def test_json_key_with_resource_with_serializer
|
||||||
assert_equal 'comments', @serializer.json_key
|
singular_key = @serializer.send(:serializers).first.json_key
|
||||||
|
assert_equal singular_key.pluralize, @serializer.json_key
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_json_key_with_resource_with_name_and_no_serializers
|
def test_json_key_with_resource_with_name_and_no_serializers
|
||||||
@ -84,13 +85,15 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_json_key_with_root
|
def test_json_key_with_root
|
||||||
serializer = collection_serializer.new(@resource, root: 'custom_root')
|
expected = 'custom_root'
|
||||||
assert_equal 'custom_roots', serializer.json_key
|
serializer = collection_serializer.new(@resource, root: expected)
|
||||||
|
assert_equal expected, serializer.json_key
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_json_key_with_root_and_no_serializers
|
def test_json_key_with_root_and_no_serializers
|
||||||
serializer = collection_serializer.new(build_named_collection, root: 'custom_root')
|
expected = 'custom_root'
|
||||||
assert_equal 'custom_roots', serializer.json_key
|
serializer = collection_serializer.new(build_named_collection, root: expected)
|
||||||
|
assert_equal expected, serializer.json_key
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user