aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/json_serialization_test.rb
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2007-10-04 03:28:42 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2007-10-04 03:28:42 +0000
commitb1968708e12972515fdc8eefdcaff95edbebc76b (patch)
tree93c179fda5d5428bb12bbdb4332f4450d523f77c /activerecord/test/json_serialization_test.rb
parenta30a1a9d5f06f404421d65393bcf9d73885789cb (diff)
downloadrails-b1968708e12972515fdc8eefdcaff95edbebc76b.tar.gz
rails-b1968708e12972515fdc8eefdcaff95edbebc76b.tar.bz2
rails-b1968708e12972515fdc8eefdcaff95edbebc76b.zip
Hash#to_json takes :only or :except options to specific or omit certain hash keys. Enumerable#to_json passes through its options to each element. Closes #9751.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7736 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test/json_serialization_test.rb')
-rw-r--r--activerecord/test/json_serialization_test.rb36
1 files changed, 35 insertions, 1 deletions
diff --git a/activerecord/test/json_serialization_test.rb b/activerecord/test/json_serialization_test.rb
index d12e7c42c6..eb4f90cf0f 100644
--- a/activerecord/test/json_serialization_test.rb
+++ b/activerecord/test/json_serialization_test.rb
@@ -68,11 +68,12 @@ class DatabaseConnectedJsonEncodingTest < Test::Unit::TestCase
def setup
@david = authors(:david)
+ @mary = authors(:mary)
end
def test_includes_uses_association_name
json = @david.to_json(:include => :posts)
-
+
assert_match %r{"posts": \[}, json
assert_match %r{"id": 1}, json
@@ -140,4 +141,37 @@ class DatabaseConnectedJsonEncodingTest < Test::Unit::TestCase
assert_match %r{"favorite_quote": "Constraints are liberating"}, json
assert_equal %r{"favorite_quote": }.match(json).size, 1
end
+
+ def test_should_allow_only_option_for_list_of_authors
+ authors = [@david, @mary]
+
+ assert_equal %([{"name": "David"}, {"name": "Mary"}]), authors.to_json(:only => :name)
+ end
+
+ def test_should_allow_except_option_for_list_of_authors
+ authors = [@david, @mary]
+
+ assert_equal %([{"id": 1}, {"id": 2}]), authors.to_json(:except => [:name, :author_address_id])
+ end
+
+ def test_should_allow_includes_for_list_of_authors
+ authors = [@david, @mary]
+ json = authors.to_json(
+ :only => :name,
+ :include => {
+ :posts => { :only => :id }
+ }
+ )
+
+ assert_equal %([{"name": "David", "posts": [{"id": 1}, {"id": 2}, {"id": 4}, {"id": 5}, {"id": 6}]}, {"name": "Mary", "posts": [{"id": 7}]}]), json
+ end
+
+ def test_should_allow_options_for_hash_of_authors
+ authors_hash = {
+ 1 => @david,
+ 2 => @mary
+ }
+
+ assert_equal %({1: {"name": "David"}}), authors_hash.to_json(:only => [1, :name])
+ end
end \ No newline at end of file