aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/helpers/text_helper.rb
diff options
context:
space:
mode:
authorEmilio Tagua <miloops@gmail.com>2009-05-18 20:01:06 -0300
committerEmilio Tagua <miloops@gmail.com>2009-05-18 20:01:06 -0300
commit1cc44599397e061901cd59233397129625839a60 (patch)
tree235108ce6a9d270bdfe3264a8f5f5a7c99c3ae5c /actionpack/lib/action_view/helpers/text_helper.rb
parent3db44e938fc6b5a5d272c4df4cf06dd6b5715782 (diff)
parentee5520a0a5ea83843ce88f6b9550e3c36b8cdd49 (diff)
downloadrails-1cc44599397e061901cd59233397129625839a60.tar.gz
rails-1cc44599397e061901cd59233397129625839a60.tar.bz2
rails-1cc44599397e061901cd59233397129625839a60.zip
Merge commit 'rails/master'
Conflicts: activerecord/lib/active_record.rb Updated: Arel submodule
Diffstat (limited to 'actionpack/lib/action_view/helpers/text_helper.rb')
-rw-r--r--actionpack/lib/action_view/helpers/text_helper.rb7
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