Permalink
Browse files

Fix GH #5063. Support include_root_in_json for ActiveResource properly.

I also removed redundant assigns. Because the default value of include_root_in_json is true.
  • Loading branch information...
kennyj committed Feb 18, 2012
1 parent b4b2f5d commit 06184f9e186da5516d27791ba5067963c2fd46f4
Showing with 15 additions and 3 deletions.
  1. +1 −1 activeresource/lib/active_resource/base.rb
  2. +14 −2 activeresource/test/cases/base_test.rb
@@ -1363,7 +1363,7 @@ def respond_to?(method, include_priv = false)
end
def to_json(options={})
- super({ :root => self.class.element_name }.merge(options))
+ super(include_root_in_json ? { :root => self.class.element_name }.merge(options) : options)
end
def to_xml(options={})
@@ -1018,7 +1018,6 @@ def foo
end
def test_to_json
- Person.include_root_in_json = true
joe = Person.find(6)
encode = joe.encode
json = joe.to_json
@@ -1030,9 +1029,22 @@ def test_to_json
assert_match %r{\}\}$}, json
end
+ def test_to_json_without_root
+ ActiveResource::Base.include_root_in_json = false
+ joe = Person.find(6)
+ encode = joe.encode
+ json = joe.to_json
+
+ assert_equal encode, json
+ assert_match %r{^\{"id":6}, json
+ assert_match %r{"name":"Joe"}, json
+ assert_match %r{"\}$}, json
+ ensure
+ ActiveResource::Base.include_root_in_json = true
+ end
+
def test_to_json_with_element_name
old_elem_name = Person.element_name
- Person.include_root_in_json = true
joe = Person.find(6)
Person.element_name = 'ruby_creator'
encode = joe.encode

0 comments on commit 06184f9

Please sign in to comment.