aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/helpers/text_helper.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2010-06-01 16:38:42 -0500
committerDavid Heinemeier Hansson <david@loudthinking.com>2010-06-01 16:38:42 -0500
commitd57397c4b62b6474ff8eb55bfd763f5e6dcdcd40 (patch)
treeeb9f14a745aede86baa2b4b4a6259b7bd5234a7a /actionpack/lib/action_view/helpers/text_helper.rb
parentea037ff55791a33d24773efd380b734f733c2815 (diff)
downloadrails-d57397c4b62b6474ff8eb55bfd763f5e6dcdcd40.tar.gz
rails-d57397c4b62b6474ff8eb55bfd763f5e6dcdcd40.tar.bz2
rails-d57397c4b62b6474ff8eb55bfd763f5e6dcdcd40.zip
Extracted String#truncate from TextHelper#truncate [DHH]
Diffstat (limited to 'actionpack/lib/action_view/helpers/text_helper.rb')
-rw-r--r--actionpack/lib/action_view/helpers/text_helper.rb16
1 files changed, 5 insertions, 11 deletions
diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb
index 41423d4e2e..860c1de6af 100644
--- a/actionpack/lib/action_view/helpers/text_helper.rb
+++ b/actionpack/lib/action_view/helpers/text_helper.rb
@@ -1,4 +1,5 @@
require 'active_support/core_ext/object/blank'
+require 'active_support/core_ext/string/filters'
require 'action_view/helpers/tag_helper'
module ActionView
@@ -42,7 +43,7 @@ module ActionView
# ==== Examples
#
# truncate("Once upon a time in a world far far away")
- # # => Once upon a time in a world...
+ # # => Once upon a time in a worl...
#
# truncate("Once upon a time in a world far far away", :separator => ' ')
# # => Once upon a time in a world...
@@ -50,9 +51,6 @@ module ActionView
# truncate("Once upon a time in a world far far away", :length => 14)
# # => Once upon a...
#
- # truncate("And they found that many people were sleeping better.", :length => 25, "(clipped)")
- # # => And they found t(clipped)
- #
# truncate("And they found that many people were sleeping better.", :omission => "... (continued)", :length => 25)
# # => And they f... (continued)
#
@@ -73,14 +71,10 @@ module ActionView
options[:length] = args[0] || 30
options[:omission] = args[1] || "..."
end
- options.reverse_merge!(:length => 30, :omission => "...")
- if text
- l = options[:length] - options[:omission].mb_chars.length
- chars = text.mb_chars
- 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
+ options.reverse_merge!(:length => 30)
+
+ text.truncate(options.delete(:length), options) if text
end
# Highlights one or more +phrases+ everywhere in +text+ by inserting it into