permalink 1.2.0 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/FUNDING.yml +3 -0
- data/.gitignore +3 -1
- data/.rubocop.yml +13 -0
- data/.travis.yml +7 -0
- data/Gemfile +3 -1
- data/README.md +118 -0
- data/Rakefile +14 -4
- data/lib/permalink.rb +38 -8
- data/lib/permalink/active_record.rb +127 -0
- data/lib/permalink/normalizations/contraction.rb +11 -0
- data/lib/permalink/normalizations/downcase.rb +11 -0
- data/lib/permalink/normalizations/leading_trailing_dashes.rb +11 -0
- data/lib/permalink/normalizations/multiple_dashes.rb +11 -0
- data/lib/permalink/normalizations/non_alphanumeric.rb +12 -0
- data/lib/permalink/normalizations/transliteration.rb +14 -0
- data/lib/permalink/version.rb +4 -2
- data/permalink.gemspec +15 -13
- data/test/permalink/active_record_test.rb +175 -0
- data/test/permalink/normalizations_test.rb +48 -0
- data/test/permalink/permalink_test.rb +22 -0
- data/test/support/post.rb +5 -0
- data/test/support/schema.rb +11 -0
- data/test/support/user.rb +5 -0
- data/test/test_helper.rb +17 -0
- metadata +94 -106
- data/.rspec +0 -1
- data/Gemfile.lock +0 -79
- data/README.markdown +0 -79
- data/lib/permalink/orm/active_record.rb +0 -26
- data/lib/permalink/orm/base.rb +0 -83
- data/lib/permalink/orm/mongo_mapper.rb +0 -28
- data/lib/permalink/orm/mongoid.rb +0 -24
- data/lib/permalink/string_ext.rb +0 -12
- data/spec/permalink/active_record_spec.rb +0 -13
- data/spec/permalink/mongo_mapper_spec.rb +0 -13
- data/spec/permalink/mongoid_spec.rb +0 -13
- data/spec/permalink/string_spec.rb +0 -23
- data/spec/spec_helper.rb +0 -24
- data/spec/support/article.rb +0 -4
- data/spec/support/page.rb +0 -5
- data/spec/support/post.rb +0 -2
- data/spec/support/schema.rb +0 -5
- data/spec/support/shared.rb +0 -95
data/.rspec
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
--colour --format documentation --profile
|
data/Gemfile.lock
DELETED
@@ -1,79 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
permalink (1.2.0)
|
5
|
-
|
6
|
-
GEM
|
7
|
-
remote: http://rubygems.org/
|
8
|
-
specs:
|
9
|
-
activemodel (3.1.1)
|
10
|
-
activesupport (= 3.1.1)
|
11
|
-
builder (~> 3.0.0)
|
12
|
-
i18n (~> 0.6)
|
13
|
-
activerecord (3.1.1)
|
14
|
-
activemodel (= 3.1.1)
|
15
|
-
activesupport (= 3.1.1)
|
16
|
-
arel (~> 2.2.1)
|
17
|
-
tzinfo (~> 0.3.29)
|
18
|
-
activesupport (3.1.1)
|
19
|
-
multi_json (~> 1.0)
|
20
|
-
arel (2.2.1)
|
21
|
-
bson (1.4.1)
|
22
|
-
bson_ext (1.4.1)
|
23
|
-
builder (3.0.0)
|
24
|
-
coderay (0.9.8)
|
25
|
-
diff-lcs (1.1.3)
|
26
|
-
i18n (0.6.0)
|
27
|
-
method_source (0.6.6)
|
28
|
-
ruby_parser (~> 2.0.5)
|
29
|
-
mongo (1.4.1)
|
30
|
-
bson (= 1.4.1)
|
31
|
-
mongo_mapper (0.9.2)
|
32
|
-
activemodel (~> 3.0)
|
33
|
-
activesupport (~> 3.0)
|
34
|
-
plucky (~> 0.3.8)
|
35
|
-
mongoid (2.3.2)
|
36
|
-
activemodel (~> 3.1)
|
37
|
-
mongo (~> 1.4)
|
38
|
-
tzinfo (~> 0.3.22)
|
39
|
-
multi_json (1.0.3)
|
40
|
-
plucky (0.3.8)
|
41
|
-
mongo (~> 1.3)
|
42
|
-
pry (0.9.6.2)
|
43
|
-
coderay (~> 0.9.8)
|
44
|
-
method_source (~> 0.6.5)
|
45
|
-
ruby_parser (~> 2.0.5)
|
46
|
-
slop (~> 2.1.0)
|
47
|
-
rake (0.9.2)
|
48
|
-
rspec (2.7.0)
|
49
|
-
rspec-core (~> 2.7.0)
|
50
|
-
rspec-expectations (~> 2.7.0)
|
51
|
-
rspec-mocks (~> 2.7.0)
|
52
|
-
rspec-core (2.7.1)
|
53
|
-
rspec-expectations (2.7.0)
|
54
|
-
diff-lcs (~> 1.1.2)
|
55
|
-
rspec-mocks (2.7.0)
|
56
|
-
ruby_parser (2.0.6)
|
57
|
-
sexp_processor (~> 3.0)
|
58
|
-
sexp_processor (3.0.7)
|
59
|
-
slop (2.1.0)
|
60
|
-
sqlite3 (1.3.4)
|
61
|
-
sqlite3-ruby (1.3.3)
|
62
|
-
sqlite3 (>= 1.3.3)
|
63
|
-
test-unit (2.4.0)
|
64
|
-
tzinfo (0.3.30)
|
65
|
-
|
66
|
-
PLATFORMS
|
67
|
-
ruby
|
68
|
-
|
69
|
-
DEPENDENCIES
|
70
|
-
activerecord
|
71
|
-
bson_ext
|
72
|
-
mongo_mapper (~> 0.9)
|
73
|
-
mongoid (~> 2.3)
|
74
|
-
permalink!
|
75
|
-
pry
|
76
|
-
rake
|
77
|
-
rspec (~> 2.7)
|
78
|
-
sqlite3-ruby
|
79
|
-
test-unit
|
data/README.markdown
DELETED
@@ -1,79 +0,0 @@
|
|
1
|
-
Permalink
|
2
|
-
=========
|
3
|
-
|
4
|
-
Instalation
|
5
|
-
-----------
|
6
|
-
|
7
|
-
gem install permalink
|
8
|
-
|
9
|
-
Usage
|
10
|
-
-----
|
11
|
-
|
12
|
-
Add the method call `permalink` to your model. Your model should have a `permalink` attribute.
|
13
|
-
|
14
|
-
class Page < ActiveRecord::Base
|
15
|
-
permalink :title
|
16
|
-
end
|
17
|
-
|
18
|
-
You can specify the permalink field:
|
19
|
-
|
20
|
-
class page < ActiveRecord::Base
|
21
|
-
permalink :title, :to => :title_permalink
|
22
|
-
end
|
23
|
-
|
24
|
-
If you don't want to use `permalink`, you can call `'some text'.to_permalink` string method and
|
25
|
-
manage the permalink process by yourself.
|
26
|
-
|
27
|
-
Permalinks are not unique by default. `permalink` overrides `to_param` as following:
|
28
|
-
|
29
|
-
def to_param
|
30
|
-
"#{id}-#{permalink}"
|
31
|
-
end
|
32
|
-
|
33
|
-
You can define the `to_param` format:
|
34
|
-
|
35
|
-
class Page < ActiveRecord::Base
|
36
|
-
permalink :title, :to_param => %w(id permalink page)
|
37
|
-
end
|
38
|
-
|
39
|
-
The above settings will generate something link `100-some-title-page`. By overriding `to_param` method you don't have to change a thing on your app routes.
|
40
|
-
|
41
|
-
If you want to generate unique permalink, use the `:unique` option:
|
42
|
-
|
43
|
-
class Page < ActiveRecord::Base
|
44
|
-
permalink :title, :unique => true, :to_param => :permalink
|
45
|
-
end
|
46
|
-
|
47
|
-
The permalink is generated using `ActiveSupport::Multibyte::Chars` class; this means that characters will properly replaced from `áéíó` to `aeio`, for instance.
|
48
|
-
|
49
|
-
The permalink is created when `before_validation` callback is evaluated. This plugin also tries
|
50
|
-
to generate a permalink when `before_save` callback is evaluated and the instance has no permalink set.
|
51
|
-
|
52
|
-
You can force the permalink generation by setting the `:force` option.
|
53
|
-
|
54
|
-
class Page < ActiveRecord::Base
|
55
|
-
permalink :title, :force => true
|
56
|
-
end
|
57
|
-
|
58
|
-
## License
|
59
|
-
|
60
|
-
Copyright (c) 2011 Nando Vieira, released under the MIT license
|
61
|
-
|
62
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
63
|
-
a copy of this software and associated documentation files (the
|
64
|
-
"Software"), to deal in the Software without restriction, including
|
65
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
66
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
67
|
-
permit persons to whom the Software is furnished to do so, subject to
|
68
|
-
the following conditions:
|
69
|
-
|
70
|
-
The above copyright notice and this permission notice shall be
|
71
|
-
included in all copies or substantial portions of the Software.
|
72
|
-
|
73
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
74
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
75
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
76
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
77
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
78
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
79
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -1,26 +0,0 @@
|
|
1
|
-
module Permalink
|
2
|
-
module Orm
|
3
|
-
module ActiveRecord
|
4
|
-
def self.included(base)
|
5
|
-
base.extend(ClassMethods)
|
6
|
-
base.extend(Permalink::Orm::Base::ClassMethods)
|
7
|
-
class << base; attr_accessor :permalink_options; end
|
8
|
-
end
|
9
|
-
|
10
|
-
module ClassMethods
|
11
|
-
# permalink :title
|
12
|
-
# permalink :title, :to => :custom_permalink_field
|
13
|
-
# permalink :title, :to => :permalink, :to_param => [:id, :permalink]
|
14
|
-
# permalink :title, :unique => true
|
15
|
-
def permalink(from, options={})
|
16
|
-
include Permalink::Orm::Base::InstanceMethods
|
17
|
-
setup_permalink(
|
18
|
-
{:to_param => [:id, :permalink]},
|
19
|
-
from,
|
20
|
-
options
|
21
|
-
)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
data/lib/permalink/orm/base.rb
DELETED
@@ -1,83 +0,0 @@
|
|
1
|
-
module Permalink
|
2
|
-
module Orm
|
3
|
-
module Base
|
4
|
-
module ClassMethods
|
5
|
-
def setup_permalink(orm_options, from, options)
|
6
|
-
options.reverse_merge!(orm_options)
|
7
|
-
options.reverse_merge!({
|
8
|
-
:to => :permalink,
|
9
|
-
:unique => false,
|
10
|
-
:force => false
|
11
|
-
})
|
12
|
-
|
13
|
-
self.permalink_options = {
|
14
|
-
:from_column_name => from,
|
15
|
-
:to_column_name => options[:to],
|
16
|
-
:to_param => [options[:to_param]].flatten,
|
17
|
-
:unique => options[:unique],
|
18
|
-
:force => options[:force]
|
19
|
-
}
|
20
|
-
|
21
|
-
before_validation :create_permalink
|
22
|
-
before_save :create_permalink
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
module InstanceMethods
|
27
|
-
def to_param
|
28
|
-
to_param_option = self.class.permalink_options[:to_param]
|
29
|
-
|
30
|
-
to_param_option.compact.collect do |name|
|
31
|
-
if respond_to?(name)
|
32
|
-
send(name).to_s
|
33
|
-
else
|
34
|
-
name.to_s
|
35
|
-
end
|
36
|
-
end.reject(&:blank?).join("-")
|
37
|
-
end
|
38
|
-
|
39
|
-
private
|
40
|
-
def next_available_permalink(_permalink)
|
41
|
-
the_permalink = _permalink
|
42
|
-
|
43
|
-
if self.class.permalink_options[:unique]
|
44
|
-
suffix = 2
|
45
|
-
|
46
|
-
while self.class.where(to_permalink_name => the_permalink).first
|
47
|
-
the_permalink = "#{_permalink}-#{suffix}"
|
48
|
-
suffix += 1
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
the_permalink
|
53
|
-
end
|
54
|
-
|
55
|
-
def from_permalink_name
|
56
|
-
self.class.permalink_options[:from_column_name]
|
57
|
-
end
|
58
|
-
|
59
|
-
def to_permalink_name
|
60
|
-
self.class.permalink_options[:to_column_name]
|
61
|
-
end
|
62
|
-
|
63
|
-
def from_permalink_value
|
64
|
-
read_attribute(from_permalink_name)
|
65
|
-
end
|
66
|
-
|
67
|
-
def to_permalink_value
|
68
|
-
read_attribute(to_permalink_name)
|
69
|
-
end
|
70
|
-
|
71
|
-
def update_permalink?
|
72
|
-
self.class.permalink_options[:force] || to_permalink_value.blank?
|
73
|
-
end
|
74
|
-
|
75
|
-
def create_permalink
|
76
|
-
if update_permalink?
|
77
|
-
write_attribute(to_permalink_name, next_available_permalink(from_permalink_value.to_s.to_permalink))
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
module Permalink
|
2
|
-
module Orm
|
3
|
-
module MongoMapper
|
4
|
-
def self.included(base)
|
5
|
-
base.extend(Permalink::Orm::Base::ClassMethods)
|
6
|
-
base.extend(ClassMethods)
|
7
|
-
class << base; attr_accessor :permalink_options; end
|
8
|
-
end
|
9
|
-
|
10
|
-
module ClassMethods
|
11
|
-
# permalink :title
|
12
|
-
# permalink :title, :to => :custom_permalink_field
|
13
|
-
# permalink :title, :to => :permalink, :to_param => [:id, :permalink]
|
14
|
-
# permalink :title, :unique => true
|
15
|
-
def permalink(from, options={})
|
16
|
-
include Permalink::Orm::Base::InstanceMethods
|
17
|
-
setup_permalink(
|
18
|
-
{:to_param => :permalink},
|
19
|
-
from,
|
20
|
-
options
|
21
|
-
)
|
22
|
-
|
23
|
-
key options[:to], String, :index => true
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
@@ -1,24 +0,0 @@
|
|
1
|
-
module Permalink
|
2
|
-
module Orm
|
3
|
-
module Mongoid
|
4
|
-
include Permalink::Orm::Base::ClassMethods
|
5
|
-
attr_accessor :permalink_options
|
6
|
-
|
7
|
-
# permalink :title
|
8
|
-
# permalink :title, :to => :custom_permalink_field
|
9
|
-
# permalink :title, :to => :permalink, :to_param => [:id, :permalink]
|
10
|
-
# permalink :title, :unique => true
|
11
|
-
def permalink(from, options={})
|
12
|
-
include Permalink::Orm::Base::InstanceMethods
|
13
|
-
setup_permalink(
|
14
|
-
{:to_param => :permalink},
|
15
|
-
from,
|
16
|
-
options
|
17
|
-
)
|
18
|
-
|
19
|
-
field options[:to]
|
20
|
-
key options[:to]
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
data/lib/permalink/string_ext.rb
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
class String
|
2
|
-
def to_permalink
|
3
|
-
str = ActiveSupport::Multibyte::Chars.new(self)
|
4
|
-
str = str.normalize(:kd).gsub(/[^\x00-\x7F]/,'').to_s
|
5
|
-
str.gsub!(/'/, "")
|
6
|
-
str.gsub!(/[^-\w\d]+/sim, "-")
|
7
|
-
str.gsub!(/-+/sm, "-")
|
8
|
-
str.gsub!(/^-?(.*?)-?$/, '\1')
|
9
|
-
str.downcase!
|
10
|
-
str
|
11
|
-
end
|
12
|
-
end
|
@@ -1,13 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe Permalink::Orm::ActiveRecord do
|
4
|
-
let(:model) { Post }
|
5
|
-
it_should_behave_like "orm"
|
6
|
-
|
7
|
-
it "should override to_param method" do
|
8
|
-
model.permalink :title
|
9
|
-
|
10
|
-
record = model.create(:title => "Some nice post")
|
11
|
-
record.to_param.should == "#{record.id}-some-nice-post"
|
12
|
-
end
|
13
|
-
end
|
@@ -1,13 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe Permalink::Orm::MongoMapper do
|
4
|
-
let(:model) { Page }
|
5
|
-
it_should_behave_like "orm"
|
6
|
-
|
7
|
-
it "should override to_param method" do
|
8
|
-
model.permalink :title
|
9
|
-
|
10
|
-
record = model.create(:title => "Some nice post")
|
11
|
-
record.to_param.should == "some-nice-post"
|
12
|
-
end
|
13
|
-
end
|
@@ -1,13 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe Permalink::Orm::Mongoid do
|
4
|
-
let(:model) { Article }
|
5
|
-
it_should_behave_like "orm"
|
6
|
-
|
7
|
-
it "should override to_param method" do
|
8
|
-
model.permalink :title
|
9
|
-
|
10
|
-
record = model.create(:title => "Some nice post")
|
11
|
-
record.to_param.should == "some-nice-post"
|
12
|
-
end
|
13
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
require "spec_helper"
|
3
|
-
|
4
|
-
describe "String#to_permalink" do
|
5
|
-
SAMPLES = {
|
6
|
-
'This IS a Tripped out title!!.!1 (well/ not really)' => 'this-is-a-tripped-out-title-1-well-not-really',
|
7
|
-
'////// meph1sto r0x ! \\\\\\' => 'meph1sto-r0x',
|
8
|
-
'āčēģīķļņū' => 'acegiklnu',
|
9
|
-
'中文測試 chinese text' => 'chinese-text',
|
10
|
-
'some-)()()-ExtRa!/// .data==?> to \/\/test' => 'some-extra-data-to-test',
|
11
|
-
'http://simplesideias.com.br/tags/' => 'http-simplesideias-com-br-tags',
|
12
|
-
"Don't Repeat Yourself (DRY)" => 'dont-repeat-yourself-dry',
|
13
|
-
"Text\nwith\nline\n\n\tbreaks" => 'text-with-line-breaks',
|
14
|
-
"can't do it" => "cant-do-it",
|
15
|
-
"i'm a dog" => "im-a-dog"
|
16
|
-
}
|
17
|
-
|
18
|
-
it "should create permalink using to_permalink" do
|
19
|
-
SAMPLES.each do |from, to|
|
20
|
-
from.to_permalink.should == to
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
data/spec/spec_helper.rb
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
require "mongo_mapper"
|
2
|
-
require "mongoid"
|
3
|
-
require "rspec"
|
4
|
-
require "active_record"
|
5
|
-
require "permalink"
|
6
|
-
|
7
|
-
# Setup ActiveRecord
|
8
|
-
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
9
|
-
|
10
|
-
# Setup MongoMapper
|
11
|
-
MongoMapper.connection = Mongo::Connection.new
|
12
|
-
MongoMapper.database = "permalink"
|
13
|
-
|
14
|
-
# Setup Mongoid
|
15
|
-
Mongoid.configure do |config|
|
16
|
-
config.master = Mongo::Connection.new.db("permalink")
|
17
|
-
config.persist_in_safe_mode = false
|
18
|
-
end
|
19
|
-
|
20
|
-
load("support/schema.rb")
|
21
|
-
require "support/page"
|
22
|
-
require "support/shared"
|
23
|
-
require "support/post"
|
24
|
-
require "support/article"
|
data/spec/support/article.rb
DELETED