diff options
author | Xavier Noria <fxn@hashref.com> | 2010-06-08 21:23:29 +0200 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2010-06-08 21:23:29 +0200 |
commit | 751f79a03351f1f0d21436b2b947352b97ded093 (patch) | |
tree | 9dd053597389241398c9173ab7f565697bef055f /activemodel | |
parent | e7e6ee3e7b075f5447697a6038cb46d65f9b137a (diff) | |
parent | ab2877cbe89e266ee986fc12e603abd93ac017ad (diff) | |
download | rails-751f79a03351f1f0d21436b2b947352b97ded093.tar.gz rails-751f79a03351f1f0d21436b2b947352b97ded093.tar.bz2 rails-751f79a03351f1f0d21436b2b947352b97ded093.zip |
Merge remote branch 'rails/master'
Diffstat (limited to 'activemodel')
-rw-r--r-- | activemodel/CHANGELOG | 2 | ||||
-rw-r--r-- | activemodel/lib/active_model/serializers/json.rb | 9 | ||||
-rw-r--r-- | activemodel/lib/active_model/version.rb | 2 | ||||
-rw-r--r-- | activemodel/test/cases/serializeration/json_serialization_test.rb | 36 |
4 files changed, 29 insertions, 20 deletions
diff --git a/activemodel/CHANGELOG b/activemodel/CHANGELOG index 43cf67d2b7..a5e7f300d9 100644 --- a/activemodel/CHANGELOG +++ b/activemodel/CHANGELOG @@ -1,4 +1,4 @@ -*Rails 3.0.0 [beta 4/release candidate] (unreleased)* +*Rails 3.0.0 [beta 4] (June 8th, 2010)* * JSON supports a custom root option: to_json(:root => 'custom') #4515 [Jatinder Singh] diff --git a/activemodel/lib/active_model/serializers/json.rb b/activemodel/lib/active_model/serializers/json.rb index ffdfbfcaaf..90305978c4 100644 --- a/activemodel/lib/active_model/serializers/json.rb +++ b/activemodel/lib/active_model/serializers/json.rb @@ -1,5 +1,5 @@ require 'active_support/json' -require 'active_support/core_ext/class/attribute_accessors' +require 'active_support/core_ext/class/attribute' module ActiveModel module Serializers @@ -10,7 +10,8 @@ module ActiveModel included do extend ActiveModel::Naming - cattr_accessor :include_root_in_json, :instance_writer => true + class_attribute :include_root_in_json + self.include_root_in_json = true end # Returns a JSON string representing the model. Some configuration is @@ -92,7 +93,9 @@ module ActiveModel end def from_json(json) - self.attributes = ActiveSupport::JSON.decode(json) + hash = ActiveSupport::JSON.decode(json) + hash = hash.values.first if include_root_in_json + self.attributes = hash self end end diff --git a/activemodel/lib/active_model/version.rb b/activemodel/lib/active_model/version.rb index aac47d5f81..c36fc8b1f7 100644 --- a/activemodel/lib/active_model/version.rb +++ b/activemodel/lib/active_model/version.rb @@ -3,7 +3,7 @@ module ActiveModel MAJOR = 3 MINOR = 0 TINY = 0 - BUILD = "beta3" + BUILD = "beta4" STRING = [MAJOR, MINOR, TINY, BUILD].join('.') end diff --git a/activemodel/test/cases/serializeration/json_serialization_test.rb b/activemodel/test/cases/serializeration/json_serialization_test.rb index 7e89815c96..04b50e5bb8 100644 --- a/activemodel/test/cases/serializeration/json_serialization_test.rb +++ b/activemodel/test/cases/serializeration/json_serialization_test.rb @@ -22,35 +22,41 @@ class JsonSerializationTest < ActiveModel::TestCase end test "should include root in json" do + 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 + end + + test "should not include root in json" do begin - Contact.include_root_in_json = true + Contact.include_root_in_json = false json = @contact.to_json - assert_match %r{^\{"contact":\{}, json + assert_no_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 + Contact.include_root_in_json = true end end test "should include custom root in json" do - begin - Contact.include_root_in_json = true - json = @contact.to_json(:root => 'json_contact') + json = @contact.to_json(:root => 'json_contact') - assert_match %r{^\{"json_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{^\{"json_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 |