sluggable_lily 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_lily.rb +47 -0
- metadata +43 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 82aca774fa1311bbb86e30fa6c3962e9feec9f44
|
|
4
|
+
data.tar.gz: b6d8474d0d0b6c36180316bac8301bb46f327e26
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 1ad1b86a516c01ce190c8a0a62c56b15035a0b4998703c6ef63ed19b957774f535750c2f0fb8027d88da5aba2495ada7af401fe11f6248c343caef1d57ca9195
|
|
7
|
+
data.tar.gz: 126fb7508de522c1c86664c5ae8dc083699593fee827a99fb1823295a1dc0d49d6bbdd1e18a396704792a1dea8bf0784813ba26ef41329702a37d110b7add6f2
|
|
@@ -0,0 +1,47 @@
|
|
|
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 generate_slug
|
|
10
|
+
generated_slug = sluggify_title(self.send(self.class.slug_column.to_sym))
|
|
11
|
+
#Local variable is set to the sluggified version of the obj's title.
|
|
12
|
+
|
|
13
|
+
obj = self.class.find_by(slug: generated_slug)
|
|
14
|
+
count = 2
|
|
15
|
+
while obj && obj != self
|
|
16
|
+
# While a obj object exists that has a slug that matches our generated_slug, AND while the obj isn't the one we're currently dealing with (in case we're updating a obj).
|
|
17
|
+
generated_slug = append_suffix(generated_slug, count)
|
|
18
|
+
obj = obj.find_by(slug: generated_slug)
|
|
19
|
+
# Look and see if there's now a match for a obj object with a slug that matches our *new* generated_slug.
|
|
20
|
+
count += 1
|
|
21
|
+
# Adds one to the count in case we go through this while loop again.
|
|
22
|
+
end
|
|
23
|
+
self.slug = generated_slug
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def sluggify_title(str)
|
|
27
|
+
str = str.gsub(/\s*[^a-zA-Z0-9]\s*/, '-').gsub(/-+/, '-').sub(/-$/, '').downcase
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def append_suffix(str, count)
|
|
31
|
+
if str.split('-').last.to_i != 0
|
|
32
|
+
str.split('-').slice(0...-1).join('-') + "-" + count.to_s
|
|
33
|
+
else
|
|
34
|
+
str + "-" + count.to_s
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def to_param
|
|
39
|
+
self.slug
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
module ClassMethods
|
|
43
|
+
def slug_generator_column(column_name)
|
|
44
|
+
self.slug_column = column_name
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: sluggable_lily
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Lily Jen
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2015-07-12 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Slugging gem.
|
|
14
|
+
email: lily@lilyjen.com
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- lib/sluggable_lily.rb
|
|
20
|
+
homepage: http://lilyjen.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.3
|
|
40
|
+
signing_key:
|
|
41
|
+
specification_version: 4
|
|
42
|
+
summary: A slugging gem
|
|
43
|
+
test_files: []
|