sluggable_mjvezzani_aug 0.0.0 → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/sluggable_mjvezzani_aug.rb +28 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0e5da68d542a9c7d6b26079998c1e233826a9e4f
|
4
|
+
data.tar.gz: bac19ea0d61fa325976ab6c0d68db119df68bb0f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8f4e9888750e0a49504006dbdd89a1950cc484df6aeada8c64cfd8e8a21d420f65d8a09bcd61c3a5ad7b934bfbfd77ee5ec6d0a8ae71e346cd8c8c927ad0cee5
|
7
|
+
data.tar.gz: c144fbdf17a813d9605f5fc376ff146fbbf26d7c11ace7a9fcc94bd414253b2ebce196eae00bb34af3753687ecc609e0842f53e352b713d9fb36abaa9758f719
|
@@ -8,7 +8,30 @@ module Sluggable
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def generate_slug
|
11
|
-
|
11
|
+
the_slug = to_slug(self.send(self.class.slug_column.to_sym))
|
12
|
+
obj = self.class.find_by slug: the_slug
|
13
|
+
count = 2
|
14
|
+
while obj && obj != self
|
15
|
+
the_slug = append_suffix(the_slug, count)
|
16
|
+
obj = self.class.find_by slug: the_slug
|
17
|
+
count +=1
|
18
|
+
end
|
19
|
+
self.slug = the_slug.downcase
|
20
|
+
end
|
21
|
+
|
22
|
+
def append_suffix(str, count)
|
23
|
+
if str.split('-').last.to_i != 0
|
24
|
+
return str.split('-').slice(0...-1).join('-') + "-" + count.to_s
|
25
|
+
else
|
26
|
+
return str + "-" + count.to_s
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def to_slug(name)
|
31
|
+
str = name.strip
|
32
|
+
str.gsub! /\s*[^A-Za-z0-9]\s*/, '-'
|
33
|
+
str.gsub! /-+/, "-"
|
34
|
+
str.downcase
|
12
35
|
end
|
13
36
|
|
14
37
|
def to_param
|
@@ -16,9 +39,12 @@ module Sluggable
|
|
16
39
|
end
|
17
40
|
|
18
41
|
module ClassMethods
|
42
|
+
# This allows for sluggable_column(:title), or sluggable_column(:name)
|
43
|
+
# to be called in the model and enables the generate_slug method
|
44
|
+
# to work.
|
19
45
|
def sluggable_column(col_name)
|
20
46
|
self.slug_column = col_name
|
21
47
|
end
|
22
48
|
end
|
23
49
|
|
24
|
-
end
|
50
|
+
end
|