sluggable_rockstar 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_rockstar.rb +49 -0
- metadata +45 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 531adf3bfeb97d9d5e619c3a6f7472b25732f669
|
|
4
|
+
data.tar.gz: 850ca82082979ea5a3312dbd2f395c0e023e6527
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 14ed7d5439e4a9698197398a15eac7c0771f0984c1a1892544bb1db206264b0fce48bcd7318916ffa15712c5a9d251f11c1f18e9dc64587173cd7b901174ac1f
|
|
7
|
+
data.tar.gz: 44b87c89a95aaae87f68af3632d6581cb7088f88e2edbf09e441513dac3460a7dcf9b77f9596cc5186c002aa704ac493bbd192c7c6e0ddc5a4ceea5c02d4c2a8
|
|
@@ -0,0 +1,49 @@
|
|
|
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
|
+
new_slug = to_slug(self.send(self.class.slug_column.to_sym))
|
|
15
|
+
|
|
16
|
+
# if a obj with this slug exists, uniquify it
|
|
17
|
+
obj = self.class.find_by slug: new_slug
|
|
18
|
+
|
|
19
|
+
count = 2
|
|
20
|
+
while obj && obj != self
|
|
21
|
+
new_slug = append_suffix(new_slug, count)
|
|
22
|
+
obj = self.class.find_by slug: new_slug
|
|
23
|
+
count += 1
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
self.slug = new_slug
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def append_suffix(str, count)
|
|
30
|
+
if str.split('-').last.to_i != 0
|
|
31
|
+
return str.split("-").slice(0...-1).join("-") + "-" + count.to_s
|
|
32
|
+
else
|
|
33
|
+
return str + "-" + count.to_s
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def to_slug(name)
|
|
38
|
+
str = name.strip.downcase
|
|
39
|
+
str.gsub!(/\s*[^a-z0-9]\s*/, "-")
|
|
40
|
+
str.gsub!(/-+/, "-")
|
|
41
|
+
return str
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
module ClassMethods
|
|
45
|
+
def sluggable_column(col_name)
|
|
46
|
+
self.slug_column = col_name
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: sluggable_rockstar
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Michael Mentele
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2016-10-03 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Include on objects to add slugging behavior
|
|
14
|
+
email: michaelrmentele@gmail.com
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- lib/sluggable_rockstar.rb
|
|
20
|
+
homepage: https://github.com/MichaelrMentele/sluggable-rockstar.git
|
|
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.4.6
|
|
41
|
+
signing_key:
|
|
42
|
+
specification_version: 4
|
|
43
|
+
summary: A slugging gem that uses regex to convert a str attribute on a model to a
|
|
44
|
+
unique slug.
|
|
45
|
+
test_files: []
|