diff options
author | Andy Stewart <boss@airbladesoftware.com> | 2009-01-28 16:45:28 +0000 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2009-05-18 20:46:19 +0200 |
commit | 5e190ef138a034bf86419ce4f4c343ae16bfc77b (patch) | |
tree | e8f8bd7d616807ad1a67038b67da2a2e404f2c61 /actionpack/lib | |
parent | 195fadbfd31294d43634afb7bbf4f0ffc86b470a (diff) | |
download | rails-5e190ef138a034bf86419ce4f4c343ae16bfc77b.tar.gz rails-5e190ef138a034bf86419ce4f4c343ae16bfc77b.tar.bz2 rails-5e190ef138a034bf86419ce4f4c343ae16bfc77b.zip |
Truncate helper accepts a :separator for a more legible result [#1807 state:resolved]
Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
Diffstat (limited to 'actionpack/lib')
-rw-r--r-- | actionpack/lib/action_view/helpers/text_helper.rb | 7 |
1 files changed, 6 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 |