superslug 1.0.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 +7 -0
- data/lib/superslug.rb +4 -0
- data/lib/superslug/superslug.rb +59 -0
- data/lib/superslug/version.rb +3 -0
- data/superslug.gemspec +13 -0
- metadata +50 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4bb95a3918571a903c9a509d9c946b210574a044
|
4
|
+
data.tar.gz: ab49423eea4705981fe6f92b3313c6101449b417
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 42be65db36d8de0dd0f295172faa5c5663ffd37ce4fbd32cd5f210e9792c6992c83a0a1e0515423dcb2dfd6a8644c0f7e01ecfd664e3d98510f8ff7b64fc2a93
|
7
|
+
data.tar.gz: ca55bbda8233c591f3367886019d06e85e68e7977596fd8b1da9a0e6cfe90708334f28a34dca92cd76ae93d4c9bd8bde4c3af08bf55f30454ccc7d8b3078e21f
|
data/lib/superslug.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
module SuperSlug
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
after_save :sluggify_slug
|
6
|
+
|
7
|
+
def self.find(input)
|
8
|
+
if input.class == Array
|
9
|
+
super
|
10
|
+
else
|
11
|
+
input.to_i.to_s == input.to_s ? super : find_by_slug(input)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def sluggify_slug
|
17
|
+
if slug.blank?
|
18
|
+
update_column(:slug, create_slug)
|
19
|
+
else
|
20
|
+
new_slug = slug.gsub(/[^a-zA-Z0-9 \-]/, "") # remove all bad characters
|
21
|
+
new_slug.gsub!(/\ /, "-") # replace spaces with underscores
|
22
|
+
new_slug.gsub!(/\-+/, "-") # replace repeating underscores
|
23
|
+
update_column(:slug, new_slug) unless slug == new_slug
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def create_slug
|
28
|
+
slug = self.title.downcase.gsub(/\&/, ' and ') # & -> and
|
29
|
+
slug.gsub!(/[^a-zA-Z0-9 \-]/, "") # remove all bad characters
|
30
|
+
slug.gsub!(/\ /, "-") # replace spaces with underscores
|
31
|
+
slug.gsub!(/\-+/, "-") # replace repeating underscores
|
32
|
+
|
33
|
+
dups = self.class.name.constantize.where(:slug => slug)
|
34
|
+
if dups.count == 1 and dups.first != self
|
35
|
+
if self.try(:idx)
|
36
|
+
slug = "#{slug}-#{self.idx}"
|
37
|
+
else
|
38
|
+
slug = "#{slug}-#{self.id}"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
slug
|
42
|
+
end
|
43
|
+
|
44
|
+
def make_slug_unique(slug)
|
45
|
+
dups = self.class.name.constantize.where(:slug => slug)
|
46
|
+
if dups.count == 1 and dups.first != self
|
47
|
+
if self.try(:idx)
|
48
|
+
slug = "#{slug}-#{self.idx}"
|
49
|
+
else
|
50
|
+
slug = "#{slug}-#{self.id}"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
slug
|
54
|
+
end
|
55
|
+
|
56
|
+
def to_param
|
57
|
+
slug
|
58
|
+
end
|
59
|
+
end
|
data/superslug.gemspec
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'superslug/version'
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = 'superslug'
|
6
|
+
s.version = SuperSlug::VERSION
|
7
|
+
s.summary = "Slugs, yo!"
|
8
|
+
s.description = "A simple gem to autocreate slugs"
|
9
|
+
s.authors = ['Warren Harrison','Sean Davis']
|
10
|
+
s.email = ['warren@hungry-media.com','scdavis41@gmail.com']
|
11
|
+
s.files = `git ls-files -z`.split("\x0")
|
12
|
+
s.license = 'MIT'
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: superslug
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Warren Harrison
|
8
|
+
- Sean Davis
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-04-02 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: A simple gem to autocreate slugs
|
15
|
+
email:
|
16
|
+
- warren@hungry-media.com
|
17
|
+
- scdavis41@gmail.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- lib/superslug.rb
|
23
|
+
- lib/superslug/superslug.rb
|
24
|
+
- lib/superslug/version.rb
|
25
|
+
- superslug.gemspec
|
26
|
+
homepage:
|
27
|
+
licenses:
|
28
|
+
- MIT
|
29
|
+
metadata: {}
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
requirements: []
|
45
|
+
rubyforge_project:
|
46
|
+
rubygems_version: 2.2.0
|
47
|
+
signing_key:
|
48
|
+
specification_version: 4
|
49
|
+
summary: Slugs, yo!
|
50
|
+
test_files: []
|