From 7cd1d37a51f5f53f8fc1360f886d26cabf12d969 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Mon, 26 Apr 2010 18:27:16 -0300 Subject: Reuse Active Model serialization in Active Resource. [#2584 state:committed] Signed-off-by: Jeremy Kemper --- activeresource/lib/active_resource/base.rb | 66 ++---------------------------- 1 file changed, 3 insertions(+), 63 deletions(-) (limited to 'activeresource/lib') diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index bf775a14c8..ad994214f6 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -1176,73 +1176,11 @@ module ActiveResource !new? && self.class.exists?(to_param, :params => prefix_options) end - # Converts the resource to an XML string representation. - # - # ==== Options - # The +options+ parameter is handed off to the +to_xml+ method on each - # attribute, so it has the same options as the +to_xml+ methods in - # Active Support. - # - # * :indent - Set the indent level for the XML output (default is +2+). - # * :dasherize - Boolean option to determine whether or not element names should - # replace underscores with dashes (default is false). - # * :skip_instruct - Toggle skipping the +instruct!+ call on the XML builder - # that generates the XML declaration (default is false). - # - # ==== Examples - # my_group = SubsidiaryGroup.find(:first) - # my_group.to_xml - # # => - # # [...] - # - # my_group.to_xml(:dasherize => true) - # # => - # # [...] - # - # my_group.to_xml(:skip_instruct => true) - # # => [...] - def to_xml(options={}) - attributes.to_xml({:root => self.class.element_name}.merge(options)) - end - - # Coerces to a hash for JSON encoding. - # - # ==== 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 as_json(options = nil) - attributes.as_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::XmlFormat - self.class.format.encode(attributes, {:root => self.class.element_name}.merge(options)) - else - self.class.format.encode(attributes, options) - end + send("to_#{self.class.format.extension}", options) end # A method to \reload the attributes of this object from the remote web service. @@ -1466,5 +1404,7 @@ module ActiveResource class Base extend ActiveModel::Naming include CustomMethods, Observing, Validations + include ActiveModel::Serializers::JSON + include ActiveModel::Serializers::Xml end end -- cgit v1.2.3