has_unique_slug 0.1.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.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.md +37 -0
- data/Rakefile +1 -0
- data/has_unique_slug.gemspec +24 -0
- data/lib/has_unique_slug/version.rb +3 -0
- data/lib/has_unique_slug.rb +65 -0
- metadata +72 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# HasUniqueSlug
|
2
|
+
Generates a unique slug for use as a drop-in replacement for ids Ruby on Rails Active Record
|
3
|
+
|
4
|
+
## Install
|
5
|
+
|
6
|
+
Add `gem has_unique_slug` to your Gemfile and run `bundle install` to get it installed.
|
7
|
+
|
8
|
+
## Usage
|
9
|
+
|
10
|
+
Assume you have a Post model that has a title and slug column in which the slug column is generated from the post title.
|
11
|
+
|
12
|
+
class Post < ActiveRecord::Base
|
13
|
+
has_unique_slug
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
A unique slug will be generated automatically on creation.
|
18
|
+
If the generated slug is not unique, "-n" is appended onto the end. n starts at 2 and will increment by 1 until a unique slug is found.
|
19
|
+
If a slug is provided, one will not be generated, however the same rule applies as the above if the slug is not unique.
|
20
|
+
|
21
|
+
You can specify which column to use to generate the slug and which column to use to store the slug. Below is the default:
|
22
|
+
|
23
|
+
class Post < ActiveRecord::Base
|
24
|
+
has_unique_slug :title, :slug
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
Or if only 1 argument is given, use that column to generate the slug
|
29
|
+
|
30
|
+
class Post < ActiveRecord::Base
|
31
|
+
has_unique_slug :name # Uses the name column to generate the slug
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
## TODO:
|
36
|
+
|
37
|
+
Would like to write some tests.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "has_unique_slug/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "has_unique_slug"
|
7
|
+
s.version = HasUniqueSlug::VERSION
|
8
|
+
s.authors = ["Brendan Stennett"]
|
9
|
+
s.email = ["brendan@unknowncollective.com"]
|
10
|
+
s.homepage = "https://github.com/HuffMoody/has_unique_slug"
|
11
|
+
s.summary = "Generates a unique slug for use as a drop-in replacement for ids."
|
12
|
+
s.description = "Generates a unique slug for use as a drop-in replacement for ids."
|
13
|
+
|
14
|
+
s.rubyforge_project = "has_unique_slug"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
# s.add_development_dependency "rspec"
|
23
|
+
# s.add_runtime_dependency "rest-client"
|
24
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require "has_unique_slug/version"
|
2
|
+
|
3
|
+
module HasUniqueSlug
|
4
|
+
def self.included(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
|
10
|
+
def has_unique_slug(*args)
|
11
|
+
|
12
|
+
# Setup options Hash
|
13
|
+
options = {:generates_with => :title, :slug_column => :slug}
|
14
|
+
options.merge! args.pop if args.last.kind_of? Hash
|
15
|
+
|
16
|
+
# Standaridize implementation
|
17
|
+
title, slug = options[:generates_with], options[:slug_column]
|
18
|
+
|
19
|
+
# Add ActiveRecord Callback
|
20
|
+
before_create do |record|
|
21
|
+
|
22
|
+
# Add a slug if slug doesn't exist
|
23
|
+
if record[slug].blank?
|
24
|
+
record[slug] = record[title].parameterize
|
25
|
+
end
|
26
|
+
|
27
|
+
# Ensure the current slug is unique in the database, if not, make it unqiue
|
28
|
+
i = 2
|
29
|
+
while not record.class.where(slug => record[slug]).count.zero?
|
30
|
+
record[slug] = "#{record[slug]}-#{i}"
|
31
|
+
i += 1
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Add instance methods to objects using has_unique_slug
|
36
|
+
class_eval do
|
37
|
+
include HasUniqueSlug::InstanceMethods
|
38
|
+
end
|
39
|
+
|
40
|
+
# Add class methods to objects using has_unique_slug
|
41
|
+
instance_eval do
|
42
|
+
def find(*args)
|
43
|
+
if args.length == 1
|
44
|
+
where(slug => args.first).first
|
45
|
+
else
|
46
|
+
where(slug => args)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
module InstanceMethods
|
55
|
+
|
56
|
+
def to_param
|
57
|
+
slug
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
class ActiveRecord::Base
|
64
|
+
include HasUniqueSlug
|
65
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: has_unique_slug
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Brendan Stennett
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-10-12 00:00:00 Z
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Generates a unique slug for use as a drop-in replacement for ids.
|
22
|
+
email:
|
23
|
+
- brendan@unknowncollective.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- .gitignore
|
32
|
+
- Gemfile
|
33
|
+
- README.md
|
34
|
+
- Rakefile
|
35
|
+
- has_unique_slug.gemspec
|
36
|
+
- lib/has_unique_slug.rb
|
37
|
+
- lib/has_unique_slug/version.rb
|
38
|
+
homepage: https://github.com/HuffMoody/has_unique_slug
|
39
|
+
licenses: []
|
40
|
+
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
hash: 3
|
52
|
+
segments:
|
53
|
+
- 0
|
54
|
+
version: "0"
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
hash: 3
|
61
|
+
segments:
|
62
|
+
- 0
|
63
|
+
version: "0"
|
64
|
+
requirements: []
|
65
|
+
|
66
|
+
rubyforge_project: has_unique_slug
|
67
|
+
rubygems_version: 1.8.10
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: Generates a unique slug for use as a drop-in replacement for ids.
|
71
|
+
test_files: []
|
72
|
+
|