From 170c1ef328fe1ed95873f5eb9c2bbabe8278a4ef Mon Sep 17 00:00:00 2001 From: Will Bryant Date: Wed, 11 Feb 2009 14:49:01 +1300 Subject: [PATCH] Array#wrap should use #to_ary so association collections and named scopes are not re-wrapped --- .../lib/active_support/core_ext/array/wrapper.rb | 9 +++++++-- activesupport/test/core_ext/array_ext_test.rb | 10 ++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/array/wrapper.rb b/activesupport/lib/active_support/core_ext/array/wrapper.rb index 12fd745..80b8f05 100644 --- a/activesupport/lib/active_support/core_ext/array/wrapper.rb +++ b/activesupport/lib/active_support/core_ext/array/wrapper.rb @@ -2,7 +2,8 @@ module ActiveSupport #:nodoc: module CoreExtensions #:nodoc: module Array #:nodoc: module Wrapper - # Wraps the object in an Array unless it's an Array. + # Wraps the object in an Array unless it's an Array. Converts the + # object to an Array using #to_ary if it implements that. def wrap(object) case object when nil @@ -10,7 +11,11 @@ module ActiveSupport #:nodoc: when self object else - [object] + if object.respond_to?(:to_ary) + object.to_ary + else + [object] + end end end end diff --git a/activesupport/test/core_ext/array_ext_test.rb b/activesupport/test/core_ext/array_ext_test.rb index a90d689..4dcc1d5 100644 --- a/activesupport/test/core_ext/array_ext_test.rb +++ b/activesupport/test/core_ext/array_ext_test.rb @@ -304,6 +304,12 @@ class ArrayExtRandomTests < Test::Unit::TestCase end class ArrayWrapperTests < Test::Unit::TestCase + class FakeCollection + def to_ary + ["foo", "bar"] + end + end + def test_array ary = %w(foo bar) assert_same ary, Array.wrap(ary) @@ -325,4 +331,8 @@ class ArrayWrapperTests < Test::Unit::TestCase def test_string_with_newline assert_equal ["foo\nbar"], Array.wrap("foo\nbar") end + + def test_object_with_to_ary + assert_equal ["foo", "bar"], Array.wrap(FakeCollection.new) + end end -- 1.5.5.1