From 0f4d353b6d4cfa3f98c0def31a3f1b365359c990 Mon Sep 17 00:00:00 2001 From: Cody Fauser Date: Fri, 22 Jan 2010 07:55:46 -0500 Subject: [PATCH] Fix defaulting include_root_in_json = true --- activemodel/lib/active_model/serializers/json.rb | 5 ++- .../serializeration/json_serialization_test.rb | 23 +++++++------------ activerecord/test/cases/json_serialization_test.rb | 3 -- 3 files changed, 12 insertions(+), 19 deletions(-) diff --git a/activemodel/lib/active_model/serializers/json.rb b/activemodel/lib/active_model/serializers/json.rb index 794de7d..9f7294a 100644 --- a/activemodel/lib/active_model/serializers/json.rb +++ b/activemodel/lib/active_model/serializers/json.rb @@ -10,13 +10,14 @@ module ActiveModel included do extend ActiveModel::Naming - cattr_accessor :include_root_in_json, :instance_writer => true + cattr_accessor :include_root_in_json, :instance_writer => false + self.include_root_in_json = true end # Returns a JSON string representing the model. Some configuration is # available through +options+. # - # The option ActiveModel::Base.include_root_in_json controls the + # The option ActiveRecord::Base.include_root_in_json controls the # top-level behavior of to_json. It is true by default. When it is true, # to_json will emit a single root node named after the object's type. For example: # diff --git a/activemodel/test/cases/serializeration/json_serialization_test.rb b/activemodel/test/cases/serializeration/json_serialization_test.rb index 81df52f..7992225 100644 --- a/activemodel/test/cases/serializeration/json_serialization_test.rb +++ b/activemodel/test/cases/serializeration/json_serialization_test.rb @@ -20,21 +20,16 @@ class JsonSerializationTest < ActiveModel::TestCase @contact.awesome = true @contact.preferences = { 'shows' => 'anime' } end - + test "should include root in json" do - begin - Contact.include_root_in_json = true - json = @contact.to_json + json = @contact.to_json - assert_match %r{^\{"contact":\{}, json - assert_match %r{"name":"Konata Izumi"}, json - assert_match %r{"age":16}, json - assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})) - assert_match %r{"awesome":true}, json - assert_match %r{"preferences":\{"shows":"anime"\}}, json - ensure - Contact.include_root_in_json = false - end + assert_match %r{^\{"contact":\{}, json + assert_match %r{"name":"Konata Izumi"}, json + assert_match %r{"age":16}, json + assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})) + assert_match %r{"awesome":true}, json + assert_match %r{"preferences":\{"shows":"anime"\}}, json end test "should encode all encodable attributes" do diff --git a/activerecord/test/cases/json_serialization_test.rb b/activerecord/test/cases/json_serialization_test.rb index 54bc8e2..82968ca 100644 --- a/activerecord/test/cases/json_serialization_test.rb +++ b/activerecord/test/cases/json_serialization_test.rb @@ -30,7 +30,6 @@ class JsonSerializationTest < ActiveRecord::TestCase end def test_should_include_root_in_json - Contact.include_root_in_json = true json = @contact.to_json assert_match %r{^\{"contact":\{}, json @@ -39,8 +38,6 @@ class JsonSerializationTest < ActiveRecord::TestCase assert json.include?(%("created_at":#{ActiveSupport::JSON.encode(Time.utc(2006, 8, 1))})) assert_match %r{"awesome":true}, json assert_match %r{"preferences":\{"shows":"anime"\}}, json - ensure - Contact.include_root_in_json = false end def test_should_encode_all_encodable_attributes -- 1.6.4.1