From 4e8a7377f481b3813c55f8a1e37769d6376ef58d Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Tue, 4 May 2010 22:27:17 -0300 Subject: [PATCH 1/3] Make use of to_xml and to_json in tests --- activeresource/test/cases/base_test.rb | 9 +++++++-- 1 files changed, 7 insertions(+), 2 deletions(-) diff --git a/activeresource/test/cases/base_test.rb b/activeresource/test/cases/base_test.rb index 2ed7e1c..e5f5569 100644 --- a/activeresource/test/cases/base_test.rb +++ b/activeresource/test/cases/base_test.rb @@ -1009,7 +1009,10 @@ class BaseTest < Test::Unit::TestCase def test_to_xml matz = Person.find(1) - xml = matz.encode + encode = matz.encode + xml = matz.to_xml + + assert encode, xml assert xml.include?('') assert xml.include?('Matz') assert xml.include?('1') @@ -1019,9 +1022,11 @@ class BaseTest < Test::Unit::TestCase Person.include_root_in_json = true Person.format = :json joe = Person.find(6) - json = joe.encode + encode = joe.encode + json = joe.to_json Person.format = :xml + assert encode, json assert_match %r{^\{"person":\{"person":\{}, json assert_match %r{"id":6}, json assert_match %r{"name":"Joe"}, json -- 1.7.0 From bda5c4a26bf578fe23a265884602c232a402aeb6 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 6 May 2010 18:27:06 -0300 Subject: [PATCH 2/3] Refactor of active_model/naming.rb and allow collection and element to be writable --- activemodel/lib/active_model/naming.rb | 18 ++++++++++++++---- 1 files changed, 14 insertions(+), 4 deletions(-) diff --git a/activemodel/lib/active_model/naming.rb b/activemodel/lib/active_model/naming.rb index 8cdd3d2..fd9045a 100644 --- a/activemodel/lib/active_model/naming.rb +++ b/activemodel/lib/active_model/naming.rb @@ -3,17 +3,27 @@ require 'active_support/inflector' module ActiveModel class Name < String - attr_reader :singular, :plural, :element, :collection, :partial_path - alias_method :cache_key, :collection + attr_reader :singular, :plural, :partial_path + attr_accessor :element, :collection + alias_method :cache_key, :collection def initialize(klass) super(klass.name) @klass = klass @singular = ActiveSupport::Inflector.underscore(self).tr('/', '_').freeze @plural = ActiveSupport::Inflector.pluralize(@singular).freeze - @element = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self)).freeze + self.element = ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self)).freeze + end + + def element=(element) + @element = element @human = ActiveSupport::Inflector.humanize(@element).freeze - @collection = ActiveSupport::Inflector.tableize(self).freeze + @collection = ActiveSupport::Inflector.tableize(self.sub(/[^:]*$/, @element)).freeze + @partial_path = "#{@collection}/#{@element}".freeze + end + + def collection=(collection) + @collection = collection @partial_path = "#{@collection}/#{@element}".freeze end -- 1.7.0 From 117e40439105bb1ffb56884123cb0cf322521200 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 6 May 2010 12:35:08 -0300 Subject: [PATCH 3/3] Make ActiveResource serialize XML correctly when element_name is setted [#4529 state:committed] --- activemodel/lib/active_model/serializers/xml.rb | 2 +- .../serializeration/xml_serialization_test.rb | 4 +- activeresource/lib/active_resource/base.rb | 19 ++++++++-- 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 df7026b..934af2b 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 3ba826a..4e8e4ef 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{^}, @xml - assert_match %r{$}, @xml + assert_match %r{^}, @xml + assert_match %r{$}, @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 ad99421..15d77df 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 e5f5569..5df1f41 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 -- 1.7.0