sluggify-this-js 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/sluggify-this-js.rb +63 -0
  2. metadata +46 -0
@@ -0,0 +1,63 @@
1
+ module SluggifyThisJs
2
+
3
+ #include module SluggifyThisJs in model
4
+ #after_validation :sluggify_this
5
+ #def sluggify_this
6
+ #generate_slug(Post, self.title)
7
+ #end
8
+
9
+ def self.included(base)
10
+ base.send(:inlcude, InstanceMethods)
11
+ base.extend ClassMethods
12
+ base.class_eval do
13
+ end
14
+ end
15
+
16
+ module InstanceMethods
17
+
18
+ def to_param
19
+ self.slug
20
+ end
21
+
22
+ def generate_slug(model, attribute) #Post, self.title
23
+ str = to_slug(attribute)
24
+ count = 2
25
+ obj = model.where(slug: str).first
26
+ while obj && obj != self
27
+ str = str + "-" + count.to_s
28
+ obj = model.where(slug: str).first
29
+ count += 1
30
+ end
31
+ self.slug = str.downcase
32
+ end
33
+
34
+
35
+ def to_slug(name)
36
+ #strip the string
37
+ ret = name.strip
38
+
39
+ #blow away apostrophes
40
+ ret.gsub! /['`]/,""
41
+
42
+ # @ --> at, and & --> and
43
+ ret.gsub! /\s*@\s*/, " at "
44
+ ret.gsub! /\s*&\s*/, " and "
45
+
46
+ #replace all non alphanumeric with dash
47
+ ret.gsub! /\s*[^A-Za-z0-9]\s*/, '-'
48
+
49
+ #convert double dashes to single
50
+ ret.gsub! /-+/,"-"
51
+
52
+ #strip off leading/trailing dash
53
+ ret.gsub! /\A[-\.]+|[-\.]+\z/,""
54
+
55
+ ret
56
+ end
57
+
58
+
59
+ module ClassMethods
60
+
61
+ end
62
+ end
63
+
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sluggify-this-js
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Josh Scanish
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-28 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: This gem helps you create url slugs
15
+ email:
16
+ - lib/sluggify-this-js.rb
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/sluggify-this-js.rb
22
+ homepage: http://github.com
23
+ licenses: []
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 1.8.23
43
+ signing_key:
44
+ specification_version: 3
45
+ summary: A slug gem
46
+ test_files: []