sluggable-alex-test 0.0.0

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 +7 -0
  2. data/lib/sluggable-alex-test.rb +59 -0
  3. metadata +43 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4b027f51a31f07ec2f8019ddab0e7c42c81ad1e4
4
+ data.tar.gz: a181bc1a150590e591306f4eb03999c2d8430fe5
5
+ SHA512:
6
+ metadata.gz: 56223c132d32e2e7bb570825e15a5a3b2ac787cd42bacd2502c13e52fee52c88691825cc983146f94b8031e9a3a077dc0cfe400b9fba24f13688594010864b0a
7
+ data.tar.gz: 940fbad5042ce415881992532aa4cc1db2816d5366094c82d94b42b6acc3e96533e168ad09a170c7ad80f5137f1ff33d6a504f276fb58a1fc20e9abd18fc3508
@@ -0,0 +1,59 @@
1
+ module Sluggable
2
+
3
+ def self.included(base)
4
+ base.send(:include, InstanceExtend)
5
+ base.extend ClassExtend
6
+ base.class_eval do
7
+ auto_included
8
+ end
9
+ end
10
+
11
+ module InstanceExtend
12
+ def to_param
13
+ self.slug
14
+ end
15
+
16
+ def generate_slug
17
+ str = to_slug(self.sluggable)
18
+ count = 2
19
+ obj = Post.where(slug: str).first
20
+ while obj && obj != self
21
+ str_try = str + "-" + count.to_s
22
+ obj = Post.where(slug: str_try).first
23
+ count += 1
24
+ end
25
+ self.slug = str_try
26
+ end
27
+
28
+ def to_slug(name)
29
+ #strip the string
30
+ ret = name.strip
31
+
32
+ #blow away apostrophes
33
+ ret.gsub! /['`]/,""
34
+
35
+ # @ --> at, and & --> and
36
+ ret.gsub! /\s*@\s*/, " at "
37
+ ret.gsub! /\s*&\s*/, " and "
38
+
39
+ #replace all non alphanumeric with dash
40
+ ret.gsub! /\s*[^A-Za-z0-9]\s*/, '-'
41
+
42
+ #convert double dashes to single
43
+ ret.gsub! /-+/,"-"
44
+
45
+ #strip off leading/trailing dash
46
+ ret.gsub! /\A[-\.]+|[-\.]+\z/,""
47
+
48
+ ret.downcase
49
+ end
50
+ end
51
+
52
+ module ClassExtend
53
+ def auto_included
54
+ after_validation :generate_slug
55
+ end
56
+ end
57
+
58
+ end
59
+
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sluggable-alex-test
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Alex Chen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-01 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: The gem is built for test my gem building skill.
14
+ email:
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/sluggable-alex-test.rb
20
+ homepage:
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.0.6
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: A gem creation test.
43
+ test_files: []