galetahub-salty_slugs 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.markdown +72 -0
- data/Rakefile +42 -0
- data/lib/salty_slugs.rb +8 -0
- data/lib/salty_slugs/active_record.rb +93 -0
- data/lib/salty_slugs/railtie.rb +13 -0
- data/lib/salty_slugs/transliteration.rb +60 -0
- data/lib/salty_slugs/utils.rb +31 -0
- data/lib/salty_slugs/version.rb +3 -0
- metadata +77 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 [name of plugin creator]
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
## SaltySlugs
|
2
|
+
|
3
|
+
Abstraction of word-based slugs for URLs, w/ or w/o leading numeric IDs.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
* Using Rails 3
|
8
|
+
|
9
|
+
<pre>
|
10
|
+
rails plugin install git://github.com/norbauer/salty_slugs.git
|
11
|
+
</pre>
|
12
|
+
|
13
|
+
|
14
|
+
* Using Rails 2.1+
|
15
|
+
|
16
|
+
<pre>
|
17
|
+
./script/plugin install git://github.com/norbauer/salty_slugs.git
|
18
|
+
</pre>
|
19
|
+
|
20
|
+
## Instructions
|
21
|
+
|
22
|
+
* SaltySlugs defaults to `title` as the `source_column`, `slug` as the `slug_column`, and prepends the model ID. Upon creating/updating a record, the plugin will sluggify the `source_column` when the `slug_column` is empty, otherwise it will sluggify the `slug_column` _unless_ the `slug_sync` option is set to true (defaults to false).
|
23
|
+
|
24
|
+
<pre>
|
25
|
+
class Post < ActiveRecord::Base
|
26
|
+
has_slug
|
27
|
+
end
|
28
|
+
|
29
|
+
post = Post.create(:title => "Do Not Mix Slugs and Salt!")
|
30
|
+
@post.to_param
|
31
|
+
=> '23-do-not-mix-slugs-and-salt'
|
32
|
+
</pre>
|
33
|
+
|
34
|
+
* You can also overwrite the defaults
|
35
|
+
|
36
|
+
<pre>
|
37
|
+
class Product < ActiveRecord::Base
|
38
|
+
has_slug :source_column => :name, :slug_column => :permalink, :prepend_id => false
|
39
|
+
end
|
40
|
+
|
41
|
+
@product = Product.create(:name => "Salt and Pepper Shaker")
|
42
|
+
@product.to_param
|
43
|
+
=> 'salt-and-pepper-shaker'
|
44
|
+
</pre>
|
45
|
+
|
46
|
+
* Use the `slugged_find` class method in your controllers, smart enough to modify the search conditions if prepending ID is found or not. `slugged_find` is capable of accepting standard `ActiveRecord::Base#find` options as a second parameter. If no records are found, `ActiveRecord::RecordNotFound` is raised to match behavior of `ActiveRecord::Base#find`.
|
47
|
+
|
48
|
+
<pre>
|
49
|
+
class PostsController < ApplicationController
|
50
|
+
|
51
|
+
def show
|
52
|
+
@post = Post.slugged_find(params[:id])
|
53
|
+
# or optionally with an eager-load
|
54
|
+
@post_with_author = Post.slugged_find(params[:id], :include => :author)
|
55
|
+
# catch exceptions if post is not found
|
56
|
+
rescue ActiveRecord::RecordNotFound
|
57
|
+
flash[:error] = "Post not found"
|
58
|
+
redirect_to :action => :index
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
</pre>
|
63
|
+
|
64
|
+
* If the `sync_slug` option is set to true, the `source_column` will _always_ be sluggified upon updating the record. This means that the slug will not be able to be manually edited, but will always be synchronized to the `source_column`.
|
65
|
+
|
66
|
+
## TODO
|
67
|
+
|
68
|
+
* Add a word/regexp blacklist, so that they are sliced out of a string when sluggified (for example to remove .com, .net, etc)
|
69
|
+
|
70
|
+
---
|
71
|
+
Copyright (c) 2008 Norbauer Inc, released under the MIT license
|
72
|
+
<br/>Written by Jonathan Dance and Jose Fernandez
|
data/Rakefile
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
require File.join(File.dirname(__FILE__), 'lib', 'salty_slugs', 'version')
|
5
|
+
|
6
|
+
desc 'Default: run unit tests.'
|
7
|
+
task :default => :test
|
8
|
+
|
9
|
+
desc 'Test the slug plugin.'
|
10
|
+
Rake::TestTask.new(:test) do |t|
|
11
|
+
t.libs << 'lib'
|
12
|
+
t.pattern = 'test/**/*_test.rb'
|
13
|
+
t.verbose = true
|
14
|
+
end
|
15
|
+
|
16
|
+
desc 'Generate documentation for the slug plugin.'
|
17
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
18
|
+
rdoc.rdoc_dir = 'rdoc'
|
19
|
+
rdoc.title = 'Slug'
|
20
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
21
|
+
rdoc.rdoc_files.include('README')
|
22
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
23
|
+
end
|
24
|
+
|
25
|
+
begin
|
26
|
+
require 'jeweler'
|
27
|
+
Jeweler::Tasks.new do |s|
|
28
|
+
s.name = "galetahub-salty_slugs"
|
29
|
+
s.version = SaltySlugs::VERSION
|
30
|
+
s.summary = "Generated slugs"
|
31
|
+
s.description = "Abstraction of word-based slugs for URLs, w/ or w/o leading numeric IDs."
|
32
|
+
s.email = "galeta.igor@gmail.com"
|
33
|
+
s.homepage = "https://github.com/galetahub/salty_slugs"
|
34
|
+
s.authors = ["Igor Galeta", "Pavlo Galeta"]
|
35
|
+
s.files = FileList["[A-Z]*", "lib/**/*"]
|
36
|
+
s.extra_rdoc_files = FileList["[A-Z]*"] - %w(Rakefile)
|
37
|
+
end
|
38
|
+
|
39
|
+
Jeweler::GemcutterTasks.new
|
40
|
+
rescue LoadError
|
41
|
+
puts "Jeweler not available. Install it with: gem install jeweler"
|
42
|
+
end
|
data/lib/salty_slugs.rb
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module SaltySlugs
|
3
|
+
module ActiveRecord
|
4
|
+
def self.included(base)
|
5
|
+
base.extend SlugMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module SlugMethods
|
9
|
+
# Configuration options are:
|
10
|
+
# :slug_column - column for save slug
|
11
|
+
# :source_column - source column for generate slug
|
12
|
+
# :prepend_id - method to_param return ':id-:slug' or ':slug'
|
13
|
+
# :sync_slug - regenerate slug on record update
|
14
|
+
# :scope - for validation
|
15
|
+
#
|
16
|
+
# Usage:
|
17
|
+
# class Post < ActiveRecord::Base
|
18
|
+
# has_slug :slug_column => 'permalink', :source_column => 'name'
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
def has_slug(options = {})
|
22
|
+
unless included_modules.include? InstanceMethods
|
23
|
+
extend ClassMethods
|
24
|
+
extend Columns
|
25
|
+
include InstanceMethods
|
26
|
+
include Columns
|
27
|
+
end
|
28
|
+
|
29
|
+
options = {
|
30
|
+
:slug_column => 'slug',
|
31
|
+
:source_column => 'title',
|
32
|
+
:prepend_id => true,
|
33
|
+
:sync_slug => false,
|
34
|
+
:scope => nil
|
35
|
+
}.merge(options)
|
36
|
+
|
37
|
+
if options[:scope].is_a?(Symbol) && options[:scope].to_s !~ /_id$/
|
38
|
+
options[:scope] = "#{options[:scope]}_id".intern
|
39
|
+
end
|
40
|
+
|
41
|
+
write_inheritable_attribute :salty_slugs_options, options
|
42
|
+
class_inheritable_reader :salty_slugs_options
|
43
|
+
|
44
|
+
unless slug_prepend_id
|
45
|
+
slug_scope_fields ? (validates_uniqueness_of slug_column, :scope => slug_scope_fields) : (validates_uniqueness_of slug_column)
|
46
|
+
end
|
47
|
+
|
48
|
+
before_validation { |record| record[slug_column] = (sync_slug || record[slug_column].blank?) ? SaltySlugs::Utils.sluggify(record.send(slug_source_column)) : SaltySlugs::Utils.sluggify(record[slug_column]) }
|
49
|
+
end
|
50
|
+
|
51
|
+
module ClassMethods
|
52
|
+
def slugged_find(value, options = {})
|
53
|
+
if slug_prepend_id && value.to_i != 0
|
54
|
+
find(value.to_i, options)
|
55
|
+
else
|
56
|
+
with_scope(:find => { :conditions => { slug_column => value } }) do
|
57
|
+
find(:first, options)
|
58
|
+
end or raise ::ActiveRecord::RecordNotFound
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
module Columns
|
64
|
+
def slug_column
|
65
|
+
salty_slugs_options[:slug_column]
|
66
|
+
end
|
67
|
+
|
68
|
+
def slug_source_column
|
69
|
+
salty_slugs_options[:source_column]
|
70
|
+
end
|
71
|
+
|
72
|
+
def slug_prepend_id
|
73
|
+
salty_slugs_options[:prepend_id]
|
74
|
+
end
|
75
|
+
|
76
|
+
def sync_slug
|
77
|
+
salty_slugs_options[:sync_slug]
|
78
|
+
end
|
79
|
+
|
80
|
+
def slug_scope_fields
|
81
|
+
salty_slugs_options[:scope]
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
module InstanceMethods
|
86
|
+
def to_param
|
87
|
+
return self.id.to_s if self[slug_column].blank?
|
88
|
+
slug_prepend_id ? "#{self.id}-#{self[slug_column]}" : self[slug_column]
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'rails'
|
3
|
+
require 'salty_slugs'
|
4
|
+
|
5
|
+
module SaltySlugs
|
6
|
+
class Railtie < Rails::Railtie
|
7
|
+
config.before_initialize do
|
8
|
+
ActiveSupport.on_load :active_record do
|
9
|
+
::ActiveRecord::Base.send(:include, SaltySlugs::ActiveRecord)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module SaltySlugs
|
4
|
+
# Russian transliteration
|
5
|
+
#
|
6
|
+
# Транслитерация для букв русского алфавита
|
7
|
+
module Transliteration
|
8
|
+
extend self
|
9
|
+
|
10
|
+
# Transliteration heavily based on rutils gem by Julian "julik" Tarkhanov and Co.
|
11
|
+
# <http://rutils.rubyforge.org/>
|
12
|
+
# Cleaned up and optimized.
|
13
|
+
|
14
|
+
LOWER = {
|
15
|
+
"і"=>"i","ґ"=>"g","ё"=>"yo","№"=>"#","є"=>"e",
|
16
|
+
"ї"=>"yi","а"=>"a","б"=>"b",
|
17
|
+
"в"=>"v","г"=>"g","д"=>"d","е"=>"e","ж"=>"zh",
|
18
|
+
"з"=>"z","и"=>"i","й"=>"y","к"=>"k","л"=>"l",
|
19
|
+
"м"=>"m","н"=>"n","о"=>"o","п"=>"p","р"=>"r",
|
20
|
+
"с"=>"s","т"=>"t","у"=>"u","ф"=>"f","х"=>"h",
|
21
|
+
"ц"=>"ts","ч"=>"ch","ш"=>"sh","щ"=>"sch","ъ"=>"'",
|
22
|
+
"ы"=>"y","ь"=>"","э"=>"e","ю"=>"yu","я"=>"ya"
|
23
|
+
}.freeze
|
24
|
+
|
25
|
+
UPPER = {
|
26
|
+
"Ґ"=>"G","Ё"=>"YO","Є"=>"E","Ї"=>"YI","І"=>"I",
|
27
|
+
"А"=>"A","Б"=>"B","В"=>"V","Г"=>"G",
|
28
|
+
"Д"=>"D","Е"=>"E","Ж"=>"ZH","З"=>"Z","И"=>"I",
|
29
|
+
"Й"=>"Y","К"=>"K","Л"=>"L","М"=>"M","Н"=>"N",
|
30
|
+
"О"=>"O","П"=>"P","Р"=>"R","С"=>"S","Т"=>"T",
|
31
|
+
"У"=>"U","Ф"=>"F","Х"=>"H","Ц"=>"TS","Ч"=>"CH",
|
32
|
+
"Ш"=>"SH","Щ"=>"SCH","Ъ"=>"'","Ы"=>"Y","Ь"=>"",
|
33
|
+
"Э"=>"E","Ю"=>"YU","Я"=>"YA",
|
34
|
+
}.freeze
|
35
|
+
|
36
|
+
# Transliterate a string with russian characters
|
37
|
+
#
|
38
|
+
# Возвращает строку, в которой все буквы русского алфавита заменены на похожую по звучанию латиницу
|
39
|
+
def transliterate(str)
|
40
|
+
chars = str.split(//)
|
41
|
+
|
42
|
+
result = ""
|
43
|
+
|
44
|
+
chars.each_with_index do |char, index|
|
45
|
+
if UPPER.has_key?(char) && LOWER.has_key?(chars[index+1])
|
46
|
+
# combined case
|
47
|
+
result << UPPER[char].downcase.capitalize
|
48
|
+
elsif UPPER.has_key?(char)
|
49
|
+
result << UPPER[char]
|
50
|
+
elsif LOWER.has_key?(char)
|
51
|
+
result << LOWER[char]
|
52
|
+
else
|
53
|
+
result << char
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
return result
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
begin
|
3
|
+
require "unicode"
|
4
|
+
rescue LoadError
|
5
|
+
require "iconv"
|
6
|
+
end
|
7
|
+
|
8
|
+
module SaltySlugs
|
9
|
+
module Utils
|
10
|
+
def self.sluggify(text)
|
11
|
+
return nil if text.blank?
|
12
|
+
|
13
|
+
if defined?(Transliteration)
|
14
|
+
str = Transliteration.transliterate(text)
|
15
|
+
str = str.gsub(/\W+/, '-').gsub(/^-+/,'').gsub(/-+$/,'').downcase
|
16
|
+
return str
|
17
|
+
elsif defined?(Unicode)
|
18
|
+
str = Unicode.normalize_KD(text).gsub(/[^\x00-\x7F]/n,'')
|
19
|
+
str = str.gsub(/\W+/, '-').gsub(/^-+/,'').gsub(/-+$/,'').downcase
|
20
|
+
return str
|
21
|
+
else
|
22
|
+
str = Iconv.iconv('ascii//translit//ignore', 'utf-8', text).to_s
|
23
|
+
str.gsub!(/\W+/, ' ')
|
24
|
+
str.strip!
|
25
|
+
str.downcase!
|
26
|
+
str.gsub!(/\ +/, '-')
|
27
|
+
return str
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: galetahub-salty_slugs
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 1.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Igor Galeta
|
14
|
+
- Pavlo Galeta
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2011-06-16 00:00:00 +03:00
|
20
|
+
default_executable:
|
21
|
+
dependencies: []
|
22
|
+
|
23
|
+
description: Abstraction of word-based slugs for URLs, w/ or w/o leading numeric IDs.
|
24
|
+
email: galeta.igor@gmail.com
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files:
|
30
|
+
- MIT-LICENSE
|
31
|
+
- README.markdown
|
32
|
+
files:
|
33
|
+
- MIT-LICENSE
|
34
|
+
- README.markdown
|
35
|
+
- Rakefile
|
36
|
+
- lib/salty_slugs.rb
|
37
|
+
- lib/salty_slugs/active_record.rb
|
38
|
+
- lib/salty_slugs/railtie.rb
|
39
|
+
- lib/salty_slugs/transliteration.rb
|
40
|
+
- lib/salty_slugs/utils.rb
|
41
|
+
- lib/salty_slugs/version.rb
|
42
|
+
has_rdoc: true
|
43
|
+
homepage: https://github.com/galetahub/salty_slugs
|
44
|
+
licenses: []
|
45
|
+
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
hash: 3
|
66
|
+
segments:
|
67
|
+
- 0
|
68
|
+
version: "0"
|
69
|
+
requirements: []
|
70
|
+
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 1.6.2
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: Generated slugs
|
76
|
+
test_files: []
|
77
|
+
|