From 8ca5b14c69f621c1ee227c2fd15407f7642d8ee5 Mon Sep 17 00:00:00 2001 From: Cody Fauser Date: Tue, 7 Oct 2008 12:05:41 -0400 Subject: [PATCH] Fix nested serialization of resources. Add #to_xml and #to_json methods and documentation --- activeresource/lib/active_resource/base.rb | 36 ++++++++++++++++++++ .../lib/active_resource/formats/json_format.rb | 2 +- activeresource/test/format_test.rb | 16 +++++++++ 3 files changed, 53 insertions(+), 1 deletions(-) diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index 74d8128..d59f369 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -854,6 +854,42 @@ module ActiveResource # # my_group.to_xml(:skip_instruct => true) # # => [...] + 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. + # + # * :only - Only include the specified attribute or list of + # attributes in the serialized output. Attribute names must be specified + # as strings. + # * :except - 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] diff --git a/activeresource/lib/active_resource/formats/json_format.rb b/activeresource/lib/active_resource/formats/json_format.rb index 9e269d4..1d88fc5 100644 --- a/activeresource/lib/active_resource/formats/json_format.rb +++ b/activeresource/lib/active_resource/formats/json_format.rb @@ -12,7 +12,7 @@ module ActiveResource end def encode(hash, options={}) - hash.to_json + hash.to_json(options) end def decode(json) diff --git a/activeresource/test/format_test.rb b/activeresource/test/format_test.rb index 365576a..4f3b741 100644 --- a/activeresource/test/format_test.rb +++ b/activeresource/test/format_test.rb @@ -1,5 +1,6 @@ require 'abstract_unit' require "fixtures/person" +require "fixtures/street_address" class FormatTest < Test::Unit::TestCase def setup @@ -83,6 +84,21 @@ class FormatTest < Test::Unit::TestCase assert_equal ActiveResource::Formats[:json], resource.connection.format end + def test_serialization_of_nested_resource + addy = { :street => '12345 Street' } + rus = { :name=> 'Rus', :address => addy} + for format in [ :json, :xml ] + rus_encoded = ActiveResource::Formats[format].encode(rus) + assert_match /12345 Street/, rus_encoded + rus_person = Person.new( rus.update({:address => StreetAddress.new(addy)}) ) + assert_kind_of StreetAddress, rus_person.address + using_format(Person, format) do + ActiveResource::HttpMock.respond_to.post "/people.#{format}", {'Content-Type' => ActiveResource::Formats[format].mime_type}, rus_encoded, 201, {'Location' => "/people/5.#{format}"} + rus_person.save + end + end + end + private def using_format(klass, mime_type_reference) previous_format = klass.format -- 1.5.6.1