diff options
-rw-r--r-- | activemodel/lib/active_model/serializers/xml.rb | 2 | ||||
-rw-r--r-- | activemodel/test/cases/serializeration/xml_serialization_test.rb | 4 | ||||
-rw-r--r-- | activeresource/lib/active_resource/base.rb | 19 | ||||
-rw-r--r-- | activeresource/test/cases/base_test.rb | 36 |
4 files changed, 54 insertions, 7 deletions
diff --git a/activemodel/lib/active_model/serializers/xml.rb b/activemodel/lib/active_model/serializers/xml.rb index df7026b3ec..934af2b8a8 100644 --- a/activemodel/lib/active_model/serializers/xml.rb +++ b/activemodel/lib/active_model/serializers/xml.rb @@ -90,7 +90,7 @@ module ActiveModel @builder = options[:builder] @builder.instruct! unless options[:skip_instruct] - root = (options[:root] || @serializable.class.model_name.singular).to_s + root = (options[:root] || @serializable.class.model_name.element).to_s root = ActiveSupport::XmlMini.rename_key(root, options) args = [root] diff --git a/activemodel/test/cases/serializeration/xml_serialization_test.rb b/activemodel/test/cases/serializeration/xml_serialization_test.rb index 3ba826a8d0..4e8e4efa25 100644 --- a/activemodel/test/cases/serializeration/xml_serialization_test.rb +++ b/activemodel/test/cases/serializeration/xml_serialization_test.rb @@ -37,8 +37,8 @@ class XmlSerializationTest < ActiveModel::TestCase test "should serialize namespaced root" do @xml = Admin::Contact.new(@contact.attributes).to_xml - assert_match %r{^<admin-contact>}, @xml - assert_match %r{</admin-contact>$}, @xml + assert_match %r{^<contact>}, @xml + assert_match %r{</contact>$}, @xml end test "should serialize default root with namespace" do 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., <tt>prefix/collectionname/1.xml</tt>) 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?('<id type="integer">1</id>') 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?('<?xml version="1.0" encoding="UTF-8"?>') + assert xml.include?('<ruby-creator>') + assert xml.include?('<name>Matz</name>') + assert xml.include?('<id type="integer">1</id>') + assert xml.include?('</ruby-creator>') + 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 |