sluggable-dtb 0.0.2

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-dtb.rb +48 -0
  3. metadata +43 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: bd060a0bec15fee40429d407f448cdf78d458523
4
+ data.tar.gz: 7f44a64df1d913f72cbdf5c32f9cb90d773fc0fc
5
+ SHA512:
6
+ metadata.gz: 9ecbba63e67e0532fe14b6c02243e8f2fd9ec042520defebfb9d93c0fb725acd3e7e98e4dab1a2eca9b818c950a0b6c65a9ea5fbb5bf89c4963642cefa3f7eba
7
+ data.tar.gz: ac7489a2dd996c13d6fb7118ea170f9611ca32f44efc1e00f460a9867555efaea0be1ecaf51898d45c1946d01cbd6a6b9db8beec40f51ca1b929895cbab46006
@@ -0,0 +1,48 @@
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))
15
+ obj = self.class.find_by slug: the_slug
16
+
17
+ count = 2
18
+
19
+ while obj && obj != self
20
+ the_slug = append_suffix(the_slug, count)
21
+ obj = self.class.find_by slug: the_slug
22
+ count += 1
23
+ end
24
+
25
+ self.slug = the_slug.downcase
26
+ end
27
+
28
+ def append_suffix(str, count)
29
+ if str.split('-').last.to_i != 0
30
+ return str.split('-').slice(0...-1).join('-') + "-" + count.to_s
31
+ else
32
+ return str + "-" + count.to_s
33
+ end
34
+ end
35
+
36
+ def to_slug(str)
37
+ str.strip.downcase.gsub(/\W/, "-")
38
+ .gsub(/-{2,}/, "-")
39
+ .gsub(/ {2,}/, " ")
40
+ .gsub(/^-|-$/, "")
41
+ end
42
+
43
+ module ClassMethods
44
+ def sluggable_column(col_name)
45
+ self.slug_column = col_name
46
+ end
47
+ end
48
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sluggable-dtb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Darren Burgess
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-07-05 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: ''
14
+ email: darrentburgess@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/sluggable-dtb.rb
20
+ homepage: https://github.com/darrenburgess/sluggable-dtb
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.0.14.1
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: Adds basic slugging for URLs
43
+ test_files: []