Skip to content

Commit 155065e

Browse files
Merge pull request rails#55476 from jonathanhefner/excerpt-with-non-whitespace-separator
Fix excerpt helper with non-whitespace separator
2 parents 0586754 + 0b98ec0 commit 155065e

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

actionview/lib/action_view/helpers/text_helper.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,14 @@ def excerpt(text, phrase, options = {})
260260
prefix, first_part = cut_excerpt_part(:first, first_part, separator, options)
261261
postfix, second_part = cut_excerpt_part(:second, second_part, separator, options)
262262

263-
affix = [first_part, separator, phrase, separator, second_part].join.strip
263+
affix = [
264+
first_part,
265+
!first_part.empty? ? separator : "",
266+
phrase,
267+
!second_part.empty? ? separator : "",
268+
second_part
269+
].join.strip
270+
264271
[prefix, affix, postfix].join
265272
end
266273

actionview/test/template/text_helper_test.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,9 @@ def test_excerpt_with_separator
359359
options = { separator: "\n", radius: 1 }
360360
assert_equal("...very\nvery long\nstring", excerpt("my very\nvery\nvery long\nstring", "long", options))
361361

362+
options = { separator: "_" }
363+
assert_equal("foo", excerpt("foo", "foo", options))
364+
362365
assert_equal excerpt("This is a beautiful morning", "a"),
363366
excerpt("This is a beautiful morning", "a", separator: nil)
364367
end

0 commit comments

Comments
 (0)