slugable_taylor 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/slugable_taylor.rb +39 -0
- metadata +43 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 80de4767f350519a6fdb17b8e0826bd0ca4916c3
|
4
|
+
data.tar.gz: 588cc302eaff38d2c0eb8afc339eda5162468d6a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b273b773d42778ad3fcad2675c63696546e49689ac8c3ccf47ff895f22364c9bc0f30b20a5a234d7eef3db6a8fbd3a2b32da54b785586717aeb25a2fa09faa9b
|
7
|
+
data.tar.gz: bf7f81b2fb018ccc190add2aea2fb04ad0efd271016fb843e1e64916f640401fa1e02e1bbd502633147b0d5b3bb8c31e6324b32e6903140c87f464fbb84e61a7
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Slugable
|
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
|
+
string = self.send(self.class.slug_column.to_sym)
|
11
|
+
str = string.strip.gsub(/\W+/, '-').downcase
|
12
|
+
other_slugs = self.class.all.select { |obj| obj != self }.map { |obj| obj.slug }
|
13
|
+
if other_slugs.include?(str)
|
14
|
+
str += str.chars.last == '-' ? "2" : "-2"
|
15
|
+
str = confirm_slug_uniqueness(str, other_slugs)
|
16
|
+
end
|
17
|
+
self.slug = str
|
18
|
+
end
|
19
|
+
|
20
|
+
def confirm_slug_uniqueness(str, other_slugs)
|
21
|
+
loop do
|
22
|
+
return str unless other_slugs.include?(str)
|
23
|
+
str = str.split('-')
|
24
|
+
str << (str.pop.to_i + 1).to_s
|
25
|
+
str = str.join('-')
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_param
|
30
|
+
self.slug
|
31
|
+
end
|
32
|
+
|
33
|
+
module ClassMethods
|
34
|
+
def slugable_column(name_provided)
|
35
|
+
self.slug_column = name_provided
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: slugable_taylor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Taylor Peat
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-06 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Slugging gem for making strings URL friendly.
|
14
|
+
email: taylorpeat@outlook.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/slugable_taylor.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.5.0
|
40
|
+
signing_key:
|
41
|
+
specification_version: 4
|
42
|
+
summary: A slugging gem
|
43
|
+
test_files: []
|