sluggable_doug 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_doug.rb +47 -0
- metadata +43 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 024dc329c7faa229e27674fe3e94d9c9e49e9fd1
|
4
|
+
data.tar.gz: 42b8edfb074f1b18f8f9b67e25f103f08381e737
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 18fde9bbcc59c971f50bc0f05fd3e9262e86a5de07c4e20f1220d4b8581c4c8bc7af9e77b2873f5692b9a6aba2118e3bb9a1c59442f0272af15c6226a4682bcd
|
7
|
+
data.tar.gz: 316da406f7c210be1c93acde8ceeb9d8b7f15ac390ca4099aacaa157b75040be50cf08e0d1cb169b80e377111b91b2385563da7c9df48aa2012c9ecf3114fee4
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module SluggableDoug
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
before_save :generate_slug!
|
6
|
+
class_attribute :slug_column
|
7
|
+
end
|
8
|
+
|
9
|
+
def generate_slug!
|
10
|
+
the_slug = to_slug(self.send(self.class.slug_column.to_sym)) #turn the current post title into a slug
|
11
|
+
|
12
|
+
object = self.class.find_by slug: the_slug #try to find the object with the slug we just created
|
13
|
+
count = 2
|
14
|
+
while object && object != self #while there is an object and it's not equal to the current object
|
15
|
+
the_slug = append_suffix(the_slug, count) #detirmine the proper suffix and add it to slug
|
16
|
+
object = self.class.find_by slug: the_slug #see if there is a post with the new slug
|
17
|
+
count += 1
|
18
|
+
end
|
19
|
+
self.slug = the_slug.downcase # assign the new slug to the object in lowercase
|
20
|
+
end
|
21
|
+
|
22
|
+
def append_suffix(str, count)
|
23
|
+
if str.split('-').last.to_i != 0 #if the last item in the string is not a number
|
24
|
+
#split the string on the dashes, slice off the last item, rejoin on the dashes,
|
25
|
+
return str.split('-').slice(0...-1).join('-') + "-" + count.to_s
|
26
|
+
else
|
27
|
+
return str + "-" + count.to_s #just add -count to the end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def to_slug(name)
|
32
|
+
str = name.strip
|
33
|
+
str.gsub!(/\s*[^A-Za-z0-9]\s*/ , '-')
|
34
|
+
str.gsub!(/-+/, '-')
|
35
|
+
str.downcase
|
36
|
+
end
|
37
|
+
|
38
|
+
def to_param
|
39
|
+
self.slug
|
40
|
+
end
|
41
|
+
|
42
|
+
module ClassMethods
|
43
|
+
def sluggable_column(column_name)
|
44
|
+
self.slug_column = column_name
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
metadata
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sluggable_doug
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Doug Steinberg
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-07-10 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: The best slugging gem ever
|
14
|
+
email: dstein-phins@hotmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/sluggable_doug.rb
|
20
|
+
homepage: http://github.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.2.2
|
40
|
+
signing_key:
|
41
|
+
specification_version: 4
|
42
|
+
summary: A slugging gem
|
43
|
+
test_files: []
|