sluggable_carl 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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/sluggable_carl.rb +49 -0
  3. metadata +43 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7829cca847815d2447434aadaa7a7f3f8b244234
4
+ data.tar.gz: b3f69e98e79b2b4245da72b9c5c777b81f33f892
5
+ SHA512:
6
+ metadata.gz: 06ffc8de38d36e8b23737f85f23dca4e6c202248f9e87f12f641beda46af8103f4521748cb5810f38c828e10006a9fa73d11ca1e8100952c54975e52196eada6
7
+ data.tar.gz: cc27b113a7c383ee6be4e3a4465a4e3886eda604112774ee7a2b17462895fa8bdc3eacc486fcce798677b64c23314628a0af0c4f364902188a34359e0ad6e084
@@ -0,0 +1,49 @@
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
+
10
+
11
+ def generate_slug!
12
+ the_slug = to_slug(self.send(self.class.slug_column.to_sym))
13
+ obj = self.class.find_by(slug: the_slug)
14
+ count = 2
15
+ while obj && obj != self
16
+ the_slug = append_suffix(the_slug, count)
17
+ obj = self.class.find_by slug: the_slug
18
+ count += 1
19
+ end
20
+ self.slug = the_slug.downcase
21
+ end
22
+
23
+ def append_suffix(str, count)
24
+ if str.split('-').last.to_i != 0
25
+ return str.split('-').slice(0...-1).join('-') + "-" + count.to_s
26
+ else
27
+ return str + "-" + count.to_s
28
+ end
29
+ end
30
+
31
+
32
+ def to_slug(name)
33
+ str = name.strip
34
+ str.gsub! /\s*[^A-Za-z0-9]\s*/, '-'
35
+ str.gsub! /-+/, "-"
36
+ str.downcase
37
+ end
38
+
39
+ def to_param
40
+ self.slug
41
+ end
42
+
43
+ module ClassMethods
44
+ def sluggable_column(column_name)
45
+ self.slug_column = column_name
46
+ end
47
+ end
48
+
49
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sluggable_carl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Carl Jenkins
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-09 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Slug generating gem tool
14
+ email: carljenkins@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/sluggable_carl.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.2.2
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: A slug generating gem
43
+ test_files: []