From d8f38d2ae02948a21970bd062dc4d702ebefab9c Mon Sep 17 00:00:00 2001 From: Rasik Pandey Date: Tue, 23 Sep 2008 15:44:24 -0400 Subject: [PATCH] fixed lighthouse issue 1011 --- activeresource/lib/active_resource/base.rb | 10 +++++++++- activeresource/test/format_test.rb | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletions(-) diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index d966062..69fe3af 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -854,7 +854,15 @@ module ActiveResource # # my_group.to_xml(:skip_instruct => true) # # => [...] - def encode(options={}) + def to_xml(options={}) + attributes.to_xml({:root => self.class.element_name}.merge(options)) + end + + def to_json(options={}) + attributes.to_json(options) + end + + def encode(options={}) case self.class.format when ActiveResource::Formats[:xml] self.class.format.encode(attributes, {:root => self.class.element_name}.merge(options)) diff --git a/activeresource/test/format_test.rb b/activeresource/test/format_test.rb index 365576a..b07f8ad 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/person" 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.5.3 From 720ae2be4a103f3e65536f90d0fc7a48adac7c16 Mon Sep 17 00:00:00 2001 From: Rasik Pandey Date: Tue, 23 Sep 2008 15:49:04 -0400 Subject: [PATCH] fixed require in formats_test --- activeresource/test/format_test.rb | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-) diff --git a/activeresource/test/format_test.rb b/activeresource/test/format_test.rb index b07f8ad..7b41b52 100644 --- a/activeresource/test/format_test.rb +++ b/activeresource/test/format_test.rb @@ -1,6 +1,6 @@ require 'abstract_unit' require "fixtures/person" -require "fixtures/person" +require "fixtures/street_address" class FormatTest < Test::Unit::TestCase def setup -- 1.5.5.3 From 4fc5b82645a82d232b6a64dec851d8d39b64fbe1 Mon Sep 17 00:00:00 2001 From: Rasik Pandey Date: Tue, 23 Sep 2008 15:51:32 -0400 Subject: [PATCH] fixed options omission in json_format.rb --- .../lib/active_resource/formats/json_format.rb | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-) 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) -- 1.5.5.3