sluggi 1.2.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +47 -14
- data/lib/sluggi/history.rb +4 -5
- data/lib/sluggi/model.rb +10 -2
- data/lib/sluggi/version.rb +1 -1
- metadata +27 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 183a80354c5c2961a06f2bfb30c00d2ba041475c66903307b6b3ceb365796cbe
|
4
|
+
data.tar.gz: 215ba9fbe8b604f934c71e5c8114d31e03f143d97dcee1a34424c13151ced084
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 157a8f7ff7f2cba47c84741db578733a5ca1a61474f8a1e9d50b4a42ed9ee2a718fb15b0dcab90a53887bca51fcfe567b599d40a9371d703bab565217f754782
|
7
|
+
data.tar.gz: 6d673e52d9bd68df5d4413e8eb64bf7d8c90db80cec5fe011802ff209efee0b685a56710d25996a48e2b6698955f89397421f69f0da5d0bfe1ed1a4f7e80ffe8
|
data/README.md
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
# Sluggi
|
2
2
|
|
3
|
-
[![Gem Version](
|
4
|
-
[![Build Status](
|
3
|
+
[![Gem Version](https://img.shields.io/gem/v/sluggi.svg)](https://rubygems.org/gems/sluggi)
|
4
|
+
[![Build Status](https://github.com/neighborland/sluggi/actions/workflows/ruby.yml/badge.svg)](https://github.com/neighborland/sluggi/actions/workflows/ruby.yml)
|
5
5
|
|
6
|
-
Sluggi is a simple [friendly_id](https://github.com/norman/friendly_id)-inspired slugging library for ActiveRecord models.
|
6
|
+
Sluggi is a simple [friendly_id](https://github.com/norman/friendly_id)-inspired slugging library for ActiveRecord models. It is faster than `friendly_id` (see below for benchmarks).
|
7
7
|
|
8
8
|
It provides basic slugs, slug history, and the ability to define multiple slug candidates.
|
9
9
|
|
@@ -129,20 +129,23 @@ Cat.find_by_slug('tuxedo-stan')
|
|
129
129
|
|
130
130
|
### Model with Slug Candidates
|
131
131
|
|
132
|
-
Override `#slug_candidates` to define cascading candidate values for slugs.
|
133
|
-
uniqueness conflicts.
|
132
|
+
Override `#slug_candidates` to define cascading candidate values for slugs.
|
133
|
+
This is useful to avoid uniqueness conflicts. Do not override `#slug_value` -
|
134
|
+
the default implementation in `Model` will call `#slug_candidates` and
|
135
|
+
works with or without `History`.
|
134
136
|
|
135
137
|
```ruby
|
136
138
|
class Cat < ActiveRecord::Base
|
137
139
|
include Sluggi::Slugged
|
138
140
|
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
# the first unused value in the list is used
|
141
|
+
# The first unused value in the list is used.
|
142
|
+
# Each item may be a value or a lambda.
|
143
|
+
# Use a lambda to defer expensive unique value calculations.
|
144
144
|
def slug_candidates
|
145
|
-
[
|
145
|
+
[
|
146
|
+
name,
|
147
|
+
-> { "#{name}-#{Cat.count}" }
|
148
|
+
]
|
146
149
|
end
|
147
150
|
|
148
151
|
def slug_value_changed?
|
@@ -158,11 +161,41 @@ cat.slug
|
|
158
161
|
|
159
162
|
cat_2 = Cat.create(name: 'Tuxedo Stan')
|
160
163
|
cat_2.slug
|
161
|
-
=> 'tuxedo-stan-
|
162
|
-
|
163
|
-
|
164
|
+
=> 'tuxedo-stan-456'
|
165
|
+
```
|
166
|
+
|
167
|
+
## Performance
|
168
|
+
|
169
|
+
Run the benchmark script: `ruby bench.rb`. This script is based on the
|
170
|
+
benchmark script from `friendly_id`.
|
171
|
+
|
172
|
+
Here are some anecdotal results using ruby 2.5.3:
|
164
173
|
|
165
174
|
```
|
175
|
+
SLUGGI FRIENDLY_ID
|
176
|
+
1) find (id) - direct ActiveRecord 0.092318 0.093049
|
177
|
+
2) find (in-table slug) 0.102773 0.259542
|
178
|
+
3) find (in-table slug; using finders module) 0.098183 0.108248
|
179
|
+
4) find (external slug) 0.670229 0.832791
|
180
|
+
5) insert (plain AR / no slug) 0.345077 0.345105
|
181
|
+
6) insert (in-table-slug) 0.666451 0.815505
|
182
|
+
7) insert (in-table-slug; using finders module) 0.668737 0.744433
|
183
|
+
8) insert (external slug) 2.480790 2.849761
|
184
|
+
```
|
185
|
+
|
186
|
+
Notes:
|
187
|
+
|
188
|
+
Sluggi is at least 10% faster in every benchmark.
|
189
|
+
|
190
|
+
1) Baseline (does not use either gem)
|
191
|
+
2) 0.44x
|
192
|
+
3) 0.90x
|
193
|
+
4) 0.80x
|
194
|
+
5) Baseline (does not use either gem)
|
195
|
+
6) 0.82x
|
196
|
+
7) 0.90x
|
197
|
+
8) 0.87x
|
198
|
+
|
166
199
|
|
167
200
|
## Alternatives
|
168
201
|
|
data/lib/sluggi/history.rb
CHANGED
@@ -39,11 +39,10 @@ module Sluggi
|
|
39
39
|
end
|
40
40
|
|
41
41
|
def create_slug
|
42
|
-
|
43
|
-
return if
|
44
|
-
|
45
|
-
|
46
|
-
slugs.create(slug: value)
|
42
|
+
return if slug.blank?
|
43
|
+
return if slugs.first&.slug == slug
|
44
|
+
self.class.find_slugs(slug).delete_all # revert to previous slug & put first
|
45
|
+
slugs.create(slug: slug)
|
47
46
|
end
|
48
47
|
end
|
49
48
|
end
|
data/lib/sluggi/model.rb
CHANGED
@@ -11,6 +11,13 @@ module Sluggi
|
|
11
11
|
before_validation :set_slug
|
12
12
|
end
|
13
13
|
|
14
|
+
module ClassMethods
|
15
|
+
# Define this so that History can override it.
|
16
|
+
def slug_exists?(name)
|
17
|
+
exists?(slug: name)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
14
21
|
def to_param
|
15
22
|
errors.any? ? slug_was : slug
|
16
23
|
end
|
@@ -31,11 +38,12 @@ module Sluggi
|
|
31
38
|
# these are generally good to override:
|
32
39
|
|
33
40
|
def slug_value
|
34
|
-
slug_candidates.each do |
|
41
|
+
slug_candidates.each do |item|
|
42
|
+
value = item.respond_to?(:call) ? item.call : item
|
35
43
|
next if value.blank?
|
36
44
|
candidate = clean_slug(value)
|
37
45
|
return candidate if candidate == slug
|
38
|
-
return candidate unless self.class.
|
46
|
+
return candidate unless self.class.slug_exists?(candidate)
|
39
47
|
end
|
40
48
|
nil
|
41
49
|
end
|
data/lib/sluggi/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sluggi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tee Parham
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-11-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -16,28 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '6.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '6.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: railties
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '6.0'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '6.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: appraisal
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -52,20 +52,34 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '2.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.22'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.22'
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: rake
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
58
72
|
requirements:
|
59
73
|
- - "~>"
|
60
74
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
75
|
+
version: '13.0'
|
62
76
|
type: :development
|
63
77
|
prerelease: false
|
64
78
|
version_requirements: !ruby/object:Gem::Requirement
|
65
79
|
requirements:
|
66
80
|
- - "~>"
|
67
81
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
82
|
+
version: '13.0'
|
69
83
|
- !ruby/object:Gem::Dependency
|
70
84
|
name: sqlite3
|
71
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -104,7 +118,7 @@ homepage: https://github.com/neighborland/sluggi
|
|
104
118
|
licenses:
|
105
119
|
- MIT
|
106
120
|
metadata: {}
|
107
|
-
post_install_message:
|
121
|
+
post_install_message:
|
108
122
|
rdoc_options: []
|
109
123
|
require_paths:
|
110
124
|
- lib
|
@@ -112,16 +126,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
112
126
|
requirements:
|
113
127
|
- - ">="
|
114
128
|
- !ruby/object:Gem::Version
|
115
|
-
version:
|
129
|
+
version: 3.0.0
|
116
130
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
131
|
requirements:
|
118
132
|
- - ">="
|
119
133
|
- !ruby/object:Gem::Version
|
120
134
|
version: '0'
|
121
135
|
requirements: []
|
122
|
-
|
123
|
-
|
124
|
-
signing_key:
|
136
|
+
rubygems_version: 3.5.23
|
137
|
+
signing_key:
|
125
138
|
specification_version: 4
|
126
139
|
summary: Rails Slug Generator
|
127
140
|
test_files: []
|