sluggable_danyeboah 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.rb +52 -0
  3. metadata +48 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9211302606cd3e4b14719c8099c72a54b8cda656
4
+ data.tar.gz: aa89855fcb48c118118a9031b3de2ffd499b6a42
5
+ SHA512:
6
+ metadata.gz: 5c5118379777d17fbf0b5e401815bb46b94f8e3e6336da10f7820a84662cae0bab0b7f69953c0c9833ab382633b51bcde22ab3a6642acc08ff3b7a35167180c5
7
+ data.tar.gz: 081d10093bbcea152249899a0ebb225279e9c178829bf8ac8b6bd0a96c723c27289e9f0907924ccfa7a607f5ee9f85c69ccdd496f978cfbd0a1f0afae9493e7c
@@ -0,0 +1,52 @@
1
+ module Slug
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ before_save :generate_slug
6
+ class_attribute :slug_column
7
+ end
8
+
9
+ # generate slug for urls
10
+ def generate_slug
11
+ count = 2
12
+ slug = to_slug(self.send(self.class.slug_column.to_sym))
13
+ duplicate = self.class.find_by(slug: slug)
14
+
15
+ while duplicate && duplicate != self
16
+ slug = append_suffix(slug, count)
17
+ duplicate = self.class.find_by(slug: slug)
18
+ count += 1
19
+ end
20
+
21
+ self.slug = slug
22
+ end
23
+
24
+ # convert string to more friendly slug format
25
+ def to_slug(str)
26
+ str = str.strip
27
+ str.gsub!(/\W/, '-')
28
+ str.gsub!('-+', '-')
29
+ str
30
+ end
31
+
32
+ # deal with appending to duplicate slugs
33
+ def append_suffix(str, count)
34
+ str = str.split('-')
35
+ if str.last.to_i != 0
36
+ return str.slice(0...-1).join('-') + "-" + count.to_s
37
+ else
38
+ return str.join('-') + '-' + count.to_s
39
+ end
40
+ end
41
+
42
+ def to_param
43
+ self.slug
44
+ end
45
+
46
+ module ClassMethods
47
+ def sluggable_column(column)
48
+ self.slug_column = column
49
+ end
50
+ end
51
+
52
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sluggable_danyeboah
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Yeboah-Kordieh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-30 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |-
14
+ In order to use this to generate a slug:
15
+ 1. include it in your model
16
+ 2. call the generate_slug method in this module
17
+ 3. choose which column of the table will be used for making the slug using the class method sluggable_column :<column name>
18
+ email: danyeboah@gmail.com
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - lib/sluggable.rb
24
+ homepage: https://www.github.com/danyeboah
25
+ licenses: []
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 2.4.6
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: This generate slugs for database tables
47
+ test_files: []
48
+ has_rdoc: