From 21644a9976842d6740f94fa1ab14965690e0f8c1 Mon Sep 17 00:00:00 2001 From: Andrew Briening Date: Sun, 2 Aug 2009 12:17:55 -0400 Subject: [PATCH] Added always_yield option to Array#to_xml, allows yielding to the block even when empty? --- .../active_support/core_ext/array/conversions.rb | 7 +++-- activesupport/test/core_ext/array_ext_test.rb | 28 +++++++++++++++----- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/array/conversions.rb b/activesupport/lib/active_support/core_ext/array/conversions.rb index 11846f2..f844854 100644 --- a/activesupport/lib/active_support/core_ext/array/conversions.rb +++ b/activesupport/lib/active_support/core_ext/array/conversions.rb @@ -164,8 +164,9 @@ class Array options[:indent] ||= 2 options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent]) - root = options.delete(:root).to_s - children = options.delete(:children) + root = options.delete(:root).to_s + children = options.delete(:children) + always_yield = options.delete(:always_yield) || false if !options.has_key?(:dasherize) || options[:dasherize] root = root.dasherize @@ -176,7 +177,7 @@ class Array opts = options.merge({ :root => children }) xml = options[:builder] - if empty? + if empty? && always_yield == false xml.tag!(root, options[:skip_types] ? {} : {:type => "array"}) else xml.tag!(root, options[:skip_types] ? {} : {:type => "array"}) { diff --git a/activesupport/test/core_ext/array_ext_test.rb b/activesupport/test/core_ext/array_ext_test.rb index 24d3389..8263939 100644 --- a/activesupport/test/core_ext/array_ext_test.rb +++ b/activesupport/test/core_ext/array_ext_test.rb @@ -17,10 +17,10 @@ class ArrayExtAccessTests < Test::Unit::TestCase assert_equal %w( a b c ), %w( a b c d ).to(2) assert_equal %w( a b c d ), %w( a b c d ).to(10) end - + def test_second_through_tenth array = (1..42).to_a - + assert_equal array[1], array.second assert_equal array[2], array.third assert_equal array[3], array.fourth @@ -53,7 +53,7 @@ end class ArrayExtToSentenceTests < Test::Unit::TestCase include ActiveSupport::Testing::Deprecation - + def test_plain_array_to_sentence assert_equal "", [].to_sentence assert_equal "one", ['one'].to_sentence @@ -65,11 +65,11 @@ class ArrayExtToSentenceTests < Test::Unit::TestCase assert_deprecated(":connector has been deprecated. Use :words_connector instead") do assert_equal "one, two, three", ['one', 'two', 'three'].to_sentence(:connector => '') end - + assert_deprecated(":connector has been deprecated. Use :words_connector instead") do assert_equal "one, two, and three", ['one', 'two', 'three'].to_sentence(:connector => 'and ') end - + assert_equal "one two, and three", ['one', 'two', 'three'].to_sentence(:words_connector => ' ') assert_equal "one & two, and three", ['one', 'two', 'three'].to_sentence(:words_connector => ' & ') assert_equal "onetwo, and three", ['one', 'two', 'three'].to_sentence(:words_connector => nil) @@ -79,11 +79,11 @@ class ArrayExtToSentenceTests < Test::Unit::TestCase assert_deprecated(":skip_last_comma has been deprecated. Use :last_word_connector instead") do assert_equal "one, two and three", ['one', 'two', 'three'].to_sentence(:skip_last_comma => true) end - + assert_deprecated(":skip_last_comma has been deprecated. Use :last_word_connector instead") do assert_equal "one, two, and three", ['one', 'two', 'three'].to_sentence(:skip_last_comma => false) end - + assert_equal "one, two, and also three", ['one', 'two', 'three'].to_sentence(:last_word_connector => ', and also ') assert_equal "one, twothree", ['one', 'two', 'three'].to_sentence(:last_word_connector => nil) assert_equal "one, two three", ['one', 'two', 'three'].to_sentence(:last_word_connector => ' ') @@ -302,6 +302,20 @@ class ArrayToXmlTests < Test::Unit::TestCase xml = [].to_xml assert_match(/type="array"\/>/, xml) end + + def test_to_xml_with_empty_and_block + xml = [].to_xml(:indent => 0) do |builder| + builder.count 0 + end + assert !xml.include?(%(0)), xml + end + + def test_to_xml_with_empty_and_always_yield_option + xml = [].to_xml(:always_yield => true, :indent => 0) do |builder| + builder.count 0 + end + assert xml.include?(%(0)), xml + end end class ArrayExtractOptionsTests < Test::Unit::TestCase -- 1.6.0.2 From bccb71e5cedb78be3d4c72bb8360738b21da7d35 Mon Sep 17 00:00:00 2001 From: Andrew Briening Date: Wed, 5 Aug 2009 10:59:56 -0400 Subject: [PATCH] Allow passing attributes to the root element of Array#to_xml --- .../active_support/core_ext/array/conversions.rb | 10 ++++++---- activesupport/test/core_ext/array_ext_test.rb | 12 ++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/array/conversions.rb b/activesupport/lib/active_support/core_ext/array/conversions.rb index f844854..7fa771f 100644 --- a/activesupport/lib/active_support/core_ext/array/conversions.rb +++ b/activesupport/lib/active_support/core_ext/array/conversions.rb @@ -167,6 +167,7 @@ class Array root = options.delete(:root).to_s children = options.delete(:children) always_yield = options.delete(:always_yield) || false + root_attrs = options.delete(:attributes) || {} if !options.has_key?(:dasherize) || options[:dasherize] root = root.dasherize @@ -177,13 +178,14 @@ class Array opts = options.merge({ :root => children }) xml = options[:builder] - if empty? && always_yield == false - xml.tag!(root, options[:skip_types] ? {} : {:type => "array"}) - else - xml.tag!(root, options[:skip_types] ? {} : {:type => "array"}) { + + block = + if always_yield || any? + lambda{ yield xml if block_given? each { |e| e.to_xml(opts.merge({ :skip_instruct => true })) } } end + xml.tag!(root, options[:skip_types] ? {} : {:type => "array"}, root_attrs, &block) end end diff --git a/activesupport/test/core_ext/array_ext_test.rb b/activesupport/test/core_ext/array_ext_test.rb index 8263939..2fdb085 100644 --- a/activesupport/test/core_ext/array_ext_test.rb +++ b/activesupport/test/core_ext/array_ext_test.rb @@ -316,6 +316,18 @@ class ArrayToXmlTests < Test::Unit::TestCase end assert xml.include?(%(0)), xml end + + def test_to_xml_with_attributes_option + xml = [].to_xml(:attributes => {:an_attribute => "on records"}) + assert xml.include?(%(an_attribute="on records")), xml + end + + def test_to_xml_with_attributes_option_merged_over_defaults + xml = [].to_xml(:attributes => {:type => "collection"}) + assert xml.include?(%(type="collection")), xml + assert !xml.include?(%(type="array")), xml + end + end class ArrayExtractOptionsTests < Test::Unit::TestCase -- 1.6.0.2