sluggable_peter 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_peter.rb +46 -0
- metadata +43 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6dc62f6f135a680169af9579dd65c8c60c9bfd42
|
4
|
+
data.tar.gz: 3678df85c396ca96a291a909db578abdebc133d5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0581469b6f526526564168f4191896a24ee5a9e2c30614a029a593555f0fcab62a2253cdab15616607423e0a2edd24489a25088451508580e5e9ad13d628a080
|
7
|
+
data.tar.gz: 04eebfd168a08d759d16ab2c9ea5c7ac336f449b022f46f160214bff8403fab0469e9b3545f4dc4ec9c1561a91618988256acca5244e314b476d6737284e75d0
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module Sluggable
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
included do
|
4
|
+
before_save :create_slug!
|
5
|
+
class_attribute :slug_column
|
6
|
+
end
|
7
|
+
|
8
|
+
def to_param
|
9
|
+
self.slug
|
10
|
+
end
|
11
|
+
|
12
|
+
def create_slug!
|
13
|
+
the_slug = to_slug(self.send(self.class.slug_column.to_sym))
|
14
|
+
object = self.class.find_by slug: the_slug
|
15
|
+
count = 2
|
16
|
+
while object && object != self
|
17
|
+
the_slug = append_suffix(the_slug, count)
|
18
|
+
object = self.class.find_by slug: the_slug
|
19
|
+
count += 1
|
20
|
+
end
|
21
|
+
|
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
|
+
end
|
46
|
+
|
metadata
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sluggable_peter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Peter Karth
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-31 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: The most basic slugging ever
|
14
|
+
email: falonofthetower@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/sluggable_peter.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: Slugging gem
|
43
|
+
test_files: []
|