sluggeable_ric 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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/sluggeable_ric.rb +47 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3169d231604bded457f5f4c65f80d6172cad5771
4
+ data.tar.gz: 0d8d6d820d2c8f81f87cc173d1af534a1d8fa306
5
+ SHA512:
6
+ metadata.gz: 64407a392e1829b7cc757205cc97ecf5d182e8c5b6f5a53e2515a2a180b95920374fc1421394a9911be35694ae086a40d3d3a92a948d021d4845a3e5b67b5364
7
+ data.tar.gz: dfe0f5608fff68aa1dede1688e36a4824b1d054617852d748c5f460bd7600481767ca36ba0fc48e3021666a39dc0356b13742262c22080b3758f5295006be8e6
@@ -0,0 +1,47 @@
1
+ module SluggeableRic
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ after_validation :add_slug!
6
+ class_attribute :slug_col
7
+ end
8
+
9
+
10
+ def to_param
11
+ self.slug
12
+ end
13
+
14
+ def add_slug!
15
+ the_slug = to_slug(self.send(slug_col.to_sym))
16
+ post = self.class.find_by(slug: the_slug)
17
+ count = 2
18
+ while post && post != self
19
+ the_slug = append_suffix(the_slug, count)
20
+ post = self.class.find_by(slug: the_slug)
21
+ count += 1
22
+ end
23
+ self.slug = the_slug.downcase
24
+ end
25
+
26
+ def append_suffix(str, count)
27
+ if str.split("-").last.to_i != 0
28
+ return str.split("-").slice(0...-1).join("-") + "-" + count.to_s
29
+ else
30
+ return str + "-" + count.to_s
31
+ end
32
+
33
+ end
34
+
35
+ def to_slug(str)
36
+ working_string = str.strip
37
+ working_string.gsub! /\s*[^A-Za-z0-9]\s*/ , "-"
38
+ working_string.gsub! /-+/ , "-"
39
+ working_string.downcase
40
+ end
41
+
42
+ module ClassMethods
43
+ def slugged_column(column)
44
+ self.slug_col = column
45
+ end
46
+ end
47
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sluggeable_ric
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ricardo Rojo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-24 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple sluggeable gem to add slugs to models. Test gem, use under your
14
+ responsability
15
+ email: ricardorojo@netscape.net
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/sluggeable_ric.rb
21
+ homepage: http://rubygems.org/gems/sluggeable_ric
22
+ licenses: []
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.4.6
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Gem for implementing slug
44
+ test_files: []