sluggable_jeff 0.1.0

This diff has not been reviewed by any users.
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 +15 -0
  2. data/lib/sluggable_jeff.rb +45 -0
  3. metadata +43 -0
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ YWI3OTAwMzE4OTJiN2RkZmY1M2IxYzk1OTA5MTlkYTE0M2QyN2FlZg==
5
+ data.tar.gz: !binary |-
6
+ N2E4N2U4ZDkxYTk5ZTEyYzhkY2ZiOTdkNGY1OTQ3MjBiNTEyNDM2Zg==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ OGVkOTZiMDE5N2U3ZjMzY2NhYmY2OWQ5NGJkZTgyZGE5ZmMzM2U3MTA2ZmJk
10
+ ZGNhYTMzNDAyZjQ4OGNhZDgzYWMxMDE4YTc4ODUwOTI0ZTBkYTg3ODgxYWEz
11
+ Mjc4YzI1OTQyYzhiODBiYTA5NjgxZWQ4ZTMyMjc3YTQ4NGU2Nzc=
12
+ data.tar.gz: !binary |-
13
+ NzlhNjBlNjAxYjI3M2RjOTcwZWYxNmJhNWUzNzI1ZDdlZjgyZDVhNzQ1M2Ni
14
+ ZmE3ZmM1Yzg5ODVmYWQyM2FmZDA4ODcyMmJlNzcxYjJjNjM3NDAzNWE3ZDQ3
15
+ YWNiYzNlNWQ2MDExYWVlNzIzMGI1Y2UyMjlhYmIxMzI4MTA2YTE=
@@ -0,0 +1,45 @@
1
+ module SluggableJeff
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
+ existingItem = self.class.find_by slug: the_slug
16
+ count = 2
17
+ while existingItem && existingItem != self
18
+ the_slug = append_suffix(the_slug, count)
19
+ existingItem = self.class.find_by slug: the_slug
20
+ count += 1
21
+ end
22
+ self.slug = the_slug.downcase
23
+ end
24
+
25
+ def append_suffix(str, count)
26
+ if str.split('-').last.to_i != 0
27
+ return str.split('-').slice(0...-1).join('-') + "-" + count.to_s
28
+ else
29
+ return str + "-" + count.to_s
30
+ end
31
+ end
32
+
33
+ def to_slug(name)
34
+ str = name.strip
35
+ str.gsub! /\s*[^A-Za-z0-9]\s*/, '-'
36
+ str.gsub! /-+/,"-"
37
+ str.downcase
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_jeff
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeff Scott
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A sample slugging gem.
14
+ email: test@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/sluggable_jeff.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.1
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: A slugging gem
43
+ test_files: []