From 76d13a66dba5d8731b3be843a509c4f376eebfac Mon Sep 17 00:00:00 2001 From: Adam Sindelar Date: Tue, 6 May 2008 17:50:47 +0200 Subject: [PATCH] Fixing to_xml serialization in some rare cases It is possible for a model to overwrite its to_xml method in a way that'll make sure some specific association is always included with that model. This can cause problems if more than one kind of object is included in an array that's being serialized though, due to the way that the options hash is distributed to the objects. This patch aims to fix that behavior. See lighthouse ticket #123 for more information. --- .../active_support/core_ext/array/conversions.rb | 16 +++++++++++++++- 1 files changed, 15 insertions(+), 1 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/array/conversions.rb b/activesupport/lib/active_support/core_ext/array/conversions.rb index a988282..0db01cf 100644 --- a/activesupport/lib/active_support/core_ext/array/conversions.rb +++ b/activesupport/lib/active_support/core_ext/array/conversions.rb @@ -161,13 +161,27 @@ module ActiveSupport #:nodoc: opts = options.merge({ :root => children }) + classes = collect {|e| e.class}.uniq + if(classes.length != 1) + cloned_options = {} + classes.each do |class_name| + cloned_options[class_name] = opts.clone + end + end + xml = options[:builder] if empty? xml.tag!(root, options[:skip_types] ? {} : {:type => "array"}) else xml.tag!(root, options[:skip_types] ? {} : {:type => "array"}) { yield xml if block_given? - each { |e| e.to_xml(opts.merge!({ :skip_instruct => true })) } + each do |e| + if(classes.length == 1) + e.to_xml(opts.merge!({ :skip_instruct => true })) + else + e.to_xml(cloned_options[e.class].merge!({ :skip_instruct => true})) + end + end } end end -- 1.5.5.1