sluggable_jason_nov 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_jason_nov.rb +44 -0
- metadata +43 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e9fa0058aad5abc64418fdca0b5f8a99c01a35b9
|
4
|
+
data.tar.gz: f68b7b9f2f5406c19374c381277205bc359af4ab
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ba296f60ab5ed1d15ad9da1d0a0bc33ae962f605c0882b3e9eff9a653f4b572a8c72f1763afa4c06355b020abfc82612a5b8e3c8144208d979c03c660cf67136
|
7
|
+
data.tar.gz: 091c83f5cea60282175785d74049cdbc962d83e8f50b5a73f31e0c28a2c27d97b9c88a25b1a0c0b4abb12edf8918751ff54e019da5f6138af8fddf75060b507b
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Sluggable
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
class_attribute :slug_column
|
6
|
+
before_save :generate_slug!
|
7
|
+
end
|
8
|
+
|
9
|
+
def generate_slug!
|
10
|
+
sluggable_col_name = self.class.slug_column.to_s
|
11
|
+
sluggable_col_value = self.send(sluggable_col_name).to_s
|
12
|
+
slug_url = clean_string(sluggable_col_value)
|
13
|
+
obj = self.class.find_by slug: slug_url
|
14
|
+
count = 2
|
15
|
+
while obj && obj != self
|
16
|
+
slug_url = append_suffix(slug_url, count)
|
17
|
+
obj = self.class.find_by slug: slug_url
|
18
|
+
count += 1
|
19
|
+
end
|
20
|
+
self.slug = slug_url
|
21
|
+
end
|
22
|
+
|
23
|
+
def append_suffix(string, count)
|
24
|
+
if string.split("--").last.to_i != 0
|
25
|
+
return string.split("--").first + "--" + count.to_s
|
26
|
+
else
|
27
|
+
return string + "--" + count.to_s
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def clean_string(string)
|
32
|
+
string.gsub(/[^0-9a-z ]/i, '').gsub(" ", "-").gsub(/-+/, "-").downcase
|
33
|
+
end
|
34
|
+
|
35
|
+
def to_param
|
36
|
+
self.slug
|
37
|
+
end
|
38
|
+
|
39
|
+
module ClassMethods
|
40
|
+
def sluggable_column(col_name)
|
41
|
+
self.slug_column = col_name
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
metadata
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sluggable_jason_nov
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jason Slusarchuk
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-08 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A gem that adds sluggable functionality to your models.
|
14
|
+
email: jason.slusarchuk@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/sluggable_jason_nov.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.4.6
|
40
|
+
signing_key:
|
41
|
+
specification_version: 4
|
42
|
+
summary: A gem for creating url slugs
|
43
|
+
test_files: []
|