mirror of
https://github.com/github/rails.git
synced 2026-01-08 22:27:59 -05:00
Support include_root_in_json for ActiveResource properly.
This commit is a backport from https://github.com/rails/activeresource/pull/29. The ActiveResource's include_root_in_json option is broken for 3.2.x.
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
## unreleased ##
|
||||
|
||||
* No changes.
|
||||
* Fixes an issue that ActiveResource models ignores ActiveResource::Base.include_root_in_json.
|
||||
Backported from the now separate repo rails/activeresouce.
|
||||
|
||||
*Xinjiang Lu*
|
||||
|
||||
## Rails 3.2.13 (Mar 18, 2013) ##
|
||||
|
||||
|
||||
@@ -1336,7 +1336,7 @@ module ActiveResource
|
||||
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={})
|
||||
|
||||
@@ -1020,7 +1020,6 @@ class BaseTest < Test::Unit::TestCase
|
||||
end
|
||||
|
||||
def test_to_json
|
||||
Person.include_root_in_json = true
|
||||
joe = Person.find(6)
|
||||
encode = joe.encode
|
||||
json = joe.to_json
|
||||
@@ -1032,6 +1031,21 @@ class BaseTest < Test::Unit::TestCase
|
||||
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_no_match %r{^\{"person":\}}, 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
|
||||
|
||||
Reference in New Issue
Block a user