rails_sluggable 1.0.1 → 1.0.2
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 +4 -4
- data/lib/rails_sluggable/version.rb +1 -1
- data/lib/rails_sluggable.rb +25 -3
- metadata +5 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4e1f2b6d459c161e06e69f69bb7bd3ee34e0083b1711fa1a001c34179b5fe413
|
|
4
|
+
data.tar.gz: d7fdac082bcc086db409ce4c6c52f577719ca4a64e47c27da1c203dd1d582832
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f06376c3117c9563f9a8710a5f0ee8209c5d872227731311d811455f08439922f4934b0972b603dbd9fc6803e2e1b16cc1c850624bbab86ab7b4f8dc03ec8fd2
|
|
7
|
+
data.tar.gz: a70d2f8410cbcb5f6c262d7eb68f76a6e5e01d3d90fd1e2c9b0df1043ad5de3bf2485bb384067747a02e21afe88d1e3ea11479355637837f431c9e8988366a6e
|
data/lib/rails_sluggable.rb
CHANGED
|
@@ -20,9 +20,31 @@ module RailsSluggable
|
|
|
20
20
|
length = self.class.slug_options[:length] || 12
|
|
21
21
|
separator = self.class.slug_options[:separator] || ''
|
|
22
22
|
|
|
23
|
-
#
|
|
24
|
-
|
|
25
|
-
|
|
23
|
+
# 生成唯一的 slug
|
|
24
|
+
self.slug = generate_unique_slug(length, separator)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
private
|
|
28
|
+
|
|
29
|
+
def generate_unique_slug(length, separator)
|
|
30
|
+
max_attempts = 10
|
|
31
|
+
attempts = 0
|
|
32
|
+
|
|
33
|
+
begin
|
|
34
|
+
uuid = SecureRandom.uuid.gsub('-', separator)
|
|
35
|
+
candidate_slug = separator.present? ?
|
|
36
|
+
uuid.split(separator)[0..(length-1)].join(separator) :
|
|
37
|
+
uuid[0, length]
|
|
38
|
+
|
|
39
|
+
attempts += 1
|
|
40
|
+
|
|
41
|
+
# 检查数据库中是否已存在此 slug
|
|
42
|
+
end while self.class.exists?(slug: candidate_slug) && attempts < max_attempts
|
|
43
|
+
|
|
44
|
+
# 如果达到最大尝试次数仍未找到唯一 slug,抛出异常
|
|
45
|
+
raise "Unable to generate unique slug after #{max_attempts} attempts" if attempts >= max_attempts
|
|
46
|
+
|
|
47
|
+
candidate_slug
|
|
26
48
|
end
|
|
27
49
|
|
|
28
50
|
def save_with_slug
|
metadata
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rails_sluggable
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0.
|
|
4
|
+
version: 1.0.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- aric.zheng
|
|
8
|
+
autorequire:
|
|
8
9
|
bindir: exe
|
|
9
10
|
cert_chain: []
|
|
10
11
|
date: 2025-11-07 00:00:00.000000000 Z
|
|
@@ -46,6 +47,7 @@ metadata:
|
|
|
46
47
|
homepage_uri: https://github.com/afeiship/rails_sluggable
|
|
47
48
|
source_code_uri: https://github.com/afeiship/rails_sluggable
|
|
48
49
|
changelog_uri: https://github.com/afeiship/rails_sluggable/blob/main/CHANGELOG.md
|
|
50
|
+
post_install_message:
|
|
49
51
|
rdoc_options: []
|
|
50
52
|
require_paths:
|
|
51
53
|
- lib
|
|
@@ -60,7 +62,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
60
62
|
- !ruby/object:Gem::Version
|
|
61
63
|
version: '0'
|
|
62
64
|
requirements: []
|
|
63
|
-
rubygems_version: 3.
|
|
65
|
+
rubygems_version: 3.5.22
|
|
66
|
+
signing_key:
|
|
64
67
|
specification_version: 4
|
|
65
68
|
summary: A Rails plugin for generating configurable model slugs
|
|
66
69
|
test_files: []
|