Skip to content

Commit f307197

Browse files
authored
Merge pull request rails#39979 from tgxworld/speed_up_text_helper_excerpt
Improve perf of `ActionView::Helpers::TextHelper#excerpt` for large strings.
2 parents 7378ffb + a40d3de commit f307197

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

actionview/lib/action_view/helpers/text_helper.rb

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require "active_support/core_ext/string/filters"
4+
require "active_support/core_ext/string/access"
45
require "active_support/core_ext/array/extract_options"
56
require "action_view/helpers/sanitize_helper"
67
require "action_view/helpers/tag_helper"
@@ -470,18 +471,25 @@ def cut_excerpt_part(part_position, part, separator, options)
470471
radius = options.fetch(:radius, 100)
471472
omission = options.fetch(:omission, "...")
472473

473-
part = part.split(separator)
474-
part.delete("")
475-
affix = part.size > radius ? omission : ""
474+
if separator != ""
475+
part = part.split(separator)
476+
part.delete("")
477+
end
476478

477-
part = if part_position == :first
478-
drop_index = [part.length - radius, 0].max
479-
part.drop(drop_index)
480-
else
481-
part.first(radius)
479+
affix = part.length > radius ? omission : ""
480+
481+
part =
482+
if part_position == :first
483+
part.last(radius)
484+
else
485+
part.first(radius)
486+
end
487+
488+
if separator != ""
489+
part = part.join(separator)
482490
end
483491

484-
return affix, part.join(separator)
492+
return affix, part
485493
end
486494
end
487495
end

0 commit comments

Comments
 (0)