sluggable_loc 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_loc.rb +49 -0
  3. metadata +43 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f528447b2b739b75af36c33695804c89079d6264
4
+ data.tar.gz: a54bfb5844a099cb0a554c1ca53f186630cf19e9
5
+ SHA512:
6
+ metadata.gz: 0336d9c4a25314f2bf381911f4eb6123ac507c6899d934f248760cea1c0e03b75e23cc1385135f9a8e676a6ba3459b9854b423efa572249b7eba4fc52594e198
7
+ data.tar.gz: 6f1c28e6ae604128bd646f804cba993ffa5f955c25ef20a22aa80739d71f37d004b62f22d3b4413a96fcec35b6814de6ee4be9698698cb1430c435a7de6c8d79
@@ -0,0 +1,49 @@
1
+ module Sluggable_Loc
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ before_save :generate_slug!
6
+ class_attribute :slug_column
7
+
8
+ end
9
+
10
+ def to_param
11
+ self.slug
12
+ end
13
+ def m
14
+ "ilove you"
15
+ end
16
+
17
+ def generate_slug!
18
+ the_slug = to_slug(self.send(slug_column))
19
+ obj = self.class.find_by slug: the_slug
20
+ count = 2
21
+ while obj && obj != self
22
+ the_slug = append_suffix(the_slug, count)
23
+ obj = self.class.find_by slug: the_slug
24
+ count += 1
25
+ end
26
+ self.slug = the_slug.downcase
27
+ end
28
+
29
+ def append_suffix(str, count)
30
+ if str.split('-').last.to_i != 0
31
+ return str.split('-').slice(0...-1).join('-') + "-" + count.to_s
32
+ else
33
+ return str + "-" + count.to_s
34
+ end
35
+ end
36
+
37
+ def to_slug(name)
38
+ str = name.strip
39
+ str.gsub! /\s*[^A-Za-z0-9]\s*/, '-'
40
+ str.gsub! /-+/,"-"
41
+ str.downcase
42
+ end
43
+
44
+ module ClassMethods
45
+ def sluggable_column(col_name)
46
+ self.slug_column = col_name
47
+ end
48
+ end
49
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sluggable_loc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Loc Ho
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-01 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: The best voting slug ever.
14
+ email: nguyenthasnhmai580@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/sluggable_loc.rb
20
+ homepage: https://ffff.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.4.6
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: A sluggable gem
43
+ test_files: []