From 29c18fcda68f9cbcad8c0e96cbdba8c3491be4fa Mon Sep 17 00:00:00 2001 From: Philip Hallstrom Date: Tue, 1 Feb 2011 10:58:34 -0800 Subject: [PATCH] truncate will now remove any spaces between the truncated string and the omission string. This prevents results like 'Hello ...' --- .../lib/active_support/core_ext/string/filters.rb | 2 +- activesupport/test/core_ext/string_ext_test.rb | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/string/filters.rb b/activesupport/lib/active_support/core_ext/string/filters.rb index e15a1df..92519c5 100644 --- a/activesupport/lib/active_support/core_ext/string/filters.rb +++ b/activesupport/lib/active_support/core_ext/string/filters.rb @@ -44,6 +44,6 @@ class String stop = options[:separator] ? (chars.rindex(options[:separator].mb_chars, length_with_room_for_omission) || length_with_room_for_omission) : length_with_room_for_omission - (chars.length > length ? chars[0...stop] + options[:omission] : text).to_s + (chars.length > length ? chars[0...stop].rstrip + options[:omission] : text).to_s end end diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index bb865ca..df124e9 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -254,6 +254,7 @@ class StringInflectionsTest < Test::Unit::TestCase def test_truncate assert_equal "Hello World!", "Hello World!".truncate(12) assert_equal "Hello Wor...", "Hello World!!".truncate(12) + assert_equal "Hello...", "Hello World!!".truncate(9) end def test_truncate_with_omission_and_seperator @@ -269,13 +270,13 @@ class StringInflectionsTest < Test::Unit::TestCase assert_equal "\354\225\210\353\205\225\355...", "\354\225\210\353\205\225\355\225\230\354\204\270\354\232\224".truncate(10) end with_kcode 'u' do - assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 ...", + assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254...", "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 \354\225\204\353\235\274\353\246\254\354\230\244".truncate(10) end end else def test_truncate_multibyte - assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 ...".force_encoding('UTF-8'), + assert_equal "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254...".force_encoding('UTF-8'), "\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 \354\225\204\353\235\274\353\246\254\354\230\244".force_encoding('UTF-8').truncate(10) end end -- 1.7.0.6