sluggable_aero 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 (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/sluggable_aero.rb +47 -0
  3. metadata +43 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d17871d018121268b82e4ef16148cece4efa6336
4
+ data.tar.gz: 1622a89ff0f3bd96eb04380279faf501630a5efc
5
+ SHA512:
6
+ metadata.gz: f898562d7898c5509716578c155ffb5994aa3c4301ce6e3a260b6db639c97ad69b5520d3ce70c96713ab4831a3eeeb68ac72014c5bdd60689a7ef101d445051c
7
+ data.tar.gz: 6cfa32135f4ed656249a2b49a18711b0327bf5f3c0d3c32b9542b47ddb7d81934be610dad28488aa5e201b31d51f67140e72beebf53d73783fbc3cd87019f44f
@@ -0,0 +1,47 @@
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
+ the_slug = to_slug(self.send(self.class.slug_column.to_sym)) # doing such like: self.title, self.name, self.username
15
+ obj = self.class.find_by slug: the_slug
16
+ count = 2
17
+
18
+ while obj && obj != self
19
+ the_slug = append_suffix(the_slug, count)
20
+ obj = self.class.find_by slug: the_slug
21
+ count += 1
22
+ end
23
+
24
+ self.slug = the_slug.downcase
25
+ end
26
+
27
+ def append_suffix(str, count)
28
+ if str.split('-').last.to_i != 0 # google-2 or google-3 etc
29
+ return str.split('-').slice(0...-1).join('-') + "-" + count.to_s
30
+ else
31
+ return str + "-" + count.to_s
32
+ end
33
+ end
34
+
35
+ def to_slug(name)
36
+ str = name.strip
37
+ str.gsub! /\s*[^A-Za-z0-9]\s*/, '-'
38
+ str.gsub! /-+/, "-"
39
+ str.downcase
40
+ end
41
+
42
+ module ClassMethods
43
+ def sluggable_column(col_name)
44
+ self.slug_column = col_name
45
+ end
46
+ end
47
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sluggable_aero
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - aero
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A useful gem for secure urls.
14
+ email: zetaentranceserious@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/sluggable_aero.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.4.3
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: A slug gem
43
+ test_files: []