From e8e4bdefd20ba5f9d5b6ffc82b1c674992b1dc2f Mon Sep 17 00:00:00 2001 From: Mikhail Topolskiy Date: Thu, 13 Aug 2015 20:31:48 +0300 Subject: [PATCH] Use underscored json_root --- lib/active_model/serializer.rb | 2 +- test/adapter/json/collection_test.rb | 21 +++++++++++++++------ test/serializers/root_test.rb | 12 ++++++++---- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/lib/active_model/serializer.rb b/lib/active_model/serializer.rb index cfad42b1f..738743007 100644 --- a/lib/active_model/serializer.rb +++ b/lib/active_model/serializer.rb @@ -127,7 +127,7 @@ def initialize(object, options = {}) end def json_key - @root || object.class.model_name.to_s.downcase + @root || object.class.model_name.to_s.underscore end def id diff --git a/test/adapter/json/collection_test.rb b/test/adapter/json/collection_test.rb index 3643ca230..295a0e3f4 100644 --- a/test/adapter/json/collection_test.rb +++ b/test/adapter/json/collection_test.rb @@ -17,26 +17,27 @@ def setup @first_post.blog = @blog @second_post.blog = nil - @serializer = ArraySerializer.new([@first_post, @second_post]) - @adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer) ActionController::Base.cache_store.clear end def test_with_serializer_option @blog.special_attribute = "Special" @blog.articles = [@first_post, @second_post] - @serializer = ArraySerializer.new([@blog], serializer: CustomBlogSerializer) - @adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer) + serializer = ArraySerializer.new([@blog], serializer: CustomBlogSerializer) + adapter = ActiveModel::Serializer::Adapter::Json.new(serializer) expected = {blogs:[{ id: 1, special_attribute: "Special", 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 def test_include_multiple_posts + serializer = ArraySerializer.new([@first_post, @second_post]) + adapter = ActiveModel::Serializer::Adapter::Json.new(serializer) + expected = { posts: [{ title: "Hello!!", body: "Hello, world!!", @@ -64,7 +65,15 @@ def test_include_multiple_posts 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 diff --git a/test/serializers/root_test.rb b/test/serializers/root_test.rb index 05853e391..749ff0a7f 100644 --- a/test/serializers/root_test.rb +++ b/test/serializers/root_test.rb @@ -5,13 +5,17 @@ class Serializer class RootTest < Minitest::Test def setup - @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }) - @profile_serializer = ProfileSerializer.new(@post, {root: 'smth'}) + @virtual_value = VirtualValue.new(id: 1) end def test_overwrite_root - setup - assert_equal('smth', @profile_serializer.json_key) + serializer = VirtualValueSerializer.new(@virtual_value, {root: 'smth'}) + 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