sluggable_postit 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_postit.rb +47 -0
- metadata +43 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a57a38dea047101a113f7ce1ef9b0ba5ac0906f1
|
4
|
+
data.tar.gz: 55da78ff5b15fe1984cef26a94af921acba42864
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b7ff5d63a1f452b5c74918e3d0077587b76bd3d7d932b85d01d7d93bc00b15b1309e057d6486dae4e1656fbda7eafa863e2416cebe81ff15619c1921517b1643
|
7
|
+
data.tar.gz: 1abd39a040730012916eb45d7dbcfe303b482a34188bde1a9e921b529fcfe958fa9d14cab2b854cd0cb3de7bfcd84f170e8932b2946fe91695dd9519a8d5f20f
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Sluggable
|
2
|
+
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
before_save :generate_slug!
|
7
|
+
class_attribute :slug_column
|
8
|
+
end
|
9
|
+
|
10
|
+
def generate_slug!
|
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
|
35
|
+
end
|
36
|
+
|
37
|
+
def to_param
|
38
|
+
self.slug
|
39
|
+
end
|
40
|
+
|
41
|
+
module ClassMethods
|
42
|
+
def sluggable_column(col_name)
|
43
|
+
self.slug_column = col_name
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
metadata
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sluggable_postit
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rein Van Imschoot
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-18 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A slugging gem for postit app
|
14
|
+
email: reinvanimschoot@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/sluggable_postit.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: []
|