diff options
author | Rick Olson <technoweenie@gmail.com> | 2008-04-01 20:09:45 +0000 |
---|---|---|
committer | Rick Olson <technoweenie@gmail.com> | 2008-04-01 20:09:45 +0000 |
commit | d450ac4459165e3af6e45798a6afeac1876f3bc4 (patch) | |
tree | f555ffd09f7d79fde29824b658cc6e278fcf46ca /activerecord/test | |
parent | 6a74360f972c75c70b64e3b6f51bb7a64681e2e3 (diff) | |
download | rails-d450ac4459165e3af6e45798a6afeac1876f3bc4.tar.gz rails-d450ac4459165e3af6e45798a6afeac1876f3bc4.tar.bz2 rails-d450ac4459165e3af6e45798a6afeac1876f3bc4.zip |
Tweak ActiveRecord::Base#to_json to include a root value in the returned hash: {post: {title: ...}} [rick]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9202 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/json_serialization_test.rb | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/activerecord/test/cases/json_serialization_test.rb b/activerecord/test/cases/json_serialization_test.rb index c4597a110d..3446e5e7f0 100644 --- a/activerecord/test/cases/json_serialization_test.rb +++ b/activerecord/test/cases/json_serialization_test.rb @@ -7,6 +7,10 @@ require 'models/tag' require 'models/comment' class JsonSerializationTest < ActiveRecord::TestCase + class NamespacedContact < Contact + column :name, :string + end + def setup @contact = Contact.new( :name => 'Konata Izumi', @@ -18,6 +22,27 @@ class JsonSerializationTest < ActiveRecord::TestCase ) end + def test_should_demodulize_root_in_json + NamespacedContact.include_root_in_json = true + @contact = NamespacedContact.new :name => 'whatever' + json = @contact.to_json + assert_match %r{^\{"namespaced_contact": \{}, json + end + + def test_should_include_root_in_json + Contact.include_root_in_json = true + 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 + def test_should_encode_all_encodable_attributes json = @contact.to_json |