From 8aadb2dca5b359af95e068e4758b0b56fdfab8da Mon Sep 17 00:00:00 2001 From: Sebastian Nowak Date: Tue, 18 Nov 2008 21:52:39 +0100 Subject: [PATCH] Truncate text after last space before limit --- actionpack/lib/action_view/helpers/text_helper.rb | 12 +++++++----- actionpack/test/template/text_helper_test.rb | 8 ++++---- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index 510c1a6..d67d922 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -42,16 +42,17 @@ module ActionView output_buffer << string end - # Truncates a given +text+ after a given :length if +text+ is longer than :length - # (defaults to 30). The last characters will be replaced with the :omission (defaults to "..."). + # Truncates a given +text+ after last space before a given :length if +text+ is longer + # than :length (defaults to 30). The last characters will be replaced with the + # :omission (defaults to "..."). # # ==== Examples # # truncate("Once upon a time in a world far far away") - # # => Once upon a time in a world f... + # # => Once upon a time in a world ... # # truncate("Once upon a time in a world far far away", :length => 14) - # # => Once upon a... + # # => Once upon ... # # truncate("And they found that many people were sleeping better.", :length => 25, "(clipped)") # # => And they found that many (clipped) @@ -81,7 +82,8 @@ module ActionView if text l = options[:length] - options[:omission].mb_chars.length chars = text.mb_chars - (chars.length > options[:length] ? chars[0...l] + options[:omission] : text).to_s + last_space_position = (chars[0...l].reverse.index("\s") || 0) + (chars.length > options[:length] ? chars[0...l-last_space_position] + options[:omission] : text).to_s end end diff --git a/actionpack/test/template/text_helper_test.rb b/actionpack/test/template/text_helper_test.rb index 3e7a8f3..719130e 100644 --- a/actionpack/test/template/text_helper_test.rb +++ b/actionpack/test/template/text_helper_test.rb @@ -36,7 +36,7 @@ class TextHelperTest < ActionView::TestCase def test_truncate assert_equal "Hello World!", truncate("Hello World!", :length => 12) - assert_equal "Hello Wor...", truncate("Hello World!!", :length => 12) + assert_equal "Hello ...", truncate("Hello World!!", :length => 12) end def test_truncate_should_use_default_length_of_30 @@ -45,9 +45,9 @@ class TextHelperTest < ActionView::TestCase end def test_truncate_with_options_hash - assert_equal "This is a string that wil[...]", truncate("This is a string that will go longer then the default truncate length of 30", :omission => "[...]") - assert_equal "Hello W...", truncate("Hello World!", :length => 10) - assert_equal "Hello[...]", truncate("Hello World!", :omission => "[...]", :length => 10) + assert_equal "This is a string that [...]", truncate("This is a string that will go longer then the default truncate length of 30", :omission => "[...]") + assert_equal "Hello ...", truncate("Hello World!", :length => 10) + assert_equal "Hello [...]", truncate("Hello World!", :omission => "[...]", :length => 10) end if RUBY_VERSION < '1.9.0' -- 1.5.6.3