sluggable_th 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/sluggable_th.rb +46 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3583f0c64fc4c2df3deb8e07023c2d4f602f4cce
|
4
|
+
data.tar.gz: 5d5b1995eecbdebe33d2f5998eb2091bcfe4bc89
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a7b21fe05b62cb08736f75894d6e20e2aa0df83b68372fdb3fff4f60df5ded3280e619c9a13b91a6313ce3655873faacae25c20a11c63f989252fe4959b85a90
|
7
|
+
data.tar.gz: f8434e413c9ad7d9dc79f0bc2c51fe114fd5cc31df135de3c786b668548ba55fb7ae6cb034fb3a9797dc4fd06988a1e118ef7e30c81aae1e621b9f07ca6c872f
|
data/lib/sluggable_th.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
module SluggableTh
|
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(name)
|
34
|
+
str = 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_th
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tsung-Hua Tsai
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-18 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Set up slugs for Rails
|
14
|
+
email: tsai.tsunghua@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/sluggable_th.rb
|
20
|
+
homepage: http://tsaith.github.io/
|
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.4.4
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Set up slugs
|
44
|
+
test_files: []
|