diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2013-09-12 13:25:04 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2013-09-12 13:25:04 -0300 |
commit | 9c18beae641a954f818eeb8f349248341160debe (patch) | |
tree | 78f9341e1e3de5580bb3f0f65c504118131a1eff /actionview | |
parent | 329721e35194958ba86832e995775d02bacf8bc7 (diff) | |
parent | 061e48df26557bb0a667794df9772880846058cb (diff) | |
download | rails-9c18beae641a954f818eeb8f349248341160debe.tar.gz rails-9c18beae641a954f818eeb8f349248341160debe.tar.bz2 rails-9c18beae641a954f818eeb8f349248341160debe.zip |
Merge pull request #11603 from jetthoughts/join_strings_instead_of_contactination
Cleanup: replace String concatenation by joining for excerpt helper
Diffstat (limited to 'actionview')
-rw-r--r-- | actionview/CHANGELOG.md | 4 | ||||
-rw-r--r-- | actionview/lib/action_view/helpers/text_helper.rb | 5 | ||||
-rw-r--r-- | actionview/test/template/text_helper_test.rb | 3 |
3 files changed, 10 insertions, 2 deletions
diff --git a/actionview/CHANGELOG.md b/actionview/CHANGELOG.md index 6240dc6ac6..9f53195c58 100644 --- a/actionview/CHANGELOG.md +++ b/actionview/CHANGELOG.md @@ -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* diff --git a/actionview/lib/action_view/helpers/text_helper.rb b/actionview/lib/action_view/helpers/text_helper.rb index 3fc64fa8a5..c23d605c5f 100644 --- a/actionview/lib/action_view/helpers/text_helper.rb +++ b/actionview/lib/action_view/helpers/text_helper.rb @@ -150,7 +150,7 @@ module ActionView 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 @@ module ActionView 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 diff --git a/actionview/test/template/text_helper_test.rb b/actionview/test/template/text_helper_test.rb index 1b2234f4e2..c2999fcb85 100644 --- a/actionview/test/template/text_helper_test.rb +++ b/actionview/test/template/text_helper_test.rb @@ -314,6 +314,9 @@ class TextHelperTest < ActionView::TestCase 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 |