sluggable_hunter 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_hunter.rb +57 -0
  2. metadata +45 -0
@@ -0,0 +1,57 @@
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 to_param
10
+ self.slug
11
+ end
12
+
13
+ def generate_slug!
14
+ # My solution before watching the solution video
15
+
16
+ # if self.class == Category
17
+ # the_slug = to_slug(self.name)
18
+ # elsif self.class == Post
19
+ # the_slug = to_slug(self.title)
20
+ # elsif self.class == User
21
+ # the_slug = to_slug(self.username)
22
+ # end
23
+
24
+ the_slug = to_slug(self.send(self.class.slug_column.to_sym))
25
+ obj = self.class.find_by slug: the_slug
26
+ count = 2
27
+
28
+ while obj && obj != self
29
+ the_slug = append_suffix(the_slug, count)
30
+ obj = self.class.find_by slug: the_slug
31
+ count += 1
32
+ end
33
+
34
+ self.slug = the_slug.downcase
35
+ end
36
+
37
+ def append_suffix(str, count)
38
+ if str.split('-').last.to_i != 0
39
+ return str.split('-').slice(0...-1).join('-') + "-" + count.to_s
40
+ else
41
+ return str + "-" + count.to_s
42
+ end
43
+ end
44
+
45
+ def to_slug(name)
46
+ str = name.strip
47
+ str.gsub! /\s*[^A-Za-z0-9]\s*/, '-'
48
+ str.gsub! /-+/, '-'
49
+ str.downcase
50
+ end
51
+
52
+ module ClassMethods
53
+ def sluggable_column(col_name)
54
+ self.slug_column = col_name
55
+ end
56
+ end
57
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sluggable_hunter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Hunter Ladd
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-09-10 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A slugging gem using regular expressions
15
+ email: dhunter.ladd@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/sluggable_hunter.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.28
42
+ signing_key:
43
+ specification_version: 3
44
+ summary: A slugging gem
45
+ test_files: []