Skip to content

Commit

Permalink
Add ActiveResource::Base#to_xml and ActiveResource::Base#to_json meth…
Browse files Browse the repository at this point in the history
…ods. [#1011 state:resolved]

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
  • Loading branch information
Cody Fauser authored and lifo committed Oct 7, 2008
1 parent dce6ade commit f2c10f2
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
2 changes: 2 additions & 0 deletions 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)
Expand Down
36 changes: 36 additions & 0 deletions activeresource/lib/active_resource/base.rb
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion activeresource/lib/active_resource/formats/json_format.rb
Expand Up @@ -12,7 +12,7 @@ def mime_type
end

def encode(hash, options={})
hash.to_json
hash.to_json(options)
end

def decode(json)
Expand Down
17 changes: 17 additions & 0 deletions 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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f2c10f2

Please sign in to comment.