rails-truncate-words-fix 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3c547c35c7e482aeea4823ea67cb112c7c9ff956349425229b45a76f150fd627
4
+ data.tar.gz: 8f3442c6455e806bbfe62b13bf091b25abf55c00fde33bd76768070089e38d34
5
+ SHA512:
6
+ metadata.gz: 2431dd405d60bd4084e5bd816f161bc92218b7a970bc01b3fb8eb8e4dcc828f4e96d356065106a4d1110c577a28e5880936deb29cf3e787c36d5b19550a34583
7
+ data.tar.gz: 18be7598f65c4a3ec3453c2976a58eaf6cc7480d03ca6cb4dd89f4c0f3dd3e4bdbfeec0e35bed18f44a855109f71934d276aac4ef863c595d7c5473cee5e879b
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Robert Shecter
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # rails-truncate-words-fix
2
+
3
+ Speeds up Active Support's `String#truncate_words` and avoids transient memory use when truncating long strings.
4
+
5
+ This gem packages the **one-line regex optimization** in [rails/rails#59848](https://github.com/rails/rails/pull/59848). The original Rails regex consumes the entire suffix after the truncation point. This replacement uses a positive lookahead, so it stops matching at the requested boundary without changing the public API.
6
+
7
+
8
+ ## Additional information
9
+
10
+ **Speed (`benchmark-ips`, Ruby 4.0.9)**
11
+
12
+ | Scenario | current | lookahead | Improvement |
13
+ | ----------------------------------------- | ---------- | ---------- | ----------- |
14
+ | Short string, no truncation | 299k i/s | 296k i/s | ≈ same |
15
+ | 100k char string, truncate to 10 words | 2.8k i/s | 295k i/s | **106×** |
16
+ | 100k char string, truncate to 100 words | 2.7k i/s | 182k i/s | **67×** |
17
+ | Medium string, no truncation needed | 6.6k i/s | 6.7k i/s | ≈ same |
18
+
19
+ Source and full output: https://gist.github.com/dogweather/1d17cc89fa8f69ff9e0f6fa90f364cb7
20
+
21
+
22
+ **Peak / delta RSS (Ruby 4.0.9)**
23
+
24
+ | Input size | current delta | lookahead delta |
25
+ | ------------------------------- | ------------- | --------------- |
26
+ | 100K chars (request 500 words) | **+4.8 MB** | +0.1 MB |
27
+ | 500K chars (request 200 words) | **+23.5 MB** | **0 KB** |
28
+
29
+ The original pattern forces the engine to consume the remainder of the string (`.*`), which produces both the large slowdown and the transient RSS spike. The lookahead version performs the same logical check without materializing that match.
30
+
31
+ Source and full output: https://gist.github.com/dogweather/40f182ef2ec7dd96e3fb08d97dac4df7
32
+
33
+
34
+ ## Installation
35
+
36
+ Add this to your Rails application's `Gemfile`:
37
+
38
+ ```ruby
39
+ gem "rails-truncate-words-fix"
40
+ ```
41
+
42
+ The gem requires `railties` 7.2 through 8.x. Its Railtie loads Active Support's `String#truncate_words` definition before prepending the optimized implementation, during Rails initialization.
43
+
44
+ ## Example
45
+
46
+ ```ruby
47
+ "one two three four".truncate_words(2) # => "one two..."
48
+ ```
49
+
50
+ ## Tests
51
+
52
+ ```sh
53
+ ruby -Itest test/truncate_words_test.rb
54
+ ```
55
+
56
+ ## Why?
57
+
58
+ The underlying fix replaces `#{sep}.*` with `(?=#{sep})` at the end of Rails' regular expression. Benchmarks and memory measurements are in the [upstream PR](https://github.com/rails/rails/pull/59848).
59
+
60
+ Once Rails merges and ships the fix, remove this gem from the Gemfile.
61
+
62
+ MIT licensed.
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsTruncateWordsFix
4
+ module Patch
5
+ def truncate_words(words_count, options = {})
6
+ sep = options[:separator] || /\s+/
7
+ sep = Regexp.escape(sep.to_s) unless Regexp === sep
8
+
9
+ if self =~ /\A((?>.+?#{sep}){#{words_count - 1}}.+?)(?=#{sep})/m
10
+ $1 + (options[:omission] || "...")
11
+ else
12
+ dup
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ String.prepend(RailsTruncateWordsFix::Patch) unless String.ancestors.include?(RailsTruncateWordsFix::Patch)
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsTruncateWordsFix
4
+ class Railtie < Rails::Railtie
5
+ initializer "rails_truncate_words_fix.patch" do
6
+ require "active_support/core_ext/string/filters"
7
+ require "rails_truncate_words_fix/patch"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/railtie"
4
+ require_relative "rails_truncate_words_fix/railtie"
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-truncate-words-fix
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Robert Shecter
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: railties
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ -
17
+ - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: "7.2"
20
+ -
21
+ - "<"
22
+ - !ruby/object:Gem::Version
23
+ version: "9"
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: !ruby/object:Gem::Requirement
27
+ requirements:
28
+ -
29
+ - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: "7.2"
32
+ -
33
+ - "<"
34
+ - !ruby/object:Gem::Version
35
+ version: "9"
36
+ description: "A Rails Railtie applying the optimization from rails/rails#59848."
37
+ executables: []
38
+ extensions: []
39
+ extra_rdoc_files: []
40
+ files:
41
+ - LICENSE
42
+ - README.md
43
+ - lib/rails_truncate_words_fix.rb
44
+ - lib/rails_truncate_words_fix/patch.rb
45
+ - lib/rails_truncate_words_fix/railtie.rb
46
+ homepage: "https://github.com/public-law/rails-truncate-words-fix"
47
+ licenses:
48
+ - MIT
49
+ metadata:
50
+ source_uri: "https://github.com/public-law/rails-truncate-words-fix"
51
+ bug_tracker_uri: "https://github.com/public-law/rails-truncate-words-fix/issues"
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ -
58
+ - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "3.1"
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ -
64
+ - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ requirements: []
68
+ rubygems_version: 4.1.0
69
+ specification_version: 4
70
+ summary: "Avoid unnecessary suffix matching in Active Support's String#truncate_words"
71
+ test_files: []