sluggable_mbz 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_mbz.rb +47 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c6991ee3d958df51c3b9eb57a2ef772e11058efb
4
+ data.tar.gz: c5cad41b5cbd3aada635797577a0a897b61bfaf3
5
+ SHA512:
6
+ metadata.gz: da79c58aa7580abaac31f4ef7a5e45030e18b517b9102a43876ab62b87d01e059e2786d23616f651f9dba266f6da3c353b3f6e13bc69279c3798fca6cdacb856
7
+ data.tar.gz: 4aac69612bd085b32cb8832a2e2c841cc8209d843702432fa29acddef8c031a4932bc68e0617f8002539b2fc60295cd417e90caa472c8ef3fc9b4d362c54cda5
@@ -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 generate_slug!
10
+ the_slug = to_slug(self.send(self.class.slug_column.to_sym))
11
+ obj = self.class.find_by slug: the_slug
12
+ count = 2
13
+ while obj && obj != self
14
+ the_slug = append_suffix(the_slug, count)
15
+ obj = self.class.find_by slug: the_slug
16
+ count += 1
17
+ end
18
+ self.slug = the_slug.downcase
19
+ end
20
+
21
+ def append_suffix(str, count)
22
+
23
+ if str.split('-').last.to_i !=0
24
+ return str.split('-').slice(0...-1).join('-') + "-" + count.to_s
25
+ else
26
+ return str + "-" + count.to_s
27
+ end
28
+ end
29
+
30
+ def to_slug(name)
31
+ str = name.strip
32
+ str.gsub! /\s*[^A-Za-z0-9]\s*/, '-'
33
+ str.gsub! /-+/, "-"
34
+ str.downcase
35
+ end
36
+
37
+ def to_param
38
+ self.slug
39
+ end
40
+
41
+ module ClassMethods
42
+ def sluggable_column(column_name)
43
+ self.slug_column = column_name
44
+ end
45
+ end
46
+
47
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sluggable_mbz
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Zac Clay
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-21 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: 'Generates slugs from "sluggable_column" taking out any offensive characters.
14
+ Really useful for "pretty urls" '
15
+ email: zacclay@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/sluggable_mbz.rb
21
+ homepage: http://zaccodes.com
22
+ licenses: []
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.2.1
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: A gem that allows you to give slugs to different models.
44
+ test_files: []