sluggable_jl 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_jl.rb +47 -0
- metadata +43 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b1d41c4127f0c97fe9fd36fe96fb324aaabd22c8
|
4
|
+
data.tar.gz: c65942cf86724ba431e6ee638e1546ce92538f31
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 37275e788742a885483a96574abf3f05ef99852b938a779e2f71b82242aa3dc1860fe5c8726aef15532f7fd83f3704a046c60b0d4c2830cad09e3e926f271297
|
7
|
+
data.tar.gz: 16f82d5d02a8e0bba6b4d1cd4e516ea9cbc59594728910e0c12c006280ae2cd5bc71bb5825e6ec22bd55a78166db0ab8a6ead6edafaa5e26313cb104869795e3
|
data/lib/sluggable_jl.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
module Sluggable
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
before_create :generate_slug
|
6
|
+
class_attribute :slug_col
|
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_col.to_sym))
|
15
|
+
obj = self.class.find_by(slug: the_slug)
|
16
|
+
count = 2
|
17
|
+
|
18
|
+
while obj && obj != self
|
19
|
+
the_slug = append_suffix(the_slug, count)
|
20
|
+
obj = self.class.find_by(slug: the_slug)
|
21
|
+
count += 1
|
22
|
+
end
|
23
|
+
self.slug = the_slug.downcase
|
24
|
+
end
|
25
|
+
|
26
|
+
def append_suffix(str, count)
|
27
|
+
if str.split("-").last.to_i != 0
|
28
|
+
return str.split("-").slice(0...-1).join("-") + "-" + count.to_s
|
29
|
+
else
|
30
|
+
return str + "-" + count.to_s
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def to_slug(name)
|
35
|
+
str = name.strip
|
36
|
+
str.gsub!(/\s*[^A-Za-z0-9]\s*/, "-")
|
37
|
+
str.gsub!(/-+/, "-")
|
38
|
+
str.downcase
|
39
|
+
end
|
40
|
+
|
41
|
+
module ClassMethods
|
42
|
+
def sluggable_column(col_name)
|
43
|
+
self.slug_col = col_name
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
metadata
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sluggable_jl
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Josh Lee
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-05-01 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Create slugs for your models
|
14
|
+
email: josh@joshlee.tv
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/sluggable_jl.rb
|
20
|
+
homepage: http://joshlee.tv
|
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.0.14
|
40
|
+
signing_key:
|
41
|
+
specification_version: 4
|
42
|
+
summary: Slugging gem
|
43
|
+
test_files: []
|