aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/helpers
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2012-05-26 13:34:31 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2012-05-26 13:34:31 -0300
commit9c8132c7bb3224dd357df7246ac65b647a59806f (patch)
tree1d49f5dc7820e01a31da763fdcdbd6905de778e9 /actionpack/lib/action_view/helpers
parent2114e2187dd8cbdcc0ab304ec0adf1a1bf5cc09a (diff)
parent38813da6dc6611d6b68dd4d584012c19e86a218a (diff)
downloadrails-9c8132c7bb3224dd357df7246ac65b647a59806f.tar.gz
rails-9c8132c7bb3224dd357df7246ac65b647a59806f.tar.bz2
rails-9c8132c7bb3224dd357df7246ac65b647a59806f.zip
Merge pull request #6031 from lellisga/truncate_readme
Truncate now has the ability to receive a html option that allows it to call rails helpers.
Diffstat (limited to 'actionpack/lib/action_view/helpers')
-rw-r--r--actionpack/lib/action_view/helpers/text_helper.rb14
1 files changed, 12 insertions, 2 deletions
diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb
index 8cd7cf0052..515bd78101 100644
--- a/actionpack/lib/action_view/helpers/text_helper.rb
+++ b/actionpack/lib/action_view/helpers/text_helper.rb
@@ -80,8 +80,18 @@ module ActionView
#
# truncate("<p>Once upon a time in a world far far away</p>")
# # => "<p>Once upon a time in a wo..."
- def truncate(text, options = {})
- text.truncate(options.fetch(:length, 30), options) if text
+ #
+ # truncate("Once upon a time in a world far far away") { link_to "Continue", "#" }
+ # # => "Once upon a time in a wo...<a href="#">Continue</a>"
+ def truncate(text, options = {}, &block)
+ return unless text
+
+ options = { :length => 30 }.merge!(options)
+ length = options.delete(:length)
+
+ content = ERB::Util.html_escape(text.truncate(length, options))
+ content << capture(&block) if block_given? && text.length > length
+ content
end
# Highlights one or more +phrases+ everywhere in +text+ by inserting it into