From b3f114e46861c79c476ca950adcbdb51e975d5f4 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 b764e57da8cbffd40abaf9da5ab146c777a54561 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 7 May 2010 18:06:02 -0300 Subject: [PATCH 2/3] Allow ActiveModel::Name class to be initialized with optional arguments and add a model_name setter on ActiveModel::Naming module --- activemodel/lib/active_model/naming.rb | 18 +++++++---- activemodel/test/cases/naming_with_params_test.rb | 34 +++++++++++++++++++++ 2 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 activemodel/test/cases/naming_with_params_test.rb diff --git a/activemodel/lib/active_model/naming.rb b/activemodel/lib/active_model/naming.rb index 8cdd3d2..c53b432 100644 --- a/activemodel/lib/active_model/naming.rb +++ b/activemodel/lib/active_model/naming.rb @@ -6,15 +6,15 @@ module ActiveModel attr_reader :singular, :plural, :element, :collection, :partial_path alias_method :cache_key, :collection - def initialize(klass) + def initialize(klass, names = {}) 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 - @human = ActiveSupport::Inflector.humanize(@element).freeze - @collection = ActiveSupport::Inflector.tableize(self).freeze - @partial_path = "#{@collection}/#{@element}".freeze + @singular = names[:singular] || ActiveSupport::Inflector.underscore(self).tr('/', '_').freeze + @plural = names[:plural] || ActiveSupport::Inflector.pluralize(@singular).freeze + @element = names[:element] || ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self)).freeze + @human = names[:human] || ActiveSupport::Inflector.humanize(@element).freeze + @collection = names[:collection] || ActiveSupport::Inflector.tableize(self.sub(/[^:]*$/, @element)).freeze + @partial_path = names[:partial_path] || "#{@collection}/#{@element}".freeze end # Transform the model name into a more humane format, using I18n. By default, @@ -57,6 +57,10 @@ module ActiveModel def model_name @_model_name ||= ActiveModel::Name.new(self) end + + def model_name=(name) + @_model_name = name + end end end diff --git a/activemodel/test/cases/naming_with_params_test.rb b/activemodel/test/cases/naming_with_params_test.rb new file mode 100644 index 0000000..746859b --- /dev/null +++ b/activemodel/test/cases/naming_with_params_test.rb @@ -0,0 +1,34 @@ +require 'cases/helper' +require 'models/track_back' + +class NamingWithParamsTest < ActiveModel::TestCase + def test_with_singular_param + @model_name = ActiveModel::Name.new(Post::TrackBack, :singular => "singular_name") + assert_equal 'singular_name', @model_name.singular + assert_equal 'singular_names', @model_name.plural + end + + def test_with_plural_param + @model_name = ActiveModel::Name.new(Post::TrackBack, :plural => "plural_name") + assert_equal 'plural_name', @model_name.plural + end + + def test_with_element_param + @model_name = ActiveModel::Name.new(Post::TrackBack, :element => "element_name") + assert_equal 'element_name', @model_name.element + assert_equal 'Element name', @model_name.human + assert_equal 'post/element_names', @model_name.collection + assert_equal 'post/element_names/element_name', @model_name.partial_path + end + + def test_with_collection_param + @model_name = ActiveModel::Name.new(Post::TrackBack, :collection => "collection_name") + assert_equal 'collection_name', @model_name.collection + assert_equal 'collection_name/track_back', @model_name.partial_path + end + + def test_with_partial_path_param + @model_name = ActiveModel::Name.new(Post::TrackBack, :partial_path => "partial_path_name") + assert_equal 'partial_path_name', @model_name.partial_path + end +end -- 1.7.0 From b56b4f6f8c03173a6bbb97bead7f48ac01467aad Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 7 May 2010 18:08:05 -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..bd8ba06 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=(element_name) + self.model_name = ActiveModel::Name.new(self, :element => element_name) + end + + def collection_name + model_name.collection + end + + def collection_name=(collection_name) + self.model_name = ActiveModel::Name.new(self, :collection => collection_name) + 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