From 1a90745111d242630fcf9547392ad821a0a6cc25 Mon Sep 17 00:00:00 2001 From: James Ravn Date: Sun, 18 Jan 2009 18:06:17 -0600 Subject: [PATCH] Correctly handle unsupported iconv libraries. * The Iconv library raises an exception on Solaris, AIX, and other platforms when ActiveSupport::Inflector.transliterate is defined. This fix rescues from the exception so that the native transliterate method can be defined. --- activesupport/lib/active_support/inflector.rb | 17 ++++++++++++----- activesupport/test/inflector_test.rb | 8 ++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/activesupport/lib/active_support/inflector.rb b/activesupport/lib/active_support/inflector.rb index 4921b99..2455d8c 100644 --- a/activesupport/lib/active_support/inflector.rb +++ b/activesupport/lib/active_support/inflector.rb @@ -284,11 +284,18 @@ module ActiveSupport # The iconv transliteration code doesn't function correctly # on some platforms, but it's very fast where it does function. - elsif "foo" != Inflector.transliterate("föö") - undef_method :transliterate - def transliterate(string) - string.mb_chars.normalize(:kd). # Decompose accented characters - gsub(/[^\x00-\x7F]+/, '') # Remove anything non-ASCII entirely (e.g. diacritics). + else + begin + if "foo" != Inflector.transliterate("föö") + undef_method :transliterate + def transliterate(string) + string.mb_chars.normalize(:kd). # Decompose accented characters + gsub(/[^\x00-\x7F]+/, '') # Remove anything non-ASCII entirely (e.g. diacritics). + end + end + rescue Iconv::InvalidEncoding + define_method(:transliterate) { nil } + retry end end diff --git a/activesupport/test/inflector_test.rb b/activesupport/test/inflector_test.rb index d8c93dc..3311ef7 100644 --- a/activesupport/test/inflector_test.rb +++ b/activesupport/test/inflector_test.rb @@ -104,6 +104,14 @@ class InflectorTest < Test::Unit::TestCase end end + def test_unsupported_iconv + Iconv.stubs(:iconv).raises(Iconv::InvalidEncoding.allocate) + assert_nothing_raised { load 'active_support/inflector.rb' } + StringToParameterizedAndNormalized.each do |some_string, parameterized_string| + assert_equal(parameterized_string, ActiveSupport::Inflector.parameterize(some_string)) + end + end + def test_parameterize_and_normalize StringToParameterizedAndNormalized.each do |some_string, parameterized_string| assert_equal(parameterized_string, ActiveSupport::Inflector.parameterize(some_string)) -- 1.6.0.5