From 903637f5f0a1a9789fa12da1519e028b9faa37d8 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 12 May 2010 15:31:37 -0300 Subject: [PATCH] Fixes to_json and to_xml for ActiveResource MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: José Valim --- activeresource/lib/active_resource/base.rb | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index 15d77df3b5..ffdb5fb50a 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -552,19 +552,20 @@ module ActiveResource end def element_name - model_name.element + @element_name ||= model_name.element end - def element_name=(value) - model_name.element = value + def element_name=(element_name) + @element_name = element_name + @collection_name ||= model_name.collection.sub(/[^\/]*$/, @element_name.pluralize) end def collection_name - model_name.collection + @collection_name ||= model_name.collection end - def collection_name=(value) - model_name.collection = value + def collection_name=(collection_name) + @collection_name = collection_name end attr_accessor_with_default(:primary_key, 'id') #:nodoc: @@ -1306,6 +1307,14 @@ module ActiveResource end end + def to_json(options={}) + super({ :root => self.class.element_name }.merge(options)) + end + + def to_xml(options={}) + super({ :root => self.class.element_name }.merge(options)) + end + protected def connection(refresh = false) self.class.connection(refresh)