rails_sluggable 1.0.1
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 +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +52 -0
- data/Rakefile +3 -0
- data/lib/rails_sluggable/engine.rb +9 -0
- data/lib/rails_sluggable/railtie.rb +4 -0
- data/lib/rails_sluggable/version.rb +3 -0
- data/lib/rails_sluggable.rb +38 -0
- data/lib/tasks/rails_sluggable_tasks.rake +4 -0
- metadata +66 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 8a3a7c668ab162a3dc8dce5271ec5d8b9cff0c2c27de73ad4d2cd06a553cf057
|
|
4
|
+
data.tar.gz: 972474ef9ee2f89d0f499663f4c178c90b0dd28d74a3832c531b74d340a9527e
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 8522d4792d1d976ae9a3bdeb041cf72f72a1312ee9d534f773c2a090697e00fa6672305243132d88ed0a50ea034d19831e420569c566f913948b2abc19967203
|
|
7
|
+
data.tar.gz: 397d1fb2a0edff86b87eeccb250d8d9fa5dee992d19b499ac75fcda323fea5c1d037105501eed4d9979999c0d1186d53a0228fe00c88c0713010ecce2ec8fe6b
|
data/MIT-LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright aric.zheng
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# RailsSluggable
|
|
2
|
+
> A Rails plugin for generating configurable model slugs.
|
|
3
|
+
|
|
4
|
+
## Usage
|
|
5
|
+
```rb
|
|
6
|
+
# generate slug for `Posts`
|
|
7
|
+
rails generate migration AddSlugToPosts slug:string:uniq
|
|
8
|
+
|
|
9
|
+
class Post < ApplicationRecord
|
|
10
|
+
sluggable length: 12
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
class Article < ApplicationRecord
|
|
14
|
+
sluggable length: 8, separator: '-'
|
|
15
|
+
end
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Example
|
|
19
|
+
> Use slug for post model.
|
|
20
|
+
```json
|
|
21
|
+
{
|
|
22
|
+
"id": 1,
|
|
23
|
+
"content": "Ruby on Rails 是一个强大的 Web 开发框架...",
|
|
24
|
+
"created_at": "2025-11-07T06:48:29.259Z",
|
|
25
|
+
"slug": "077f802de102",
|
|
26
|
+
"title": "Rails 入门指南",
|
|
27
|
+
"updated_at": "2025-11-07T06:48:29.259Z"
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Installation
|
|
32
|
+
Add this line to your application's Gemfile:
|
|
33
|
+
|
|
34
|
+
```ruby
|
|
35
|
+
gem "rails_sluggable"
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
And then execute:
|
|
39
|
+
```bash
|
|
40
|
+
$ bundle
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Or install it yourself as:
|
|
44
|
+
```bash
|
|
45
|
+
$ gem install rails_sluggable
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Contributing
|
|
49
|
+
Contribution directions go here.
|
|
50
|
+
|
|
51
|
+
## License
|
|
52
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
require 'rails_sluggable/engine'
|
|
2
|
+
require 'active_support/concern'
|
|
3
|
+
|
|
4
|
+
module RailsSluggable
|
|
5
|
+
extend ActiveSupport::Concern
|
|
6
|
+
|
|
7
|
+
class_methods do
|
|
8
|
+
def sluggable(options = {})
|
|
9
|
+
include InstanceMethods
|
|
10
|
+
|
|
11
|
+
# Store configuration per model
|
|
12
|
+
self.slug_options = options
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
module InstanceMethods
|
|
17
|
+
def generate_slug_if_blank
|
|
18
|
+
return if slug.present?
|
|
19
|
+
|
|
20
|
+
length = self.class.slug_options[:length] || 12
|
|
21
|
+
separator = self.class.slug_options[:separator] || ''
|
|
22
|
+
|
|
23
|
+
# Generate UUID, remove separators, take specified length
|
|
24
|
+
uuid = SecureRandom.uuid.gsub('-', separator)
|
|
25
|
+
self.slug = separator.present? ? uuid.split(separator)[0..(length-1)].join(separator) : uuid[0, length]
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def save_with_slug
|
|
29
|
+
generate_slug_if_blank
|
|
30
|
+
save
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
included do
|
|
35
|
+
before_save :generate_slug_if_blank
|
|
36
|
+
class_attribute :slug_options
|
|
37
|
+
end
|
|
38
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: rails_sluggable
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- aric.zheng
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 2025-11-07 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: rails
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: 6.0.0
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: 6.0.0
|
|
26
|
+
description: A Rails plugin to generate configurable slugs for models, replacing ID-based
|
|
27
|
+
URLs with user-friendly identifiers.
|
|
28
|
+
email:
|
|
29
|
+
- 1290657123@qq.com
|
|
30
|
+
executables: []
|
|
31
|
+
extensions: []
|
|
32
|
+
extra_rdoc_files: []
|
|
33
|
+
files:
|
|
34
|
+
- MIT-LICENSE
|
|
35
|
+
- README.md
|
|
36
|
+
- Rakefile
|
|
37
|
+
- lib/rails_sluggable.rb
|
|
38
|
+
- lib/rails_sluggable/engine.rb
|
|
39
|
+
- lib/rails_sluggable/railtie.rb
|
|
40
|
+
- lib/rails_sluggable/version.rb
|
|
41
|
+
- lib/tasks/rails_sluggable_tasks.rake
|
|
42
|
+
homepage: https://github.com/afeiship/rails_sluggable
|
|
43
|
+
licenses:
|
|
44
|
+
- MIT
|
|
45
|
+
metadata:
|
|
46
|
+
homepage_uri: https://github.com/afeiship/rails_sluggable
|
|
47
|
+
source_code_uri: https://github.com/afeiship/rails_sluggable
|
|
48
|
+
changelog_uri: https://github.com/afeiship/rails_sluggable/blob/main/CHANGELOG.md
|
|
49
|
+
rdoc_options: []
|
|
50
|
+
require_paths:
|
|
51
|
+
- lib
|
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
53
|
+
requirements:
|
|
54
|
+
- - ">="
|
|
55
|
+
- !ruby/object:Gem::Version
|
|
56
|
+
version: 2.6.0
|
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - ">="
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '0'
|
|
62
|
+
requirements: []
|
|
63
|
+
rubygems_version: 3.6.3
|
|
64
|
+
specification_version: 4
|
|
65
|
+
summary: A Rails plugin for generating configurable model slugs
|
|
66
|
+
test_files: []
|