mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
Use underscored json_root
This commit is contained in:
parent
8568ed5913
commit
e8e4bdefd2
@ -127,7 +127,7 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def json_key
|
def json_key
|
||||||
@root || object.class.model_name.to_s.downcase
|
@root || object.class.model_name.to_s.underscore
|
||||||
end
|
end
|
||||||
|
|
||||||
def id
|
def id
|
||||||
|
|||||||
@ -17,26 +17,27 @@ module ActiveModel
|
|||||||
@first_post.blog = @blog
|
@first_post.blog = @blog
|
||||||
@second_post.blog = nil
|
@second_post.blog = nil
|
||||||
|
|
||||||
@serializer = ArraySerializer.new([@first_post, @second_post])
|
|
||||||
@adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
|
|
||||||
ActionController::Base.cache_store.clear
|
ActionController::Base.cache_store.clear
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_with_serializer_option
|
def test_with_serializer_option
|
||||||
@blog.special_attribute = "Special"
|
@blog.special_attribute = "Special"
|
||||||
@blog.articles = [@first_post, @second_post]
|
@blog.articles = [@first_post, @second_post]
|
||||||
@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 = {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"}]
|
||||||
}]}
|
}]}
|
||||||
assert_equal expected, @adapter.serializable_hash
|
assert_equal expected, adapter.serializable_hash
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_include_multiple_posts
|
def test_include_multiple_posts
|
||||||
|
serializer = ArraySerializer.new([@first_post, @second_post])
|
||||||
|
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
||||||
|
|
||||||
expected = { posts: [{
|
expected = { posts: [{
|
||||||
title: "Hello!!",
|
title: "Hello!!",
|
||||||
body: "Hello, world!!",
|
body: "Hello, world!!",
|
||||||
@ -64,7 +65,15 @@ module ActiveModel
|
|||||||
name: "Custom blog"
|
name: "Custom blog"
|
||||||
}
|
}
|
||||||
}]}
|
}]}
|
||||||
assert_equal expected, @adapter.serializable_hash
|
assert_equal expected, adapter.serializable_hash
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_root_is_underscored
|
||||||
|
virtual_value = VirtualValue.new(id: 1)
|
||||||
|
serializer = ArraySerializer.new([virtual_value])
|
||||||
|
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
||||||
|
|
||||||
|
assert_equal 1, adapter.serializable_hash[:virtual_values].length
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -5,13 +5,17 @@ module ActiveModel
|
|||||||
class RootTest < Minitest::Test
|
class RootTest < Minitest::Test
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
@virtual_value = VirtualValue.new(id: 1)
|
||||||
@profile_serializer = ProfileSerializer.new(@post, {root: 'smth'})
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_overwrite_root
|
def test_overwrite_root
|
||||||
setup
|
serializer = VirtualValueSerializer.new(@virtual_value, {root: 'smth'})
|
||||||
assert_equal('smth', @profile_serializer.json_key)
|
assert_equal('smth', serializer.json_key)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_underscore_in_root
|
||||||
|
serializer = VirtualValueSerializer.new(@virtual_value)
|
||||||
|
assert_equal('virtual_value', serializer.json_key)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user