sluggable_ant 0.0.1
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_ant.rb +34 -0
- metadata +46 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0a97868218352167d7d6624b24b9ece4305a9038
|
4
|
+
data.tar.gz: f8caa96789422c4ab06d6a33e39c61ec2d341289
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ea1bd62a3c47715ad14aa38b59326670cad80378965b5bcacfec4c46ab355499367924195b5276ea6d43db0298054b886e7b4a4f3169ab3e2c65b367aedcc146
|
7
|
+
data.tar.gz: ddca71a8cde7ace69247c7c813c4ae58c3ed13a8acc2685b65edf40c7cf28b232d9b7bbf593165fbfcd745fff2b5c73e284e4e1156cb78d529314b8d9ffb2f0c
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Sluggable
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
class_attribute :slug_field
|
6
|
+
# adds an accessor to both class and instances,
|
7
|
+
# so both self.class.slug_field and self.slug_field will work
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
# ex. when_to_generate = { on: [:create, :update], if: ... }
|
12
|
+
# use the same parameters that before_save callback accepts
|
13
|
+
def has_slug(field, **when_to_generate)
|
14
|
+
self.slug_field = field
|
15
|
+
before_save :generate_slug!, when_to_generate
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_param
|
20
|
+
self.slug
|
21
|
+
end
|
22
|
+
|
23
|
+
def generate_slug!
|
24
|
+
slug = self.send(self.slug_field).downcase
|
25
|
+
.gsub(/\W|\_/, '-').gsub(/[\-]+/, '-')
|
26
|
+
.gsub(/^[\-]+/, '').gsub(/[\-]+$/, '')
|
27
|
+
i = 1
|
28
|
+
while !!self.class.where.not(id: self.id).find_by(slug: slug) do
|
29
|
+
i == 1 ? slug += '-1' : slug = slug.split('-')[0..-2].join('-') + "-#{i}"
|
30
|
+
i += 1
|
31
|
+
end
|
32
|
+
self.slug = slug
|
33
|
+
end
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sluggable_ant
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Anton Malkov
|
8
|
+
- Tealeaf Academy
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-11-21 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Sluggable gem created as part of Tealeaf Academy Course for the PostIt
|
15
|
+
app.
|
16
|
+
email: anton.malkov@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/sluggable_ant.rb
|
22
|
+
homepage: http://github.com/antonmal/sluggable_ant
|
23
|
+
licenses: []
|
24
|
+
metadata: {}
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 2.4.6
|
42
|
+
signing_key:
|
43
|
+
specification_version: 4
|
44
|
+
summary: Adds slug generation and routing to your models.
|
45
|
+
test_files: []
|
46
|
+
has_rdoc:
|