mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
Merge pull request #998 from joaomdmoura/changing_root_to_model_class_name
Changing root to model class name
This commit is contained in:
commit
e1c25e86e3
@ -169,7 +169,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def json_key
|
def json_key
|
||||||
@root || self.class.root_name
|
@root || object.class.model_name.to_s.downcase
|
||||||
end
|
end
|
||||||
|
|
||||||
def id
|
def id
|
||||||
|
|||||||
@ -39,7 +39,7 @@ module ActiveModel
|
|||||||
serializer = PostPreviewSerializer.new(@anonymous_post)
|
serializer = PostPreviewSerializer.new(@anonymous_post)
|
||||||
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
||||||
|
|
||||||
assert_equal({posts: {title: "Hello!!", body: "Hello, world!!", id: 43, comments: [], author: nil}}, adapter.serializable_hash)
|
assert_equal({post: {title: "Hello!!", body: "Hello, world!!", id: 43, comments: [], author: nil}}, adapter.serializable_hash)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -28,7 +28,7 @@ module ActiveModel
|
|||||||
@serializer = ArraySerializer.new([@blog], serializer: CustomBlogSerializer)
|
@serializer = ArraySerializer.new([@blog], serializer: CustomBlogSerializer)
|
||||||
@adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
|
@adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
|
||||||
|
|
||||||
expected = {custom_blogs:[{
|
expected = {blogs:[{
|
||||||
id: 1,
|
id: 1,
|
||||||
special_attribute: "Special",
|
special_attribute: "Special",
|
||||||
articles: [{id: 1,title: "Hello!!", body: "Hello, world!!"}, {id: 2, title: "New Post", body: "Body"}]
|
articles: [{id: 1,title: "Hello!!", body: "Hello, world!!"}, {id: 2, title: "New Post", body: "Body"}]
|
||||||
|
|||||||
@ -38,7 +38,7 @@ module ActiveModel
|
|||||||
tags: [
|
tags: [
|
||||||
{"attributes"=>{"id"=>1, "name"=>"#hash_tag"}}
|
{"attributes"=>{"id"=>1, "name"=>"#hash_tag"}}
|
||||||
]
|
]
|
||||||
}.to_json, adapter.serializable_hash[:post_with_tags].to_json)
|
}.to_json, adapter.serializable_hash[:post].to_json)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -39,10 +39,9 @@ module ActiveModel
|
|||||||
],
|
],
|
||||||
writer: {id: 1, name: "Steve K."},
|
writer: {id: 1, name: "Steve K."},
|
||||||
site: {id: 1, name: "My Blog!!"}
|
site: {id: 1, name: "My Blog!!"}
|
||||||
}, adapter.serializable_hash[:post_with_custom_keys])
|
}, adapter.serializable_hash[:post])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -15,7 +15,7 @@ module ActiveModel
|
|||||||
|
|
||||||
def test_json_serializable_hash
|
def test_json_serializable_hash
|
||||||
adapter = ActiveModel::Serializer::Adapter::Json.new(@blog_serializer)
|
adapter = ActiveModel::Serializer::Adapter::Json.new(@blog_serializer)
|
||||||
assert_equal({alternate_blog: { id:1, title:"AMS Hints"}}, adapter.serializable_hash)
|
assert_equal({blog: { id:1, title:"AMS Hints"}}, adapter.serializable_hash)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_attribute_inheritance_with_key
|
def test_attribute_inheritance_with_key
|
||||||
|
|||||||
@ -15,7 +15,7 @@ module ActiveModel
|
|||||||
serializer = AlternateBlogSerializer.new(@blog, meta: {total: 10})
|
serializer = AlternateBlogSerializer.new(@blog, meta: {total: 10})
|
||||||
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
||||||
expected = {
|
expected = {
|
||||||
alternate_blog: {
|
blog: {
|
||||||
id: 1,
|
id: 1,
|
||||||
title: "AMS Hints"
|
title: "AMS Hints"
|
||||||
},
|
},
|
||||||
@ -40,7 +40,7 @@ module ActiveModel
|
|||||||
serializer = AlternateBlogSerializer.new(@blog, meta: {total: 10}, meta_key: "haha_meta")
|
serializer = AlternateBlogSerializer.new(@blog, meta: {total: 10}, meta_key: "haha_meta")
|
||||||
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
||||||
expected = {
|
expected = {
|
||||||
alternate_blog: {
|
blog: {
|
||||||
id: 1,
|
id: 1,
|
||||||
title: "AMS Hints"
|
title: "AMS Hints"
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user