diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2009-05-18 13:43:44 -0700 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2009-05-18 13:43:44 -0700 |
commit | 3e2f0800e33cc056d344a45557815b19463ab416 (patch) | |
tree | 617b18720b5ce1beda64c478845538756509cb2c /actionpack | |
parent | 87adecfef59577be17a9731245cb201ecb1b477f (diff) | |
parent | cef76c8af4705dc60f85a721e3a14adb99418d33 (diff) | |
download | rails-3e2f0800e33cc056d344a45557815b19463ab416.tar.gz rails-3e2f0800e33cc056d344a45557815b19463ab416.tar.bz2 rails-3e2f0800e33cc056d344a45557815b19463ab416.zip |
Merge branch 'master' of git@github.com:rails/rails
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_view/helpers/text_helper.rb | 7 | ||||
-rw-r--r-- | actionpack/test/template/text_helper_test.rb | 3 |
2 files changed, 9 insertions, 1 deletions
diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index 8136a1cb13..ad0733a7e1 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -34,12 +34,16 @@ module ActionView # Truncates a given +text+ after a given <tt>:length</tt> if +text+ is longer than <tt>:length</tt> # (defaults to 30). The last characters will be replaced with the <tt>:omission</tt> (defaults to "..."). + # Pass a <tt>:separator</tt> to truncate +text+ at a natural break. # # ==== Examples # # truncate("Once upon a time in a world far far away") # # => Once upon a time in a world f... # + # truncate("Once upon a time in a world far far away", :separator => ' ') + # # => Once upon a time in a world... + # # truncate("Once upon a time in a world far far away", :length => 14) # # => Once upon a... # @@ -71,7 +75,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 + stop = options[:separator] ? (chars.rindex(options[:separator].mb_chars, l) || l) : l + (chars.length > options[:length] ? chars[0...stop] + 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 a780bfc606..706b5085f4 100644 --- a/actionpack/test/template/text_helper_test.rb +++ b/actionpack/test/template/text_helper_test.rb @@ -49,6 +49,9 @@ class TextHelperTest < ActionView::TestCase 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 "Hello[...]", truncate("Hello Big World!", :omission => "[...]", :length => 13, :separator => ' ') + assert_equal "Hello Big[...]", truncate("Hello Big World!", :omission => "[...]", :length => 14, :separator => ' ') + assert_equal "Hello Big[...]", truncate("Hello Big World!", :omission => "[...]", :length => 15, :separator => ' ') end if RUBY_VERSION < '1.9.0' |