mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Add ActiveResource::Base.include_root_in_json, like Active Record, to serialize instances as hash of model name -> attributes hash rather than the bare attributes hash. [#2584 state:committed]
Signed-off-by: Santiago Pastorino <santiago@wyeworks.com> Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
This commit is contained in:
committed by
Jeremy Kemper
parent
9e262de3d8
commit
aa401bd75a
@@ -1,5 +1,7 @@
|
||||
*2.3.6 (unreleased)*
|
||||
|
||||
* JSON: set Base.include_root_in_json = true to include a root value in the JSON: {"post": {"title": ...}}. Mirrors the Active Record option. #2584 [Matthew Moore, Joe Martinez, Elad Meidar, Santiago Pastorino]
|
||||
|
||||
* Ruby 1.9: ERB template encoding using a magic comment at the top of the file. [Jeremy Kemper]
|
||||
<%# encoding: utf-8 %>
|
||||
|
||||
|
||||
@@ -227,6 +227,9 @@ module ActiveResource
|
||||
# The logger for diagnosing and tracing Active Resource calls.
|
||||
cattr_accessor :logger
|
||||
|
||||
# Controls the top-level behavior of JSON serialization
|
||||
cattr_accessor :include_root_in_json, :instance_writer => false
|
||||
|
||||
class << self
|
||||
# Gets the URI of the REST resources to map for this class. The site variable is required for
|
||||
# Active Resource's mapping to work.
|
||||
@@ -971,6 +974,12 @@ module ActiveResource
|
||||
case self.class.format
|
||||
when ActiveResource::Formats[:xml]
|
||||
self.class.format.encode(attributes, {:root => self.class.element_name}.merge(options))
|
||||
when ActiveResource::Formats::JsonFormat
|
||||
if ActiveResource::Base.include_root_in_json
|
||||
self.class.format.encode({self.class.element_name => attributes}, options)
|
||||
else
|
||||
self.class.format.encode(attributes, options)
|
||||
end
|
||||
else
|
||||
self.class.format.encode(attributes, options)
|
||||
end
|
||||
|
||||
@@ -4,6 +4,7 @@ require "fixtures/customer"
|
||||
require "fixtures/street_address"
|
||||
require "fixtures/beast"
|
||||
require "fixtures/proxy"
|
||||
require 'active_support/json'
|
||||
|
||||
class BaseTest < Test::Unit::TestCase
|
||||
def setup
|
||||
@@ -13,6 +14,7 @@ class BaseTest < Test::Unit::TestCase
|
||||
@addy = { :id => 1, :street => '12345 Street' }.to_xml(:root => 'address')
|
||||
@default_request_headers = { 'Content-Type' => 'application/xml' }
|
||||
@rick = { :name => "Rick", :age => 25 }.to_xml(:root => "person")
|
||||
@joe = {'person' => { :id => 6, :name => 'Joe' }}.to_json
|
||||
@people = [{ :id => 1, :name => 'Matz' }, { :id => 2, :name => 'David' }].to_xml(:root => 'people')
|
||||
@people_david = [{ :id => 2, :name => 'David' }].to_xml(:root => 'people')
|
||||
@addresses = [{ :id => 1, :street => '12345 Street' }].to_xml(:root => 'addresses')
|
||||
@@ -64,6 +66,7 @@ class BaseTest < Test::Unit::TestCase
|
||||
ActiveResource::HttpMock.respond_to do |mock|
|
||||
mock.get "/people/1.xml", {}, @matz
|
||||
mock.get "/people/2.xml", {}, @david
|
||||
mock.get "/people/6.json", {}, @joe
|
||||
mock.get "/people/5.xml", {}, @marty
|
||||
mock.get "/people/Greg.xml", {}, @greg
|
||||
mock.get "/people/4.xml", {'key' => 'value'}, nil, 404
|
||||
@@ -1005,6 +1008,18 @@ class BaseTest < Test::Unit::TestCase
|
||||
assert xml.include?('<id type="integer">1</id>')
|
||||
end
|
||||
|
||||
def test_to_json_including_root
|
||||
Person.include_root_in_json = true
|
||||
Person.format = :json
|
||||
joe = Person.find(6)
|
||||
json = joe.encode
|
||||
assert_match '{"person":{"person":{', json
|
||||
assert_match '"name":"Joe"', json
|
||||
assert_match '"id":6', json
|
||||
ensure
|
||||
Person.format = :xml
|
||||
end
|
||||
|
||||
def test_to_param_quacks_like_active_record
|
||||
new_person = Person.new
|
||||
assert_nil new_person.to_param
|
||||
|
||||
Reference in New Issue
Block a user