Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
Cody Fauser authored lifo committed
View
2  activeresource/CHANGELOG
@@ -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)
View
36 activeresource/lib/active_resource/base.rb
@@ -854,6 +854,42 @@ def exists?
#
# 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]
View
2  activeresource/lib/active_resource/formats/json_format.rb
@@ -12,7 +12,7 @@ def mime_type
end
def encode(hash, options={})
- hash.to_json
+ hash.to_json(options)
end
def decode(json)
View
17 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,22 @@ def test_setting_format_before_site
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
Please sign in to comment.
Something went wrong with that request. Please try again.