Skip to content
This repository
Browse code

Add ActiveResource::Base#to_xml and ActiveResource::Base#to_json meth…

…ods. [#1011 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information...
commit f2c10f27565318b5efbdc57fb608b2afe9ae445d 1 parent dce6ade
authored October 07, 2008 lifo committed October 07, 2008
2  activeresource/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *Edge*
2 2
 
  3
+* Add ActiveResource::Base#to_xml and ActiveResource::Base#to_json. #1011 [Rasik Pandey, Cody Fauser]
  4
+
3 5
 * Add ActiveResource::Base.find(:last). [#754 state:resolved] (Adrian Mugnolo)
4 6
 
5 7
 * Fixed problems with the logger used if the logging string included %'s [#840 state:resolved] (Jamis Buck)
36  activeresource/lib/active_resource/base.rb
@@ -854,6 +854,42 @@ def exists?
854 854
     #
855 855
     #   my_group.to_xml(:skip_instruct => true)
856 856
     #   # => <subsidiary_group> [...] </subsidiary_group>
  857
+    def to_xml(options={})
  858
+      attributes.to_xml({:root => self.class.element_name}.merge(options))
  859
+    end
  860
+
  861
+    # Returns a JSON string representing the model. Some configuration is
  862
+    # available through +options+.
  863
+    #
  864
+    # ==== Options
  865
+    # The +options+ are passed to the +to_json+ method on each
  866
+    # attribute, so the same options as the +to_json+ methods in
  867
+    # Active Support.
  868
+    #
  869
+    # * <tt>:only</tt> - Only include the specified attribute or list of
  870
+    #   attributes in the serialized output. Attribute names must be specified
  871
+    #   as strings.
  872
+    # * <tt>:except</tt> - Do not include the specified attribute or list of
  873
+    #   attributes in the serialized output. Attribute names must be specified
  874
+    #   as strings.
  875
+    #
  876
+    # ==== Examples
  877
+    #   person = Person.new(:first_name => "Jim", :last_name => "Smith")
  878
+    #   person.to_json
  879
+    #   # => {"first_name": "Jim", "last_name": "Smith"}
  880
+    #
  881
+    #   person.to_json(:only => ["first_name"])
  882
+    #   # => {"first_name": "Jim"}
  883
+    #
  884
+    #   person.to_json(:except => ["first_name"])
  885
+    #   # => {"last_name": "Smith"}
  886
+    def to_json(options={})
  887
+      attributes.to_json(options)
  888
+    end
  889
+
  890
+    # Returns the serialized string representation of the resource in the configured
  891
+    # serialization format specified in ActiveResource::Base.format. The options
  892
+    # applicable depend on the configured encoding format.
857 893
     def encode(options={})
858 894
       case self.class.format
859 895
         when ActiveResource::Formats[:xml]
2  activeresource/lib/active_resource/formats/json_format.rb
@@ -12,7 +12,7 @@ def mime_type
12 12
       end
13 13
 
14 14
       def encode(hash, options={})
15  
-        hash.to_json
  15
+        hash.to_json(options)
16 16
       end
17 17
 
18 18
       def decode(json)
17  activeresource/test/format_test.rb
... ...
@@ -1,5 +1,6 @@
1 1
 require 'abstract_unit'
2 2
 require "fixtures/person"
  3
+require "fixtures/street_address"
3 4
 
4 5
 class FormatTest < Test::Unit::TestCase
5 6
   def setup
@@ -83,6 +84,22 @@ def test_setting_format_before_site
83 84
     assert_equal ActiveResource::Formats[:json], resource.connection.format
84 85
   end
85 86
 
  87
+  def test_serialization_of_nested_resource
  88
+   address  = { :street => '12345 Street' }
  89
+   person  = { :name=> 'Rus', :address => address}
  90
+
  91
+   [:json, :xml].each do |format|
  92
+     encoded_person = ActiveResource::Formats[format].encode(person)
  93
+     assert_match /12345 Street/, encoded_person
  94
+     remote_person = Person.new(person.update({:address => StreetAddress.new(address)}))
  95
+     assert_kind_of StreetAddress, remote_person.address
  96
+     using_format(Person, format) do
  97
+       ActiveResource::HttpMock.respond_to.post "/people.#{format}", {'Content-Type' => ActiveResource::Formats[format].mime_type}, encoded_person, 201, {'Location' => "/people/5.#{format}"}
  98
+       remote_person.save
  99
+     end
  100
+   end
  101
+ end
  102
+
86 103
   private
87 104
     def using_format(klass, mime_type_reference)
88 105
       previous_format = klass.format

0 notes on commit f2c10f2

Please sign in to comment.
Something went wrong with that request. Please try again.