diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2006-03-09 21:12:28 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2006-03-09 21:12:28 +0000 |
commit | d872281975b2bdcfcd06e21b55d78c8fb53ba5d1 (patch) | |
tree | 8d380e62fc9df44a12d0132dfcdd58046c05ab8a /activesupport/lib | |
parent | 966c276d6071f8c331f75820f8c2f30d1bba02b2 (diff) | |
download | rails-d872281975b2bdcfcd06e21b55d78c8fb53ba5d1.tar.gz rails-d872281975b2bdcfcd06e21b55d78c8fb53ba5d1.tar.bz2 rails-d872281975b2bdcfcd06e21b55d78c8fb53ba5d1.zip |
Fixed to_xml across the board to use nice indention, better skip_attributes workings, no type on strings, and cleaned tests [DHH]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3829 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/core_ext/array/conversions.rb | 11 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/hash/conversions.rb | 18 |
2 files changed, 19 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 faa995bc9c..6f83231487 100644 --- a/activesupport/lib/active_support/core_ext/array/conversions.rb +++ b/activesupport/lib/active_support/core_ext/array/conversions.rb @@ -28,9 +28,14 @@ module ActiveSupport #:nodoc: def to_xml(options = {}) raise "Not all elements respond to to_xml" unless all? { |e| e.respond_to? :to_xml } - options[:root] ||= all? { |e| e.is_a? first.class } ? first.class.to_s.underscore.pluralize : "records" - xml = options[:builder] || Builder::XmlMarkup.new - xml.__send__(options[:root]) { each { |e| e.to_xml(:builder => xml) } } + + options[:root] ||= all? { |e| e.is_a? first.class } ? first.class.to_s.underscore.pluralize : "records" + options[:indent] ||= 2 + options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent]) + + options[:builder].instruct! unless options.delete(:skip_instruct) + root = options.delete(:root) + options[:builder].__send__(root) { each { |e| e.to_xml(options.merge({ :skip_instruct => true })) } } end end end diff --git a/activesupport/lib/active_support/core_ext/hash/conversions.rb b/activesupport/lib/active_support/core_ext/hash/conversions.rb index 3218f6302c..584a7ec7cc 100644 --- a/activesupport/lib/active_support/core_ext/hash/conversions.rb +++ b/activesupport/lib/active_support/core_ext/hash/conversions.rb @@ -3,10 +3,11 @@ module ActiveSupport #:nodoc: module Hash #:nodoc: module Conversions XML_TYPE_NAMES = { - "String" => "string", - "Fixnum" => "integer", - "Date" => "date", - "Time" => "datetime" + "Fixnum" => "integer", + "Date" => "date", + "Time" => "datetime", + "TrueClass" => "boolean", + "FalseClass" => "boolean" } XML_FORMATTING = { @@ -15,19 +16,22 @@ module ActiveSupport #:nodoc: } def to_xml(options = {}) - options.reverse_merge!({ :builder => Builder::XmlMarkup.new, :root => "hash" }) + options[:indent] ||= 2 + options.reverse_merge!({ :builder => Builder::XmlMarkup.new(:indent => options[:indent]), :root => "hash" }) + options[:builder].instruct! unless options.delete(:skip_instruct) options[:builder].__send__(options[:root]) do for key in keys value = self[key] if value.is_a?(self.class) - value.to_xml(:builder => options[:builder], :root => key) + value.to_xml(options.merge({ :root => key, :skip_instruct => true })) else type_name = XML_TYPE_NAMES[value.class.to_s] + options[:builder].__send__(key.to_s.dasherize, XML_FORMATTING[type_name] ? XML_FORMATTING[type_name].call(value) : value, - value.nil? ? { } : { :type => type_name } + options[:skip_types] || value.nil? || type_name.nil? ? { } : { :type => type_name } ) end end |