Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #8308: xml_serialize.patch

File xml_serialize.patch, 2.0 kB (added by mbbx6spp, 2 years ago)

Test Case and Fix included in patch.

  • test/core_ext/array_ext_test.rb

    old new  
    11require File.dirname(__FILE__) + '/../abstract_unit' 
    22require 'bigdecimal' 
    33 
     4module TestNamespace 
     5  class Model  
     6    attr_accessor :id, :name 
     7 
     8    def initialize(id, name) 
     9      @id = id 
     10      @name = name 
     11    end 
     12 
     13    # hacked up to_xml for test purposes 
     14    # basically ignoring both options and block parameters 
     15    def to_xml(options = {}, &block) 
     16      "<test-namespace:model><id>#{@id}</id><name>#{@name}</name></test-namespace:model>" 
     17    end 
     18  end 
     19end 
     20 
    421class ArrayExtToParamTests < Test::Unit::TestCase 
    522  def test_string_array 
    623    assert_equal '', %w().to_param 
     
    179196    assert_match(/^<\?xml [^>]*/, xml) 
    180197    assert_equal 0, xml.rindex(/<\?xml /) 
    181198  end 
     199 
     200  def test_to_xml_with_namespaced_class 
     201    xml = [TestNamespace::Model.new(1, 'MyName')].to_xml(:skip_instruct => true) 
     202    assert_match(/<test-namespace:models>/, xml) 
     203    assert_match(/<\/test-namespace:models>/, xml) 
     204  end 
    182205end 
  • lib/active_support/core_ext/array/conversions.rb

    old new  
    4747        def to_xml(options = {}) 
    4848          raise "Not all elements respond to to_xml" unless all? { |e| e.respond_to? :to_xml } 
    4949 
    50           options[:root]     ||= all? { |e| e.is_a?(first.class) && first.class.to_s != "Hash" } ? first.class.to_s.underscore.pluralize : "records" 
     50          options[:root]     ||= all? { |e| e.is_a?(first.class) && first.class.to_s != "Hash" } ? first.class.to_s.underscore.gsub(/\//, ':').pluralize : "records" 
    5151          options[:children] ||= options[:root].singularize 
    5252          options[:indent]   ||= 2 
    5353          options[:builder]  ||= Builder::XmlMarkup.new(:indent => options[:indent])