heyamate_slug 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/heyamate_slug.rb +45 -0
  3. metadata +43 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4525b8bbf243bca98307730877396c4816727d53
4
+ data.tar.gz: c454ff196c3766f4c91bdd3a1a97e3fb6e0fcbb3
5
+ SHA512:
6
+ metadata.gz: 712c17b66dc38b7a52449c5128f24c5bf7f1508d273f8aec2775ad57a81361bb4308e7b6a961dbf3007859a8fab36e25538f289e740f68bb6e14075f01dfbc57
7
+ data.tar.gz: e68e448b30411cc19b14635c629fcaaeb38a521937e1d3e6568c4608c32cd710837ee7a0c0d076d483b89c0178961143f1748494e69dfc14053fae51eca127de
@@ -0,0 +1,45 @@
1
+ module Slug
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
+ the_slug = to_slug(self.send(self.class.slug_column.to_sym))
15
+ obj = self.class.find_by slug: the_slug
16
+ count = 2
17
+ while obj && obj != self do
18
+ the_slug = append_suffix(the_slug, count)
19
+ obj = self.class.find_by slug: the_slug
20
+ count += 1
21
+ end
22
+ self.slug = the_slug
23
+ end
24
+
25
+ def append_suffix(str, count)
26
+ if str.split("-").last.to_i !=0
27
+ str.split("-").slice(0...-1).join("-") + "-" + count.to_s
28
+ else
29
+ str + "-" + count.to_s
30
+ end
31
+ end
32
+
33
+ def to_slug(name)
34
+ str = name.strip.downcase
35
+ str.gsub! /\W/, "-"
36
+ str.gsub! /-+/, "-"
37
+ str
38
+ end
39
+
40
+ module ClassMethods
41
+ def slugable_column(col_name)
42
+ self.slug_column = col_name
43
+ end
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: heyamate_slug
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Hankinson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-22 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Gem created in the tealeaf course
14
+ email: nathan.hankinson@uwa.edu.au
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/heyamate_slug.rb
20
+ homepage: http://geminfo.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.0.3
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: Gem that will create slugs
43
+ test_files: []