sluggable_wynne 0.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/sluggable_wynne.rb +47 -0
- metadata +43 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: a995b84e98654d6558bffae4eadf16a6da3a320d
|
|
4
|
+
data.tar.gz: c63f68b842f59e94c7476a24d85d81c3b09b3599
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 8b5b933c21779e8462b55b194a964f037fbf2221392815cfa32f24bc1f038c037cfbc14efa2b9f690d6de84de36c8210feb664e668c2325bbfb46acebedf51f2
|
|
7
|
+
data.tar.gz: 538c2b891a61475181d40e4969862b6c2f8462fe2f3292bf1b07aa8f910c2a4ea96aaa2b4accc2c95f2a36facf134e8094aca45e00efcea48cd46bc1e9e0f501
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
module Sluggable
|
|
2
|
+
extend ActiveSupport::Concern
|
|
3
|
+
|
|
4
|
+
included do
|
|
5
|
+
before_save :generate_slug
|
|
6
|
+
class_attribute :slug_column
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def to_param
|
|
10
|
+
self.slug
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def generate_slug
|
|
14
|
+
the_slug = to_slug(self.send(self.class.slug_column.to_sym))
|
|
15
|
+
post = self.class.find_by slug: the_slug # look if post by the same slug already exists
|
|
16
|
+
count = 2
|
|
17
|
+
while post && post != self
|
|
18
|
+
the_slug = append_suffix(the_slug, count)
|
|
19
|
+
post = self.class.find_by slug: the_slug
|
|
20
|
+
count += 1
|
|
21
|
+
end
|
|
22
|
+
self.slug = the_slug
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def to_slug(name)
|
|
26
|
+
str = name.strip
|
|
27
|
+
str.gsub! /\s*[^A-Za-z0-9]\s*/, '-' # replaces everything other than alphanumerical characters with a dash
|
|
28
|
+
str.gsub! /-+/, "-" # if multiple dashes in a row, reduces it to 1
|
|
29
|
+
str.downcase
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def append_suffix(str, count)
|
|
33
|
+
if str.split('-').last.to_i != 0 # if the last element is an integer
|
|
34
|
+
return str.split('-').slice(0...-1).join('-') + '-' + count.to_s
|
|
35
|
+
else
|
|
36
|
+
return str + '-' + count.to_s
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
module ClassMethods
|
|
42
|
+
def sluggable_column(col_name)
|
|
43
|
+
self.slug_column = col_name
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: sluggable_wynne
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Wynne Lo
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2015-06-04 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: The best slugging gem ever.
|
|
14
|
+
email: wynnemlo@gmail.com
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- lib/sluggable_wynne.rb
|
|
20
|
+
homepage: http://wynnelo.com
|
|
21
|
+
licenses: []
|
|
22
|
+
metadata: {}
|
|
23
|
+
post_install_message:
|
|
24
|
+
rdoc_options: []
|
|
25
|
+
require_paths:
|
|
26
|
+
- lib
|
|
27
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
28
|
+
requirements:
|
|
29
|
+
- - '>='
|
|
30
|
+
- !ruby/object:Gem::Version
|
|
31
|
+
version: '0'
|
|
32
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
33
|
+
requirements:
|
|
34
|
+
- - '>='
|
|
35
|
+
- !ruby/object:Gem::Version
|
|
36
|
+
version: '0'
|
|
37
|
+
requirements: []
|
|
38
|
+
rubyforge_project:
|
|
39
|
+
rubygems_version: 2.4.6
|
|
40
|
+
signing_key:
|
|
41
|
+
specification_version: 4
|
|
42
|
+
summary: A gem to add slugs to your URL
|
|
43
|
+
test_files: []
|