aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2013-05-15 18:00:11 -0700
committerRafael Mendonça França <rafaelmfranca@gmail.com>2013-05-15 18:00:11 -0700
commit0d8dbd131b551051ce35379093832fa12fb0cf10 (patch)
tree60b73a3144601eb2a6d6df5a01b1a58c65d4a4bd
parent5b020fa1101b878e5f3b8b8a5e7d47e39b505b1e (diff)
parentec55866e398feb1a52701027ab86857b62622ab6 (diff)
downloadrails-0d8dbd131b551051ce35379093832fa12fb0cf10.tar.gz
rails-0d8dbd131b551051ce35379093832fa12fb0cf10.tar.bz2
rails-0d8dbd131b551051ce35379093832fa12fb0cf10.zip
Merge pull request #10578 from dingle/lu/back_port_to_json_fix
Support include_root_in_json for ActiveResource properly.
-rw-r--r--activeresource/CHANGELOG.md4
-rw-r--r--activeresource/lib/active_resource/base.rb2
-rw-r--r--activeresource/test/cases/base_test.rb16
3 files changed, 19 insertions, 3 deletions
diff --git a/activeresource/CHANGELOG.md b/activeresource/CHANGELOG.md
index 77419bb904..1d084ec5d9 100644
--- a/activeresource/CHANGELOG.md
+++ b/activeresource/CHANGELOG.md
@@ -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) ##
diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb
index 032a245c3c..f9cc7d03c8 100644
--- a/activeresource/lib/active_resource/base.rb
+++ b/activeresource/lib/active_resource/base.rb
@@ -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={})
diff --git a/activeresource/test/cases/base_test.rb b/activeresource/test/cases/base_test.rb
index 983f0541a8..9176b5db93 100644
--- a/activeresource/test/cases/base_test.rb
+++ b/activeresource/test/cases/base_test.rb
@@ -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