sluggable_tim 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.
- data/lib/sluggable_tim.rb +52 -0
- metadata +46 -0
@@ -0,0 +1,52 @@
|
|
1
|
+
module Sluggable
|
2
|
+
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
before_save :generate_slug!
|
7
|
+
class_attribute(:slug_col_name)
|
8
|
+
end
|
9
|
+
|
10
|
+
|
11
|
+
def to_param
|
12
|
+
self.slug
|
13
|
+
end
|
14
|
+
|
15
|
+
def generate_slug!
|
16
|
+
the_slug = to_slug(self.send(self.class.slug_col_name.to_sym))
|
17
|
+
obj = self.class.find_by(:slug => the_slug)
|
18
|
+
count = 2
|
19
|
+
while obj && obj != self
|
20
|
+
the_slug = append_suffix(the_slug, count)
|
21
|
+
obj = self.class.find_by(:slug => the_slug)
|
22
|
+
count += 1
|
23
|
+
end
|
24
|
+
|
25
|
+
self.slug = the_slug.downcase
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
def append_suffix(str, count)
|
30
|
+
if str.split('-').last.to_i != 0
|
31
|
+
str.split('-').slice(0...-1).join('-') + "-" + count.to_s
|
32
|
+
else
|
33
|
+
str + "-" + count.to_s #will always be "-2"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def to_slug(name)
|
38
|
+
str = name.strip
|
39
|
+
str.gsub! /\s*[^A-Za-z0-9]\s*/, '-'
|
40
|
+
str.gsub! /-+/,"-"
|
41
|
+
str.downcase
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
module ClassMethods
|
46
|
+
def assign_name_to_slug_column(col_name)
|
47
|
+
self.slug_col_name = col_name
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sluggable_tim
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Tim Pryor
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-01-24 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: For Tealeaf Academy, Course 2
|
15
|
+
email: tdpryor@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/sluggable_tim.rb
|
21
|
+
homepage: http://github.com
|
22
|
+
licenses: []
|
23
|
+
post_install_message:
|
24
|
+
rdoc_options: []
|
25
|
+
require_paths:
|
26
|
+
- lib
|
27
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 1.8.24
|
42
|
+
signing_key:
|
43
|
+
specification_version: 3
|
44
|
+
summary: A gem for slugging functionality
|
45
|
+
test_files: []
|
46
|
+
has_rdoc:
|