sluggable_ts 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_ts.rb +45 -0
  3. metadata +43 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e2e174a6d1a9d8059afc183a96d14e7660869bec
4
+ data.tar.gz: 322e26c1fb0a9f643551413f841dddbe0276313a
5
+ SHA512:
6
+ metadata.gz: b8390e76897f12751ce2580e58385c45e5988bdda1e4b77cef4d75f63c181fd69631b18044b1eed056bbe61d1c72cfb051d80239089cb048342f0321c2031ba5
7
+ data.tar.gz: a752dad4870a8b3bd4a94da088371c87777912f216a498d789f7c2fafe234c5b3dea1563e17580028631b43f16137d2bd8e4e353b5b3eccca8985b7d904616e7
@@ -0,0 +1,45 @@
1
+ module Sluggable
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ after_validation :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 #check if the title/slug already exists
16
+ count = 2
17
+ while obj && obj != self #while obj exists && obj is not the one we're currently handling with
18
+ the_slug = append_suffix(the_slug, count) #add suffix(count/number) to the_slug
19
+ obj = self.class.find_by slug: the_slug #check again if the_slug is already taken
20
+ count += 1 #when it's ture(the_slug is taken), count accumulates
21
+ end
22
+ self.slug = the_slug.downcase
23
+ end
24
+
25
+ def append_suffix(str, count)
26
+ if str.split('-').last.to_i != 0 #if the last is not a string(0), but an integer, i.e. the "1" in Apple Pie 1"
27
+ return str.split('-').slice(0...-1).join('-') + "-" + count.to_s #remove the last string of "str.split('-'). And then join all the strings with '-', and add an count(number) at last.
28
+ else
29
+ return str + "-" + count.to_s
30
+ end
31
+ end
32
+
33
+ def to_slug(name)
34
+ str = name.strip #remove all the whitespace leading and trailing the string
35
+ str.gsub! /\s*[^A-Za-z0-9]\s*/, "-" # replace all symbols and numbers with "-"
36
+ str.gsub! /-+/, "-" #replace multiple "-", with only 1 "-"
37
+ str.downcase
38
+ end
39
+
40
+ module ClassMethods
41
+ def sluggable_column(col_name)
42
+ self.slug_column = col_name
43
+ end
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,43 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sluggable_ts
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Tsilly
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-30 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: For generating slugs.
14
+ email: jill.li87@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/sluggable_ts.rb
20
+ homepage: https://github.com/Tsilly
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.2.5
40
+ signing_key:
41
+ specification_version: 4
42
+ summary: A slug gem
43
+ test_files: []