sluggable_vanrails 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 (2) hide show
  1. data/lib/sluggable_vanrails.rb +46 -0
  2. metadata +45 -0
@@ -0,0 +1,46 @@
1
+ module Sluggable
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ after_validation :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
+ obj = self.class.find_by slug: the_slug
12
+ count = 2
13
+ while obj && obj != self
14
+ the_slug = append_suffix(the_slug, count)
15
+ obj = 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(str, count)
22
+ if str.split('-').last.to_i != 0
23
+ return str.split('-').slice(0...-1).join('-') + "-" + count.to_s
24
+ else
25
+ return str + "-" + count.to_s
26
+ end
27
+ end
28
+
29
+ def to_slug(name)
30
+ str = name.strip
31
+ str.gsub! /\s*[^A-Za-z0-9]\s*/, '-'
32
+ str.gsub! /-+/, "-"
33
+ str.downcase
34
+ end
35
+
36
+ def to_param
37
+ self.slug
38
+ end
39
+
40
+ module ClassMethods
41
+ def sluggable_column(col_name)
42
+ self.slug_column = col_name
43
+ end
44
+ end
45
+
46
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sluggable_vanrails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ruan van der Merwe
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-01-01 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: This gem allows the generation of url slugs
15
+ email: rv28@waikato.ac.nz
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/sluggable_vanrails.rb
21
+ homepage: http://github.com/
22
+ licenses: []
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 1.8.24
42
+ signing_key:
43
+ specification_version: 3
44
+ summary: A slug generating gem
45
+ test_files: []