sluggable_gemille 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_gemille.rb +51 -0
- metadata +44 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: b2a525bd5637b6f1252775c32701cbb7d72729c8
|
|
4
|
+
data.tar.gz: 7dff033ae912ce0087449cd0253ddb8dc3c91c47
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 8a2bc8255fee3ca281371700c7b616535cf244d770085bd30a4e58abcd7a57eeeaf2bd45fc03e8547dc2a33733b05f617e4fc96a863f4d3504fa456e84a50ac4
|
|
7
|
+
data.tar.gz: c5844a64dc5823f559086397ffa29bbe7c98c97cae841ef1505e530b2a9f3701a47a1e3d55925009f6c99989e3ba80120e6a1c25ffef00a0d20daf3f6aaddfa3
|
|
@@ -0,0 +1,51 @@
|
|
|
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
|
+
object = self.class.find_by slug: the_slug
|
|
16
|
+
|
|
17
|
+
count = 2
|
|
18
|
+
while object && object != self
|
|
19
|
+
the_slug = append_suffix(the_slug, count)
|
|
20
|
+
object = self.class.find_by slug: the_slug
|
|
21
|
+
count += 1
|
|
22
|
+
end
|
|
23
|
+
self.slug = the_slug.downcase
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def to_slug(name)
|
|
27
|
+
str = name.strip
|
|
28
|
+
|
|
29
|
+
#replace non alphanumeric characters with a dash
|
|
30
|
+
str.gsub! /\s*[^A-Za-z0-9]\s*/, '-'
|
|
31
|
+
|
|
32
|
+
#remove multiple, consecutive instances of a dash
|
|
33
|
+
str.gsub! /-+/, "-"
|
|
34
|
+
|
|
35
|
+
str.downcase
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def append_suffix(str, count)
|
|
39
|
+
if str.split('-').last.to_i != 0
|
|
40
|
+
return str.split('-').slice(0...-1).join('-') + "-" + count.to_s
|
|
41
|
+
else
|
|
42
|
+
return str + "-" + count.to_s
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
module ClassMethods
|
|
47
|
+
def sluggable_column(column_name)
|
|
48
|
+
self.slug_column = column_name
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: sluggable_gemille
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Gemille Ford
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2015-02-21 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: A robust slugging gem that replaces non-alphanumeric characters with
|
|
14
|
+
a dash and removes multiple, consecutive instances of a dash when generating a slug.
|
|
15
|
+
email: gemille.ford@gmail.com
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- lib/sluggable_gemille.rb
|
|
21
|
+
homepage: https://github.com/rkstarnerd/sluggable_gemille
|
|
22
|
+
licenses: []
|
|
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.1
|
|
41
|
+
signing_key:
|
|
42
|
+
specification_version: 4
|
|
43
|
+
summary: A gem that generates slugs
|
|
44
|
+
test_files: []
|