Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Support include_root_in_json for ActiveResource properly.

  • Loading branch information...
commit 2ae7772469f8d9f52f1579346c260abf93f654ef 1 parent 91dfd86
@kennyj kennyj authored
Showing with 15 additions and 3 deletions.
  1. +1 −1  lib/active_resource/base.rb
  2. +14 −2 test/cases/base_test.rb
View
2  lib/active_resource/base.rb
@@ -1375,7 +1375,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={})
View
16 test/cases/base_test.rb
@@ -1077,7 +1077,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
@@ -1089,9 +1088,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
Please sign in to comment.
Something went wrong with that request. Please try again.