From a315d54360999a1b9ffd5a127aa58ed14d8342e3 Mon Sep 17 00:00:00 2001 From: =?utf-8?q?Ernesto=20Jim=C3=A9nez?= Date: Fri, 7 Nov 2008 19:37:48 +0100 Subject: [PATCH] Array#to_xml accepts :root_attrs --- .../active_support/core_ext/array/conversions.rb | 9 ++++++- activesupport/test/core_ext/array_ext_test.rb | 21 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/array/conversions.rb b/activesupport/lib/active_support/core_ext/array/conversions.rb index cf3e03f..958e462 100644 --- a/activesupport/lib/active_support/core_ext/array/conversions.rb +++ b/activesupport/lib/active_support/core_ext/array/conversions.rb @@ -154,6 +154,7 @@ module ActiveSupport #:nodoc: options[:children] ||= options[:root].singularize options[:indent] ||= 2 options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent]) + options[:root_attrs]||= {} root = options.delete(:root).to_s children = options.delete(:children) @@ -162,15 +163,19 @@ module ActiveSupport #:nodoc: root = root.dasherize end + if !options[:skip_types] + options[:root_attrs].merge!(:type => "array") + end + options[:builder].instruct! unless options.delete(:skip_instruct) opts = options.merge({ :root => children }) xml = options[:builder] if empty? - xml.tag!(root, options[:skip_types] ? {} : {:type => "array"}) + xml.tag!(root, options[:root_attrs]) else - xml.tag!(root, options[:skip_types] ? {} : {:type => "array"}) { + xml.tag!(root, options[:root_attrs]) { yield xml if block_given? each { |e| e.to_xml(opts.merge({ :skip_instruct => true })) } } diff --git a/activesupport/test/core_ext/array_ext_test.rb b/activesupport/test/core_ext/array_ext_test.rb index 62a1f61..417add8 100644 --- a/activesupport/test/core_ext/array_ext_test.rb +++ b/activesupport/test/core_ext/array_ext_test.rb @@ -279,6 +279,27 @@ class ArrayToXmlTests < Test::Unit::TestCase assert xml.include?(%(2)), xml end + def test_to_xml_with_root_args_with_skyp_types_true + xml = [ + { :name => "David", :street_address => "Paulina" }, { :name => "Jason", :street_address => "Evergreen" } + ].to_xml(:root_attrs => {:page => 3}, :skip_instruct => true, :skip_types => true, :indent => 0, :dasherize => true) + + assert_equal "", xml.first(26) + assert xml.include?(%(Paulina)) + assert xml.include?(%(Evergreen)) + end + + def test_to_xml_with_root_args_with_skyp_types_false + xml = [ + { :name => "David", :street_address => "Paulina" }, { :name => "Jason", :street_address => "Evergreen" } + ].to_xml(:root_attrs => {:page => 3}, :skip_instruct => true, :skip_types => false, :indent => 0, :dasherize => true) + + assert_match(/^$/,xml.first(39)) + assert_match(/^$/,xml.first(39)) + assert xml.include?(%(Paulina)) + assert xml.include?(%(Evergreen)) + end + def test_to_xml_with_empty xml = [].to_xml assert_match(/type="array"\/>/, xml) -- 1.5.3.8 From 344a478cbad54387e76a224df9a1648134540e18 Mon Sep 17 00:00:00 2001 From: =?utf-8?q?Ernesto=20Jim=C3=A9nez?= Date: Fri, 7 Nov 2008 19:47:17 +0100 Subject: [PATCH] Updated RDoc for Array#to_xml --- .../active_support/core_ext/array/conversions.rb | 17 +++++++++++++++++ 1 files changed, 17 insertions(+), 0 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/array/conversions.rb b/activesupport/lib/active_support/core_ext/array/conversions.rb index 958e462..fc04aff 100644 --- a/activesupport/lib/active_support/core_ext/array/conversions.rb +++ b/activesupport/lib/active_support/core_ext/array/conversions.rb @@ -129,6 +129,23 @@ module ActiveSupport #:nodoc: # # # + # You can define root attributes using :root_attrs option: + # + # @projects.to_xml(:root_attrs => {:page => 1, :per_page => 3}) + # + # + # + # + # project 1 + # + # + # project 2 + # + # + # project 3 + # + # + # # By default root children have as node name the one of the root # singularized. You can change it with the :children option. # -- 1.5.3.8