sluggable_ryang 0.0.1
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_ryang.rb +48 -0
- metadata +43 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 4600da1328148484e10d0ea5a3d5b4e417650570
|
|
4
|
+
data.tar.gz: 67d59b265aa1de86a4e5cf20bc3b440fa30a8cd1
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 9c436c5e097deefd2ba3a00a92426d43c72746866afc8dfbd764559b6125abc73f8ef57629ad591b71701f4a4478a3dc29ae792f3b1d81ad6044f2fbe9d85e19
|
|
7
|
+
data.tar.gz: 0ecfacae7476b32f68878c0f61acc90f6d0c7454fd8ed3b0b18301217393ae858578dbd9f4847f70e2ac61cf878289f8a7f847b8b04dd867ac28a75c5f80d19b
|
|
@@ -0,0 +1,48 @@
|
|
|
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
|
+
the_slug = to_slug(self.send(self.class.slug_column))
|
|
11
|
+
object = self.class.find_by(slug: the_slug)
|
|
12
|
+
|
|
13
|
+
count = 2
|
|
14
|
+
while object && object != self
|
|
15
|
+
the_slug = append_suffix(the_slug, count)
|
|
16
|
+
object = self.class.find_by(slug: the_slug)
|
|
17
|
+
count += 1
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
self.slug = the_slug.downcase
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def append_suffix(orig_slug, count)
|
|
24
|
+
if orig_slug.split('-').last.to_i != 0
|
|
25
|
+
return orig_slug.split('-').slice(0...-1).join('-') + count.to_s
|
|
26
|
+
else
|
|
27
|
+
return orig_slug + '-' + count.to_s
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def to_slug(name)
|
|
32
|
+
str = name.strip
|
|
33
|
+
str.gsub!(/\s*[^A-Za-z0-9]\s*/, '-')
|
|
34
|
+
str.gsub!(/-+/, '-')
|
|
35
|
+
str.downcase
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def to_param
|
|
39
|
+
self.slug
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
module ClassMethods
|
|
43
|
+
def sluggable_column(column_name)
|
|
44
|
+
self.slug_column = column_name
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: sluggable_ryang
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Ryan George
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2015-04-26 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Use this gem to render slugs for objects in your database.
|
|
14
|
+
email: ryan.geo@gmail.com
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- lib/sluggable_ryang.rb
|
|
20
|
+
homepage: http://github.com/audiac
|
|
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: Slugging Gem
|
|
43
|
+
test_files: []
|