Permalink
Browse files

Merge pull request #11603 from jetthoughts/join_strings_instead_of_co…

…ntactination

Cleanup: replace String concatenation by joining for excerpt helper
  • Loading branch information...
2 parents 329721e + 061e48d commit 9c18beae641a954f818eeb8f349248341160debe @rafaelfranca rafaelfranca committed Sep 12, 2013
View
@@ -1,3 +1,7 @@
+* Refactor `excerpt` to use `Array#join` instead of `String#+`.
+
+ *Paul Nikitochkin*
+
* Only cache template digests if `config.cache_template_loading` id true.
*Josh Lauer*, *Justin Ridgewell*
@@ -150,7 +150,7 @@ def highlight(text, phrases, options = {})
def excerpt(text, phrase, options = {})
return unless text && phrase
- separator = options.fetch(:separator, "")
+ separator = options[:separator] || ''
phrase = Regexp.escape(phrase)
regex = /#{phrase}/i
@@ -171,7 +171,8 @@ def excerpt(text, phrase, options = {})
prefix, first_part = cut_excerpt_part(:first, first_part, separator, options)
postfix, second_part = cut_excerpt_part(:second, second_part, separator, options)
- prefix + (first_part + separator + phrase + separator + second_part).strip + postfix
+ affix = [first_part, separator, phrase, separator, second_part].join.strip
+ [prefix, affix, postfix].join
end
# Attempts to pluralize the +singular+ word unless +count+ is 1. If
@@ -314,6 +314,9 @@ def test_excerpt_with_separator
options = { :separator => "\n", :radius => 1 }
assert_equal("...very\nvery long\nstring", excerpt("my very\nvery\nvery long\nstring", 'long', options))
+
+ assert_equal excerpt('This is a beautiful morning', 'a'),
+ excerpt('This is a beautiful morning', 'a', separator: nil)
end
def test_word_wrap

0 comments on commit 9c18bea

Please sign in to comment.