sluggable_cano 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_cano.rb +45 -0
  3. metadata +43 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ddf858a43a7cc46a3390fd03b25f110a4083b081
4
+ data.tar.gz: 990d046a189235bcdb54380a7399c5d2b54b81ca
5
+ SHA512:
6
+ metadata.gz: bbc2c5e49f0618f5b96c8f0c17d0a487421ac05e6dfa0359ebf0cf8ce8cb0227c80532c43b30785444877e5e79bf8685bbd7df96a6acd7f6f83f1346fe97cfbd
7
+ data.tar.gz: 486245a9a13dc6b104aa84794bfe785fca022178b39346acd26a984bf58930eaaa3474f743e50b7686e1c34b5a8431a3cf28108ae4b6051ecceb3449cfc72ebd
@@ -0,0 +1,45 @@
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.to_sym))
11
+ object = self.class.find_by(slug: the_slug)
12
+ count = 2
13
+ if object && object != self
14
+ the_slug = append_suffix(the_slug, count)
15
+ object = self.class.find_by(slug: the_slug)
16
+ count +=1
17
+ end
18
+ self.slug = the_slug.downcase
19
+ end
20
+
21
+ def append_suffix(text, count)
22
+ if text.split('-').last.to_i != 0
23
+ return text.split('-').slice(0...-1).join('-') + "-" + count.to_s
24
+ else
25
+ return text + "-" + count.to_s
26
+ end
27
+ end
28
+
29
+ def to_slug(name)
30
+ text = name.strip
31
+ text.gsub!(/\s*[^A-Za-z0-9]\s*/, '-')
32
+ text.gsub!(/-+/,'-')
33
+ text.downcase
34
+ end
35
+
36
+ def to_param
37
+ self.slug
38
+ end
39
+
40
+ module ClassMethods
41
+ def sluggable_column(column_name)
42
+ self.slug_column = column_name
43
+ end
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sluggable_cano
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Christopher Cano
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: My second gem, a slugging gem, for the PostIt app.
14
+ email: canoc4262@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/sluggable_cano.rb
20
+ homepage: http://github.com/canoc62
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.8
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: Tealeaf PostIt app- A slugging gem.
43
+ test_files: []