mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add ActiveResource::Base#to_xml and ActiveResource::Base#to_json methods. [#1011 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
This commit is contained in:
parent
dce6ade4cd
commit
f2c10f2756
4 changed files with 56 additions and 1 deletions
|
@ -1,5 +1,7 @@
|
|||
*Edge*
|
||||
|
||||
* Add ActiveResource::Base#to_xml and ActiveResource::Base#to_json. #1011 [Rasik Pandey, Cody Fauser]
|
||||
|
||||
* Add ActiveResource::Base.find(:last). [#754 state:resolved] (Adrian Mugnolo)
|
||||
|
||||
* Fixed problems with the logger used if the logging string included %'s [#840 state:resolved] (Jamis Buck)
|
||||
|
|
|
@ -854,6 +854,42 @@ module ActiveResource
|
|||
#
|
||||
# my_group.to_xml(:skip_instruct => true)
|
||||
# # => <subsidiary_group> [...] </subsidiary_group>
|
||||
def to_xml(options={})
|
||||
attributes.to_xml({:root => self.class.element_name}.merge(options))
|
||||
end
|
||||
|
||||
# Returns a JSON string representing the model. Some configuration is
|
||||
# available through +options+.
|
||||
#
|
||||
# ==== Options
|
||||
# The +options+ are passed to the +to_json+ method on each
|
||||
# attribute, so the same options as the +to_json+ methods in
|
||||
# Active Support.
|
||||
#
|
||||
# * <tt>:only</tt> - Only include the specified attribute or list of
|
||||
# attributes in the serialized output. Attribute names must be specified
|
||||
# as strings.
|
||||
# * <tt>:except</tt> - Do not include the specified attribute or list of
|
||||
# attributes in the serialized output. Attribute names must be specified
|
||||
# as strings.
|
||||
#
|
||||
# ==== Examples
|
||||
# person = Person.new(:first_name => "Jim", :last_name => "Smith")
|
||||
# person.to_json
|
||||
# # => {"first_name": "Jim", "last_name": "Smith"}
|
||||
#
|
||||
# person.to_json(:only => ["first_name"])
|
||||
# # => {"first_name": "Jim"}
|
||||
#
|
||||
# person.to_json(:except => ["first_name"])
|
||||
# # => {"last_name": "Smith"}
|
||||
def to_json(options={})
|
||||
attributes.to_json(options)
|
||||
end
|
||||
|
||||
# Returns the serialized string representation of the resource in the configured
|
||||
# serialization format specified in ActiveResource::Base.format. The options
|
||||
# applicable depend on the configured encoding format.
|
||||
def encode(options={})
|
||||
case self.class.format
|
||||
when ActiveResource::Formats[:xml]
|
||||
|
|
|
@ -12,7 +12,7 @@ module ActiveResource
|
|||
end
|
||||
|
||||
def encode(hash, options={})
|
||||
hash.to_json
|
||||
hash.to_json(options)
|
||||
end
|
||||
|
||||
def decode(json)
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
require 'abstract_unit'
|
||||
require "fixtures/person"
|
||||
require "fixtures/street_address"
|
||||
|
||||
class FormatTest < Test::Unit::TestCase
|
||||
def setup
|
||||
|
@ -83,6 +84,22 @@ class FormatTest < Test::Unit::TestCase
|
|||
assert_equal ActiveResource::Formats[:json], resource.connection.format
|
||||
end
|
||||
|
||||
def test_serialization_of_nested_resource
|
||||
address = { :street => '12345 Street' }
|
||||
person = { :name=> 'Rus', :address => address}
|
||||
|
||||
[:json, :xml].each do |format|
|
||||
encoded_person = ActiveResource::Formats[format].encode(person)
|
||||
assert_match /12345 Street/, encoded_person
|
||||
remote_person = Person.new(person.update({:address => StreetAddress.new(address)}))
|
||||
assert_kind_of StreetAddress, remote_person.address
|
||||
using_format(Person, format) do
|
||||
ActiveResource::HttpMock.respond_to.post "/people.#{format}", {'Content-Type' => ActiveResource::Formats[format].mime_type}, encoded_person, 201, {'Location' => "/people/5.#{format}"}
|
||||
remote_person.save
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def using_format(klass, mime_type_reference)
|
||||
previous_format = klass.format
|
||||
|
|
Loading…
Reference in a new issue