minislug 0.0.3
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/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +60 -0
- data/Rakefile +1 -0
- data/lib/minislug/version.rb +3 -0
- data/lib/minislug.rb +45 -0
- data/minislug.gemspec +22 -0
- metadata +51 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f9dd973331cf76363e6cb94a04d80f58036b06045619c28ebccb4997e6e3e554
|
4
|
+
data.tar.gz: 1043a0f409aac1df9b0a9f103fc90ff397905102f0dc73625663a599dc7c4881
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 590423c43d141bfbafa8eb3ce2f13e830cd5c62aa85fc698592b2cefabb9bf0cd3cd19d88525f36d3a994c68451e3317e45ea0b28e6c88c7110c9706b29a0a62
|
7
|
+
data.tar.gz: fb497ce04086498a6672c46e3bf66bec74bb751b9b4215d84901ed641b52ef37828edf7b715ac1f3c46965264739b7729c58c75021245e5b4aa203e71a460562
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 conanite
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# Minislug
|
2
|
+
|
3
|
+
Please use <a href='https://github.com/bkoski/slug'>bkoski/slug</a> unless you're looking for something very bare-bones.
|
4
|
+
Minislug, like other slug implementations, generates a url-friendly id for your model object by converting or stripping
|
5
|
+
undesirable characters from a source string that you specify.
|
6
|
+
|
7
|
+
Minislug abhors uniqueness checks because it is quite possible that you are slugging objects whose uniqueness needs are context-dependent,
|
8
|
+
for example, in a multi-tenant application, you don't want globally-unique slugs. Or another example, if you manage
|
9
|
+
family data, you might expect (in a typical euro-usa-latin culture), all children in a given family have unique first names.
|
10
|
+
So when a parent logs in, he or she might understand urls like
|
11
|
+
|
12
|
+
http://example.com/children/john
|
13
|
+
http://example.com/children/paul
|
14
|
+
http://example.com/children/osiris
|
15
|
+
|
16
|
+
You have cleverly coded your application to not confuse children with the popular first name "Osiris", because you lookup children by slug
|
17
|
+
*and* by family_id, where family_id is also a property of the current user. It would really suck to have urls such as
|
18
|
+
|
19
|
+
http://example.com/children/osiris-34
|
20
|
+
|
21
|
+
As a parent, I would feel belittled by the constant reminder that 33 little Osirisses got to your site before my little Osiris did,
|
22
|
+
with his popular name casting a glow of irredeemable ordinariness about him.
|
23
|
+
|
24
|
+
Enough.
|
25
|
+
|
26
|
+
## Installation
|
27
|
+
|
28
|
+
Add this line to your application's Gemfile:
|
29
|
+
|
30
|
+
gem 'minislug'
|
31
|
+
|
32
|
+
And then execute:
|
33
|
+
|
34
|
+
$ bundle
|
35
|
+
|
36
|
+
Or install it yourself as:
|
37
|
+
|
38
|
+
$ gem install minislug
|
39
|
+
|
40
|
+
## Usage
|
41
|
+
|
42
|
+
class Person < ActiveRecord::Base
|
43
|
+
sluggable :full_name
|
44
|
+
end
|
45
|
+
|
46
|
+
p = Person.new :full_name => "Jàmês Jöÿcè"
|
47
|
+
p.slug # => "james-joyce"
|
48
|
+
p.save
|
49
|
+
|
50
|
+
Person.by_slug("james-joyce") # => <Person "full_name":"Jàmês Jöÿcè" "slug":"james-joyce">
|
51
|
+
|
52
|
+
No options, assumes "slug" attribute exists and is writable. No validations, no uniqueness checks.
|
53
|
+
|
54
|
+
## Contributing
|
55
|
+
|
56
|
+
1. Fork it
|
57
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
58
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
59
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
60
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/minislug.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require "minislug/version"
|
4
|
+
|
5
|
+
module Minislug
|
6
|
+
SUBSTITUTIONS = {
|
7
|
+
/[\s\/\\\(\)#\?]+/ => '-',
|
8
|
+
/\+/ => '-plus-',
|
9
|
+
/&/ => '-and-',
|
10
|
+
/-+/ => '-',
|
11
|
+
}
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
def sluggable source
|
15
|
+
class_attribute :slug_source
|
16
|
+
include InstanceMethods
|
17
|
+
|
18
|
+
self.slug_source = source
|
19
|
+
|
20
|
+
before_validation :set_slug
|
21
|
+
|
22
|
+
scope :by_slug, lambda { |slug| where(:slug => slug) }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.convert_to_slug txt
|
27
|
+
txt = txt.gsub(/ß/, "ss")
|
28
|
+
txt = txt.strip.mb_chars.normalize(:kd)
|
29
|
+
SUBSTITUTIONS.each do |reg, rep|
|
30
|
+
txt = txt.gsub reg, rep
|
31
|
+
end
|
32
|
+
txt.gsub(/[^0-9A-Za-z-]/, '').gsub(/^-/, '').gsub(/-$/, '')
|
33
|
+
end
|
34
|
+
|
35
|
+
module InstanceMethods
|
36
|
+
def set_slug
|
37
|
+
proto_slug = self.send(self.slug_source) || ""
|
38
|
+
self.slug = Minislug.convert_to_slug proto_slug
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
if defined?(ActiveRecord)
|
44
|
+
ActiveRecord::Base.send :extend, Minislug::ClassMethods
|
45
|
+
end
|
data/minislug.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- coding: utf-8; mode: ruby -*-
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
|
6
|
+
require 'minislug/version'
|
7
|
+
|
8
|
+
Gem::Specification.new do |gem|
|
9
|
+
gem.name = "minislug"
|
10
|
+
gem.version = Minislug::VERSION
|
11
|
+
gem.authors = ["Conan Dalton"]
|
12
|
+
gem.license = 'MIT'
|
13
|
+
gem.email = ["conan@conandalton.net"]
|
14
|
+
gem.description = %q{ Bare-bones minimum slugs. No validations or versioning. }
|
15
|
+
gem.summary = %q{ Bare-bones minimum slugs for permalinking your objects with friendly urls }
|
16
|
+
gem.homepage = "https://github.com/conanite/minislug"
|
17
|
+
|
18
|
+
gem.files = `git ls-files`.split($/)
|
19
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
20
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
21
|
+
gem.require_paths = ["lib"]
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: minislug
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Conan Dalton
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-10-11 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: " Bare-bones minimum slugs. No validations or versioning. "
|
14
|
+
email:
|
15
|
+
- conan@conandalton.net
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".gitignore"
|
21
|
+
- Gemfile
|
22
|
+
- LICENSE.txt
|
23
|
+
- README.md
|
24
|
+
- Rakefile
|
25
|
+
- lib/minislug.rb
|
26
|
+
- lib/minislug/version.rb
|
27
|
+
- minislug.gemspec
|
28
|
+
homepage: https://github.com/conanite/minislug
|
29
|
+
licenses:
|
30
|
+
- MIT
|
31
|
+
metadata: {}
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
requirements: []
|
47
|
+
rubygems_version: 3.0.3
|
48
|
+
signing_key:
|
49
|
+
specification_version: 4
|
50
|
+
summary: Bare-bones minimum slugs for permalinking your objects with friendly urls
|
51
|
+
test_files: []
|