sluggerize 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.
- data/.gitignore +3 -0
- data/Gemfile +4 -0
- data/README.md +33 -0
- data/Rakefile +2 -0
- data/lib/sluggerize/version.rb +3 -0
- data/lib/sluggerize.rb +54 -0
- data/sluggerize.gemspec +21 -0
- metadata +74 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# Sluggerize
|
2
|
+
|
3
|
+
A simple plugin that automatically generates a slug for a model on create. It takes care of protecting the key, automatically generating it and making sure it is unique.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
sluggerize :source_column, [options]
|
8
|
+
|
9
|
+
### Source Column
|
10
|
+
|
11
|
+
If you don't provide a source column, it will default to looking for a "title" column.
|
12
|
+
|
13
|
+
### Options
|
14
|
+
|
15
|
+
* **as_params** [*False*] If true, this will be used as the id of the object when creating URLs and you will be able to Object.find(slug)
|
16
|
+
* **substitution_char** - [-] The character to use when replacing spaces and other unsupported characters
|
17
|
+
|
18
|
+
## Example
|
19
|
+
|
20
|
+
create_table "projects" do |t|
|
21
|
+
t.string "title"
|
22
|
+
t.string "slug"
|
23
|
+
end
|
24
|
+
|
25
|
+
class Project < ActiveRecord::Base
|
26
|
+
sluggerize
|
27
|
+
end
|
28
|
+
|
29
|
+
Project.create(:title => 'A Very Happy Project')
|
30
|
+
Project.first.slug
|
31
|
+
=> 'a-very-happy-project'
|
32
|
+
|
33
|
+
Copyright (c) 2011 Jeremy Hubert, released under the MIT license
|
data/Rakefile
ADDED
data/lib/sluggerize.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
module Sluggerize
|
2
|
+
def self.included(base)
|
3
|
+
base.extend ClassMethods
|
4
|
+
end
|
5
|
+
module ClassMethods
|
6
|
+
def sluggerize(attr=nil,options={})
|
7
|
+
class_inheritable_accessor :options
|
8
|
+
attr ||= :title
|
9
|
+
options[:as_param] ||= true
|
10
|
+
options[:substitution_char] ||= '-'
|
11
|
+
self.options = options
|
12
|
+
|
13
|
+
raise ArgumentError, "#{self.name} is missing source column" if columns_hash[attr.to_s].nil?
|
14
|
+
raise ArgumentError, "#{self.name} is missing required slug column" if columns_hash['slug'].nil?
|
15
|
+
|
16
|
+
before_validation_on_create :create_slug
|
17
|
+
|
18
|
+
validates_presence_of :slug
|
19
|
+
validates_uniqueness_of :slug, :allow_nil => (options[:as_param] ? true : false)
|
20
|
+
|
21
|
+
send :define_method, :column_to_slug, lambda { self.send(attr) }
|
22
|
+
|
23
|
+
class << self
|
24
|
+
def find(*args)
|
25
|
+
if self.options[:as_param] && args.first.is_a?(String)
|
26
|
+
find_by_slug(args)
|
27
|
+
else
|
28
|
+
super(*args)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
include InstanceMethods
|
34
|
+
end
|
35
|
+
end
|
36
|
+
module InstanceMethods
|
37
|
+
|
38
|
+
def to_param
|
39
|
+
options[:as_param] ? self.slug : self.id
|
40
|
+
end
|
41
|
+
|
42
|
+
protected
|
43
|
+
|
44
|
+
def create_slug
|
45
|
+
self.slug ||= clean("#{column_to_slug}")
|
46
|
+
end
|
47
|
+
|
48
|
+
def clean(string)
|
49
|
+
string.downcase.gsub(/[^\w\s\d\_\-]/,'').gsub(/\s\s+/,' ').gsub(/[^\w\d]/, options[:substitution_char])
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
ActiveRecord::Base.send(:include, Sluggerize)
|
data/sluggerize.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "sluggerize/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "sluggerize"
|
7
|
+
s.version = Sluggerize::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Jeremy Hubert"]
|
10
|
+
s.email = ["jhubert@gmail.com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{Simple library for creating a slug column from another string column}
|
13
|
+
s.description = %q{Creates a slug from the specified column of any model.}
|
14
|
+
|
15
|
+
s.rubyforge_project = "sluggerize"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sluggerize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Jeremy Hubert
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-03-08 00:00:00 -05:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Creates a slug from the specified column of any model.
|
23
|
+
email:
|
24
|
+
- jhubert@gmail.com
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- README.md
|
35
|
+
- Rakefile
|
36
|
+
- lib/sluggerize.rb
|
37
|
+
- lib/sluggerize/version.rb
|
38
|
+
- sluggerize.gemspec
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: ""
|
41
|
+
licenses: []
|
42
|
+
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
hash: 3
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
hash: 3
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
version: "0"
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project: sluggerize
|
69
|
+
rubygems_version: 1.4.2
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Simple library for creating a slug column from another string column
|
73
|
+
test_files: []
|
74
|
+
|