sluggable_tkb 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_tkb.rb +52 -0
- metadata +43 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9ae3c1566b57ee063228005bf2cf9c2a8e430403
|
4
|
+
data.tar.gz: 8b7213b353c07c33ce89b4b776ce374275922a1d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: df62e12d88621dae0594da726013dedf7807483f5785af4341e9a8dd437d4582818dbcedf46966e2dd5f7a5c402b7304d411cf1dba2f452626e5df771eaecc82
|
7
|
+
data.tar.gz: ead157ad68357e2b3a88e0071d2fed2ce1116c328ebef022b0329d9f97f95a125e544caed08304bcda137f0f3fe33356984ee990e1f08c00363b0b487704bb10
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Sluggable
|
2
|
+
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
before_save :generate_slug!
|
7
|
+
#on aurait pu utliser before_create comme ça le slug aurait ete generré juste une fois afin de proteger les
|
8
|
+
#bookmarks des user, en utilisant before_save le slug va s'updaté a achaque fois le post change
|
9
|
+
class_attribute :slug_column
|
10
|
+
end
|
11
|
+
|
12
|
+
def generate_slug!
|
13
|
+
the_slug = to_slug(self.send(self.slug_column.to_sym)) #on utilise le col_name (self.title pour post par exemple)
|
14
|
+
obj = self.class.find_by slug: the_slug #self.class Remplace Post ou Category, etc. obj remplace post.
|
15
|
+
count = 2
|
16
|
+
while obj && obj != self
|
17
|
+
the_slug = append_suffix(the_slug, count)
|
18
|
+
obj = self.class.find_by slug: the_slug
|
19
|
+
count += 1
|
20
|
+
end
|
21
|
+
self.slug = the_slug.downcase
|
22
|
+
end
|
23
|
+
|
24
|
+
def append_suffix(str, count)
|
25
|
+
if str.split('-').last.to_i != 0
|
26
|
+
return str.split('-').slice(0...-1).join('-') + "-" + count.to_s
|
27
|
+
else
|
28
|
+
return str + "-" + count.to_s
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
def to_slug(name)
|
34
|
+
str = name.strip #retire les espaces début et fin
|
35
|
+
str.gsub! /\s*[^A-Za-z0-9]\s*/, '-'
|
36
|
+
str.gsub! /-+/, '-'
|
37
|
+
str.downcase #return the string car gsub va retourner nil si rien ne correspond au resex
|
38
|
+
end
|
39
|
+
|
40
|
+
def to_param
|
41
|
+
self.slug
|
42
|
+
end
|
43
|
+
|
44
|
+
module ClassMethods
|
45
|
+
|
46
|
+
def sluggable_column(col_name)
|
47
|
+
self.slug_column = col_name
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
metadata
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sluggable_tkb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- TKB Montreal
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-10-23 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Generate sluggable string
|
14
|
+
email: tkb@montreal.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/sluggable_tkb.rb
|
20
|
+
homepage: http://github.com/soon
|
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: A Slug Gem
|
43
|
+
test_files: []
|