sluggable_tim_jan15 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.
- checksums.yaml +7 -0
- data/lib/sluggable.rb +35 -0
- metadata +46 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: b209148acad2c487d08eed128e8777a8b9f3299c
|
|
4
|
+
data.tar.gz: 1420191a7fb32fcb595b1ade58fba4b64c0a49ac
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: aaafecbade4b45c39a0d296ebc9e5c50278c2a71ca5be0e64df204d3607670c41f0194ae54b8fcd37c69424cf26e4167d44169d5909608a5ac112c5862283677
|
|
7
|
+
data.tar.gz: a049ba6fcfe7821789ab05c49bf87e59207ea2fe158e0948f4e914bb76d95150c07b00eaf995a850b41333e3590f126b409fff7b18a6908f910c50fc79c032ce
|
data/lib/sluggable.rb
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
module Sluggable
|
|
2
|
+
extend ActiveSupport::Concern
|
|
3
|
+
|
|
4
|
+
included do
|
|
5
|
+
before_create :add_slug
|
|
6
|
+
class_attribute :name_key
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def to_param
|
|
10
|
+
self.slug
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def slug_string(str)
|
|
14
|
+
str = str.gsub(/[^0-9a-z]/i,'-').downcase
|
|
15
|
+
str = str.gsub(/-+/, '-')
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def add_slug
|
|
19
|
+
temp_slug = slug_string(self.send(name_key))
|
|
20
|
+
count = self.class.all.map{|member| member.send(name_key)}.count{|title| slug_string(title) == temp_slug}
|
|
21
|
+
|
|
22
|
+
if count == 0
|
|
23
|
+
self.slug = temp_slug
|
|
24
|
+
else
|
|
25
|
+
self.slug = temp_slug + '-' + (count + 1).to_s
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
module ClassMethods
|
|
30
|
+
def sluggable_attribute(column)
|
|
31
|
+
self.name_key = column
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: sluggable_tim_jan15
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Tim
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2015-01-07 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: creates slugs based, accounting for duplicates. Needs to be added from
|
|
14
|
+
the very beginning to count correctly. Adds slug before creation, does not save
|
|
15
|
+
object. Object to be slugged need to have "slug" column, which needs to be described
|
|
16
|
+
in the model with "sluggable_attribute :column_name"
|
|
17
|
+
email: tim750233@gmail.com
|
|
18
|
+
executables: []
|
|
19
|
+
extensions: []
|
|
20
|
+
extra_rdoc_files: []
|
|
21
|
+
files:
|
|
22
|
+
- lib/sluggable.rb
|
|
23
|
+
homepage: http://github.com
|
|
24
|
+
licenses: []
|
|
25
|
+
metadata: {}
|
|
26
|
+
post_install_message:
|
|
27
|
+
rdoc_options: []
|
|
28
|
+
require_paths:
|
|
29
|
+
- lib
|
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
31
|
+
requirements:
|
|
32
|
+
- - ">="
|
|
33
|
+
- !ruby/object:Gem::Version
|
|
34
|
+
version: '0'
|
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '0'
|
|
40
|
+
requirements: []
|
|
41
|
+
rubyforge_project:
|
|
42
|
+
rubygems_version: 2.2.2
|
|
43
|
+
signing_key:
|
|
44
|
+
specification_version: 4
|
|
45
|
+
summary: a slugging gem
|
|
46
|
+
test_files: []
|