dude_its_sluggable 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/dude_its_sluggable.rb +42 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2776c71a87fd18463536aadd0f445a2cf915eafa
|
4
|
+
data.tar.gz: 4cd9dd86c41635d9b136f69ea7ad4ab08e44c0a2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3844b0e671632a5dc3ebf797a47be1bb49e95bcb368c991a99e98cabe650c970ae37a5714041511ea41743e94e653e7fbd010d65d16d0f2fed5e48bc37d8b24e
|
7
|
+
data.tar.gz: 2ec45dd1645831b0405f05ba273bcd6420c2822ec8b21e1e4351f873c1dba21f8ebdc646b793e576d56954a7e09e3da64ba22ede4336dfc2d8988435f543e541
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Sluggable
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
before_save :generate_slug!
|
6
|
+
end
|
7
|
+
|
8
|
+
def to_param
|
9
|
+
self.slug
|
10
|
+
end
|
11
|
+
|
12
|
+
def generate_slug!
|
13
|
+
the_slug = to_slug(value_to_slug)
|
14
|
+
obj = self.class.find_by(slug: the_slug)
|
15
|
+
count = 2
|
16
|
+
while obj && obj != self
|
17
|
+
the_slug = append_suffix(the_slug, count)
|
18
|
+
obj = self.class.find_by(slug: the_slug)
|
19
|
+
count += 1
|
20
|
+
end
|
21
|
+
self.slug = the_slug.downcase
|
22
|
+
end
|
23
|
+
|
24
|
+
def value_to_slug
|
25
|
+
fail NotImplementedError, "Abstract method: defined by targeted subclass."
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_slug(name)
|
29
|
+
str = name.strip
|
30
|
+
str.gsub!(/\s*[^A-Za-z0-9]\s*/, "-")
|
31
|
+
str.gsub!(/-+/, "-")
|
32
|
+
str.downcase
|
33
|
+
end
|
34
|
+
|
35
|
+
def append_suffix(str, count)
|
36
|
+
if str.split("-").last.to_i != 0
|
37
|
+
return str.split("-").slice(0...-1).join("-") + "-" + count.to_s
|
38
|
+
else
|
39
|
+
return str + "-" + count.to_s
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dude_its_sluggable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matt Fernandez
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-07 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Nah
|
14
|
+
email: matiasfmolinari@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/dude_its_sluggable.rb
|
20
|
+
homepage: https://github.com/MatiasFMolinari
|
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.8
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Wat
|
44
|
+
test_files: []
|