From bea3c26833ad3e1e94f7331e0553a4e2164e7de5 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 6 May 2010 12:35:08 -0300 Subject: Make ActiveResource serialize XML correctly when element_name is set. [#4529] Signed-off-by: Jeremy Kemper --- activeresource/lib/active_resource/base.rb | 19 ++++++++++++---- activeresource/test/cases/base_test.rb | 36 ++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 4 deletions(-) (limited to 'activeresource') diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index ad994214f6..15d77df3b5 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -551,11 +551,22 @@ module ActiveResource @headers ||= {} end - # Do not include any modules in the default element name. This makes it easier to seclude ARes objects - # in a separate namespace without having to set element_name repeatedly. - attr_accessor_with_default(:element_name) { ActiveSupport::Inflector.underscore(to_s.split("::").last) } #:nodoc: + def element_name + model_name.element + end + + def element_name=(value) + model_name.element = value + end + + def collection_name + model_name.collection + end + + def collection_name=(value) + model_name.collection = value + end - attr_accessor_with_default(:collection_name) { ActiveSupport::Inflector.pluralize(element_name) } #:nodoc: attr_accessor_with_default(:primary_key, 'id') #:nodoc: # Gets the \prefix for a resource's nested URL (e.g., prefix/collectionname/1.xml) diff --git a/activeresource/test/cases/base_test.rb b/activeresource/test/cases/base_test.rb index e5f556962b..5df1f411a4 100644 --- a/activeresource/test/cases/base_test.rb +++ b/activeresource/test/cases/base_test.rb @@ -1018,6 +1018,23 @@ class BaseTest < Test::Unit::TestCase assert xml.include?('1') end + def test_to_xml_with_element_name + old_elem_name = Person.element_name + matz = Person.find(1) + Person.element_name = 'ruby_creator' + encode = matz.encode + xml = matz.to_xml + + assert encode, xml + assert xml.include?('') + assert xml.include?('') + assert xml.include?('Matz') + assert xml.include?('1') + assert xml.include?('') + ensure + Person.element_name = old_elem_name + end + def test_to_json Person.include_root_in_json = true Person.format = :json @@ -1033,6 +1050,25 @@ class BaseTest < Test::Unit::TestCase assert_match %r{\}\}\}$}, json end + def test_to_json_with_element_name + old_elem_name = Person.element_name + Person.include_root_in_json = true + Person.format = :json + joe = Person.find(6) + Person.element_name = 'ruby_creator' + encode = joe.encode + json = joe.to_json + Person.format = :xml + + assert encode, json + assert_match %r{^\{"ruby_creator":\{"person":\{}, json + assert_match %r{"id":6}, json + assert_match %r{"name":"Joe"}, json + assert_match %r{\}\}\}$}, json + ensure + Person.element_name = old_elem_name + end + def test_to_param_quacks_like_active_record new_person = Person.new assert_nil new_person.to_param -- cgit v1.2.3