sluggable_jakekaad 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_jakekaad.rb +46 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 077e7a09f8902f5484709254a3f2dffd38500965
4
+ data.tar.gz: e7d3e1be3a1fc0be17c32de3ea14da21a8ecd78d
5
+ SHA512:
6
+ metadata.gz: 34236bfb56e4f551aa8d9833ee210a75eaae65915f3f4e38875299ced085e77f7071b881b92f6a24cacf8ad76913fb6e98aca9ee939b5f3ebf6aca3370f2dc1b
7
+ data.tar.gz: c71824452267fd4be88d79db152bbbb662103a15202c85d415b31abbc008763f4caf5053d48cb2971bb5556b54e5433b240994ec5b04153c3858dc2ec30a85bb
@@ -0,0 +1,46 @@
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 to_slug(name)
22
+ slug = name.strip
23
+ slug.gsub!(/\s*[^A-Za-z0-9]\s*/, '-')
24
+ slug.gsub!(/-+/, '-')
25
+ slug.downcase
26
+ end
27
+
28
+ def append_suffix(slug, count)
29
+ unless slug.split('-').last.to_i == 0
30
+ return slug.split('-').slice(0...-1).join('-') + ('-') + count.to_s
31
+ else
32
+ return slug + '-' + count.to_s
33
+ end
34
+ end
35
+
36
+ def to_param
37
+ self.slug
38
+ end
39
+
40
+ module ClassMethods
41
+ def sluggable_column(col_name)
42
+ self.slug_column = col_name
43
+ end
44
+ end
45
+
46
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sluggable_jakekaad
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jake Kaad
8
+ - Chris Lee
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-01-16 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Use this to create slugs to hide parameters in your urls.
15
+ email: kaadalac@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/sluggable_jakekaad.rb
21
+ homepage: http://github.com/jakekaad/sluggable_jakekaad
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.4.5
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Create 'smart' slugs for your ap
44
+ test_files: []