Skip to content
This repository
Browse code

Cleanup of excerpt helper

* replaced String concatenation by joining
* separator has default value to '', even it is nil
  • Loading branch information...
commit 061e48df26557bb0a667794df9772880846058cb 1 parent b77781c
Paul Nikitochkin pftg authored
4 actionview/CHANGELOG.md
Source Rendered
... ... @@ -1,3 +1,7 @@
  1 +* Refactor `excerpt` to use `Array#join` instead of `String#+`.
  2 +
  3 + *Paul Nikitochkin*
  4 +
1 5 * Only cache template digests if `config.cache_template_loading` id true.
2 6
3 7 *Josh Lauer*, *Justin Ridgewell*
5 actionview/lib/action_view/helpers/text_helper.rb
@@ -150,7 +150,7 @@ def highlight(text, phrases, options = {})
150 150 def excerpt(text, phrase, options = {})
151 151 return unless text && phrase
152 152
153   - separator = options.fetch(:separator, "")
  153 + separator = options[:separator] || ''
154 154 phrase = Regexp.escape(phrase)
155 155 regex = /#{phrase}/i
156 156
@@ -171,7 +171,8 @@ def excerpt(text, phrase, options = {})
171 171 prefix, first_part = cut_excerpt_part(:first, first_part, separator, options)
172 172 postfix, second_part = cut_excerpt_part(:second, second_part, separator, options)
173 173
174   - prefix + (first_part + separator + phrase + separator + second_part).strip + postfix
  174 + affix = [first_part, separator, phrase, separator, second_part].join.strip
  175 + [prefix, affix, postfix].join
175 176 end
176 177
177 178 # Attempts to pluralize the +singular+ word unless +count+ is 1. If
3  actionview/test/template/text_helper_test.rb
@@ -314,6 +314,9 @@ def test_excerpt_with_separator
314 314
315 315 options = { :separator => "\n", :radius => 1 }
316 316 assert_equal("...very\nvery long\nstring", excerpt("my very\nvery\nvery long\nstring", 'long', options))
  317 +
  318 + assert_equal excerpt('This is a beautiful morning', 'a'),
  319 + excerpt('This is a beautiful morning', 'a', separator: nil)
317 320 end
318 321
319 322 def test_word_wrap

0 comments on commit 061e48d

Please sign in to comment.
Something went wrong with that request. Please try again.