sluggable_ellery_mar 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_ellery_mar.rb +52 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 29b9de78a08393305851745dbb93ae91a8cd9abf
|
4
|
+
data.tar.gz: ff3e6b934cce0ee3b8c180d539d627f91ec44b0a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 08f56efdacc2a042705d9c3d95279baf19eb66f9640e61e85fe7369fb0aca48c376f8dc4b42d7953d39211d1b5de4b62357c3395cb8e8a3f24334d9315c6776d
|
7
|
+
data.tar.gz: df30ef001ba90e27c696717a7e2e109e4b678aec285c8519ef373f68ba9d0387394c83da703052c34d3b27904f36a063661af599acc04e4028bfc68e67ff073c
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Sluggable
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
before_save :generate_slug!
|
6
|
+
class_attribute :slug_column
|
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_column.to_sym))
|
15
|
+
obj = self.class.find_by(slug: the_slug)
|
16
|
+
count = 2
|
17
|
+
while obj && obj != self
|
18
|
+
the_slug = append(the_slug, count)
|
19
|
+
obj = self.class.find_by(slug: the_slug)
|
20
|
+
count += 1
|
21
|
+
end
|
22
|
+
self.slug = the_slug
|
23
|
+
end
|
24
|
+
|
25
|
+
def append(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(title)
|
34
|
+
str = title.downcase
|
35
|
+
str.gsub!(/\s*[^a-zA-Z0-9]\s*/, '-')
|
36
|
+
arr = str.split('-')
|
37
|
+
|
38
|
+
arr.each do |char|
|
39
|
+
if char == ''
|
40
|
+
arr.delete(char)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
return str = arr.join('-')
|
45
|
+
end
|
46
|
+
|
47
|
+
module ClassMethods
|
48
|
+
def sluggable_column(col_name)
|
49
|
+
self.slug_column = col_name
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sluggable_ellery_mar
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ellery Temple Jr.
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-11 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Generate slugs
|
14
|
+
email: elreyatee@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/sluggable_ellery_mar.rb
|
20
|
+
homepage: http://rubygems.org/gems/sluggable
|
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.2.2
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Sluggable module
|
44
|
+
test_files: []
|