sluggable_buddy 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_buddy.rb +46 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cb917af4ac8ccd0e14050d5c44f4586a81e89f69
|
4
|
+
data.tar.gz: ceff22d233d498bea071d05d59a2d8148b4f8ebf
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 639290cf86ff4b3bcade9b3c629bbd04ac5c8517627ed1ad975f60bbff34175156b558c2425c6c30828898ba5fd94d93fb8a9506adf40cdfd8f3834cae6accf7
|
7
|
+
data.tar.gz: 4772e170b92cbe49253e37760ae18df526de16d5b33a3cc84e523f097781eefefdc15086a66eef961bd0270ba0bd97fbdc232362ffc8b3390505e10249f8391d
|
@@ -0,0 +1,46 @@
|
|
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
|
+
obj = self.class.find_by(slug: the_slug)
|
16
|
+
count = 2
|
17
|
+
while obj && obj != self
|
18
|
+
the_slug = append_suffix(the_slug, count)
|
19
|
+
obj = self.class.find_by(slug: the_slug)
|
20
|
+
count += 1
|
21
|
+
end
|
22
|
+
self.slug = the_slug.downcase
|
23
|
+
end
|
24
|
+
|
25
|
+
def append_suffix(str, count)
|
26
|
+
if str.split('-').last.to_i !=0
|
27
|
+
return str.split('-').slice(0...-1).join('-') + "-" + count.to_s
|
28
|
+
else
|
29
|
+
return str + "-" + count.to_s
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def to_slug(title_name)
|
34
|
+
str = title_name.strip
|
35
|
+
str.gsub!(/\s*[^A-Za-z0-9]\s*/, '-')
|
36
|
+
str.gsub!(/-+/, '-')
|
37
|
+
str.downcase
|
38
|
+
end
|
39
|
+
|
40
|
+
module ClassMethods
|
41
|
+
def sluggable_column(col_name)
|
42
|
+
self.slug_column = col_name
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sluggable_buddy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Larry Lumbreras
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-01-04 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: This is a gem for converting string to slug.
|
14
|
+
email: llumbreras@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/sluggable_buddy.rb
|
20
|
+
homepage: https://github.com/llumbreras/sluggable-buddy
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.1.11
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: A sluggable gem.
|
44
|
+
test_files: []
|