sluggable_aimee 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_aimee.rb +44 -0
- metadata +44 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 36709ff9f2b2a124dfc3ab1985051eae47ebaba0
|
|
4
|
+
data.tar.gz: 24944bdfd48466e1dd9d11829146d9a395567201
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 6885aefed8e9d9b6c99b777f3b58bb071f261e7f7bf262cc5a6e538cd4c40dec0db84f5dd09bc043642ffa5cc4b59d062cd30dd843e9853b99825d64dec8bcf1
|
|
7
|
+
data.tar.gz: 7606570db76b1c75079382f9f0257d4b6fed7ae51914e0a3186114435b7a765ef1f523f75a557ae21977d69406e4d651dd64efc497c0cfeba65f6d8ed6ef7685
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# Models this is included in require a :slug column
|
|
2
|
+
# and to specify a value for sluggable_column :value
|
|
3
|
+
module Sluggable
|
|
4
|
+
|
|
5
|
+
extend ActiveSupport::Concern
|
|
6
|
+
|
|
7
|
+
included do
|
|
8
|
+
before_save :generate_slug
|
|
9
|
+
class_attribute :slug_based_on
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def to_param
|
|
13
|
+
self.slug
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def generate_slug
|
|
17
|
+
slug_prefix = non_unique_slug
|
|
18
|
+
self.slug = unique_slug(slug_prefix)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def non_unique_slug
|
|
22
|
+
(self.send(slug_based_on)).strip.gsub(/\W/,'-').gsub(/-+/,'-').downcase
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def unique_slug(slug_prefix)
|
|
26
|
+
objects_with_similar_slugs = self.class.where("slug like ?", "#{slug_prefix}%") - [self]
|
|
27
|
+
if objects_with_similar_slugs.empty?
|
|
28
|
+
slug_prefix
|
|
29
|
+
else
|
|
30
|
+
ordered_slug_suffixes = objects_with_similar_slugs.map{|m| m.slug.split("#{slug_prefix}")[1].to_i.abs}.sort
|
|
31
|
+
new_suffix = ordered_slug_suffixes[-1].next
|
|
32
|
+
slug_prefix += "-#{new_suffix}"
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
module ClassMethods
|
|
37
|
+
|
|
38
|
+
def sluggable_column(column_name)
|
|
39
|
+
self.slug_based_on = column_name
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: sluggable_aimee
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Aimee
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2015-11-16 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Allows slugging from any model the model requires a :slug column and
|
|
14
|
+
you must specify sluggable_column :YOU_CHOOSE in the model
|
|
15
|
+
email:
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- lib/sluggable_aimee.rb
|
|
21
|
+
homepage:
|
|
22
|
+
licenses: []
|
|
23
|
+
metadata: {}
|
|
24
|
+
post_install_message:
|
|
25
|
+
rdoc_options: []
|
|
26
|
+
require_paths:
|
|
27
|
+
- lib
|
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0'
|
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
34
|
+
requirements:
|
|
35
|
+
- - ">="
|
|
36
|
+
- !ruby/object:Gem::Version
|
|
37
|
+
version: '0'
|
|
38
|
+
requirements: []
|
|
39
|
+
rubyforge_project:
|
|
40
|
+
rubygems_version: 2.4.4
|
|
41
|
+
signing_key:
|
|
42
|
+
specification_version: 4
|
|
43
|
+
summary: Slugging gem
|
|
44
|
+
test_files: []
|