From fbbf0086ca8f13f76d2969f655bb1bfc2f4eb4d6 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Thu, 12 Nov 2009 17:21:41 -0800 Subject: Ruby 1.9.2: avoid #flatten --- .../active_record/associations/association_proxy.rb | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) (limited to 'activerecord/lib/active_record/associations') diff --git a/activerecord/lib/active_record/associations/association_proxy.rb b/activerecord/lib/active_record/associations/association_proxy.rb index 75218c01d2..9b96ec0cf4 100644 --- a/activerecord/lib/active_record/associations/association_proxy.rb +++ b/activerecord/lib/active_record/associations/association_proxy.rb @@ -256,10 +256,22 @@ module ActiveRecord end end - # Array#flatten has problems with recursive arrays. Going one level - # deeper solves the majority of the problems. - def flatten_deeper(array) - array.collect { |element| (element.respond_to?(:flatten) && !element.is_a?(Hash)) ? element.flatten : element }.flatten + if RUBY_VERSION < '1.9.2' + # Array#flatten has problems with recursive arrays before Ruby 1.9.2. + # Going one level deeper solves the majority of the problems. + def flatten_deeper(array) + array.collect { |element| (element.respond_to?(:flatten) && !element.is_a?(Hash)) ? element.flatten : element }.flatten + end + else + def flatten_deeper(array) + array.sum [] do |elem| + if elem.respond_to?(:each) + flatten_deeper(elem) + else + Array.wrap(elem) + end + end + end end # Returns the ID of the owner, quoted if needed. -- cgit v1.2.3