Skip to content

Commit 6aba260

Browse files
committed
Merge pull request #1052 from whatthewhat/underscored-json-root
Use underscored json_root when serializing a collection
2 parents 8568ed5 + e8e4bde commit 6aba260

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

lib/active_model/serializer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def initialize(object, options = {})
127127
end
128128

129129
def json_key
130-
@root || object.class.model_name.to_s.downcase
130+
@root || object.class.model_name.to_s.underscore
131131
end
132132

133133
def id

test/adapter/json/collection_test.rb

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,27 @@ def setup
1717
@first_post.blog = @blog
1818
@second_post.blog = nil
1919

20-
@serializer = ArraySerializer.new([@first_post, @second_post])
21-
@adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
2220
ActionController::Base.cache_store.clear
2321
end
2422

2523
def test_with_serializer_option
2624
@blog.special_attribute = "Special"
2725
@blog.articles = [@first_post, @second_post]
28-
@serializer = ArraySerializer.new([@blog], serializer: CustomBlogSerializer)
29-
@adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
26+
serializer = ArraySerializer.new([@blog], serializer: CustomBlogSerializer)
27+
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
3028

3129
expected = {blogs:[{
3230
id: 1,
3331
special_attribute: "Special",
3432
articles: [{id: 1,title: "Hello!!", body: "Hello, world!!"}, {id: 2, title: "New Post", body: "Body"}]
3533
}]}
36-
assert_equal expected, @adapter.serializable_hash
34+
assert_equal expected, adapter.serializable_hash
3735
end
3836

3937
def test_include_multiple_posts
38+
serializer = ArraySerializer.new([@first_post, @second_post])
39+
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
40+
4041
expected = { posts: [{
4142
title: "Hello!!",
4243
body: "Hello, world!!",
@@ -64,7 +65,15 @@ def test_include_multiple_posts
6465
name: "Custom blog"
6566
}
6667
}]}
67-
assert_equal expected, @adapter.serializable_hash
68+
assert_equal expected, adapter.serializable_hash
69+
end
70+
71+
def test_root_is_underscored
72+
virtual_value = VirtualValue.new(id: 1)
73+
serializer = ArraySerializer.new([virtual_value])
74+
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
75+
76+
assert_equal 1, adapter.serializable_hash[:virtual_values].length
6877
end
6978
end
7079
end

test/serializers/root_test.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ class Serializer
55
class RootTest < Minitest::Test
66

77
def setup
8-
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
9-
@profile_serializer = ProfileSerializer.new(@post, {root: 'smth'})
8+
@virtual_value = VirtualValue.new(id: 1)
109
end
1110

1211
def test_overwrite_root
13-
setup
14-
assert_equal('smth', @profile_serializer.json_key)
12+
serializer = VirtualValueSerializer.new(@virtual_value, {root: 'smth'})
13+
assert_equal('smth', serializer.json_key)
14+
end
15+
16+
def test_underscore_in_root
17+
serializer = VirtualValueSerializer.new(@virtual_value)
18+
assert_equal('virtual_value', serializer.json_key)
1519
end
1620

1721
end

0 commit comments

Comments
 (0)