aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2010-10-18 10:28:56 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2010-10-18 10:28:56 -0700
commitca59ec354ee8ba264120783c9364927a97b90896 (patch)
tree791c25dfad2627d24ac96dc8c13c2cc7bbec1d6d /actionpack/lib/action_view
parentdda81cb177078cce44bf2a81a70b0baa5c9d9ad7 (diff)
downloadrails-ca59ec354ee8ba264120783c9364927a97b90896.tar.gz
rails-ca59ec354ee8ba264120783c9364927a97b90896.tar.bz2
rails-ca59ec354ee8ba264120783c9364927a97b90896.zip
skip extra work if no text or phrase was provided
Diffstat (limited to 'actionpack/lib/action_view')
-rw-r--r--actionpack/lib/action_view/helpers/text_helper.rb23
1 files changed, 11 insertions, 12 deletions
diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb
index 94348cf9fa..1463c94aa1 100644
--- a/actionpack/lib/action_view/helpers/text_helper.rb
+++ b/actionpack/lib/action_view/helpers/text_helper.rb
@@ -134,6 +134,8 @@ module ActionView
# excerpt('This is an example', 'an', 5) # => ...s is an exam...
# excerpt('This is also an example', 'an', 8, '<chop> ') # => <chop> is also an example
def excerpt(text, phrase, *args)
+ return unless text && phrase
+
options = args.extract_options!
unless args.empty?
options[:radius] = args[0] || 100
@@ -141,20 +143,17 @@ module ActionView
end
options.reverse_merge!(:radius => 100, :omission => "...")
- if text && phrase
- phrase = Regexp.escape(phrase)
-
- if found_pos = text.mb_chars =~ /(#{phrase})/i
- start_pos = [ found_pos - options[:radius], 0 ].max
- end_pos = [ [ found_pos + phrase.mb_chars.length + options[:radius] - 1, 0].max, text.mb_chars.length ].min
+ phrase = Regexp.escape(phrase)
+ if found_pos = text.mb_chars =~ /(#{phrase})/i
+ start_pos = [ found_pos - options[:radius], 0 ].max
+ end_pos = [ [ found_pos + phrase.mb_chars.length + options[:radius] - 1, 0].max, text.mb_chars.length ].min
- prefix = start_pos > 0 ? options[:omission] : ""
- postfix = end_pos < text.mb_chars.length - 1 ? options[:omission] : ""
+ prefix = start_pos > 0 ? options[:omission] : ""
+ postfix = end_pos < text.mb_chars.length - 1 ? options[:omission] : ""
- prefix + text.mb_chars[start_pos..end_pos].strip + postfix
- else
- nil
- end
+ prefix + text.mb_chars[start_pos..end_pos].strip + postfix
+ else
+ nil
end
end