From 38813da6dc6611d6b68dd4d584012c19e86a218a Mon Sep 17 00:00:00 2001 From: Li Ellis Gallardo Date: Sat, 28 Apr 2012 01:48:28 -0500 Subject: Truncate now has the ability to receive a html option that allows it to call rails helpers. This way if my text is long I don't have to do something like this: .text = truncate(@text, :length => 27) if @text.size >= 27 = link_to "continue", notes_path, ....."") --- actionpack/lib/action_view/helpers/text_helper.rb | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'actionpack/lib/action_view/helpers') 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("

Once upon a time in a world far far away

") # # => "

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...Continue" + 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 -- cgit v1.2.3