permalink 1.0.0 → 1.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 +1 -0
- data/.rspec +1 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +81 -0
- data/README.markdown +1 -1
- data/Rakefile +4 -30
- data/lib/permalink.rb +6 -90
- data/lib/permalink/orm/active_record.rb +26 -0
- data/lib/permalink/orm/base.rb +77 -0
- data/lib/permalink/orm/mongo_mapper.rb +28 -0
- data/lib/permalink/orm/mongoid.rb +24 -0
- data/lib/permalink/version.rb +1 -1
- data/permalink.gemspec +28 -0
- data/spec/permalink/active_record_spec.rb +13 -0
- data/spec/permalink/mongo_mapper_spec.rb +13 -0
- data/spec/permalink/mongoid_spec.rb +15 -0
- data/spec/permalink/string_spec.rb +21 -0
- data/spec/spec_helper.rb +26 -0
- data/spec/support/article.rb +4 -0
- data/spec/support/page.rb +5 -0
- data/spec/support/post.rb +2 -0
- data/spec/support/schema.rb +5 -0
- data/spec/support/shared.rb +87 -0
- metadata +131 -36
- data/test/models/beer.rb +0 -3
- data/test/models/donut.rb +0 -3
- data/test/models/post.rb +0 -5
- data/test/models/user.rb +0 -3
- data/test/permalink_test.rb +0 -110
- data/test/schema.rb +0 -18
- data/test/test_helper.rb +0 -10
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
pkg
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour --format documentation
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
permalink (1.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
activemodel (3.0.5)
|
10
|
+
activesupport (= 3.0.5)
|
11
|
+
builder (~> 2.1.2)
|
12
|
+
i18n (~> 0.4)
|
13
|
+
activerecord (3.0.5)
|
14
|
+
activemodel (= 3.0.5)
|
15
|
+
activesupport (= 3.0.5)
|
16
|
+
arel (~> 2.0.2)
|
17
|
+
tzinfo (~> 0.3.23)
|
18
|
+
activesupport (3.0.5)
|
19
|
+
archive-tar-minitar (0.5.2)
|
20
|
+
arel (2.0.9)
|
21
|
+
bson (1.2.4)
|
22
|
+
bson_ext (1.2.4)
|
23
|
+
builder (2.1.2)
|
24
|
+
columnize (0.3.2)
|
25
|
+
diff-lcs (1.1.2)
|
26
|
+
i18n (0.5.0)
|
27
|
+
jnunemaker-validatable (1.8.4)
|
28
|
+
activesupport (>= 2.3.4)
|
29
|
+
linecache19 (0.5.11)
|
30
|
+
ruby_core_source (>= 0.1.4)
|
31
|
+
mongo (1.2.2)
|
32
|
+
bson (>= 1.2.2)
|
33
|
+
mongo_mapper (0.8.6)
|
34
|
+
activesupport (>= 2.3.4)
|
35
|
+
jnunemaker-validatable (~> 1.8.4)
|
36
|
+
plucky (~> 0.3.6)
|
37
|
+
mongoid (2.0.0.rc.7)
|
38
|
+
activemodel (~> 3.0)
|
39
|
+
mongo (~> 1.2)
|
40
|
+
tzinfo (~> 0.3.22)
|
41
|
+
will_paginate (~> 3.0.pre)
|
42
|
+
plucky (0.3.6)
|
43
|
+
mongo (~> 1.1)
|
44
|
+
rspec (2.5.0)
|
45
|
+
rspec-core (~> 2.5.0)
|
46
|
+
rspec-expectations (~> 2.5.0)
|
47
|
+
rspec-mocks (~> 2.5.0)
|
48
|
+
rspec-core (2.5.1)
|
49
|
+
rspec-expectations (2.5.0)
|
50
|
+
diff-lcs (~> 1.1.2)
|
51
|
+
rspec-mocks (2.5.0)
|
52
|
+
ruby-debug-base19 (0.11.24)
|
53
|
+
columnize (>= 0.3.1)
|
54
|
+
linecache19 (>= 0.5.11)
|
55
|
+
ruby_core_source (>= 0.1.4)
|
56
|
+
ruby-debug19 (0.11.6)
|
57
|
+
columnize (>= 0.3.1)
|
58
|
+
linecache19 (>= 0.5.11)
|
59
|
+
ruby-debug-base19 (>= 0.11.19)
|
60
|
+
ruby_core_source (0.1.4)
|
61
|
+
archive-tar-minitar (>= 0.5.2)
|
62
|
+
sqlite3 (1.3.3)
|
63
|
+
sqlite3-ruby (1.3.3)
|
64
|
+
sqlite3 (>= 1.3.3)
|
65
|
+
test-unit (2.2.0)
|
66
|
+
tzinfo (0.3.24)
|
67
|
+
will_paginate (3.0.pre2)
|
68
|
+
|
69
|
+
PLATFORMS
|
70
|
+
ruby
|
71
|
+
|
72
|
+
DEPENDENCIES
|
73
|
+
activerecord
|
74
|
+
bson_ext
|
75
|
+
mongo_mapper (~> 0.8.6)
|
76
|
+
mongoid (~> 2.0.0.rc.7)
|
77
|
+
permalink!
|
78
|
+
rspec (~> 2.5.0)
|
79
|
+
ruby-debug19
|
80
|
+
sqlite3-ruby
|
81
|
+
test-unit
|
data/README.markdown
CHANGED
@@ -51,7 +51,7 @@ to generate a permalink when `before_save` callback is evaluated and the instanc
|
|
51
51
|
|
52
52
|
## License
|
53
53
|
|
54
|
-
Copyright (c)
|
54
|
+
Copyright (c) 2011 Nando Vieira, released under the MIT license
|
55
55
|
|
56
56
|
Permission is hereby granted, free of charge, to any person obtaining
|
57
57
|
a copy of this software and associated documentation files (the
|
data/Rakefile
CHANGED
@@ -1,31 +1,5 @@
|
|
1
|
-
require "
|
2
|
-
|
1
|
+
require "bundler"
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
desc "Run tests"
|
8
|
-
Rake::TestTask.new(:test) do |t|
|
9
|
-
t.libs << "lib"
|
10
|
-
t.libs << "test"
|
11
|
-
t.pattern = "test/**/*_test.rb"
|
12
|
-
t.verbose = true
|
13
|
-
end
|
14
|
-
|
15
|
-
begin
|
16
|
-
require "jeweler"
|
17
|
-
|
18
|
-
JEWEL = Jeweler::Tasks.new do |gem|
|
19
|
-
gem.name = "permalink"
|
20
|
-
gem.email = "fnando.vieira@gmail.com"
|
21
|
-
gem.homepage = "http://github.com/fnando/permalink"
|
22
|
-
gem.authors = ["Nando Vieira"]
|
23
|
-
gem.version = Permalink::Version::STRING
|
24
|
-
gem.summary = "ActiveRecord plugin for automatically converting fields to permalinks."
|
25
|
-
gem.files = FileList["README.markdown", "{lib,test}/**/*", "Rakefile"]
|
26
|
-
end
|
27
|
-
|
28
|
-
Jeweler::GemcutterTasks.new
|
29
|
-
rescue LoadError => e
|
30
|
-
puts "[JEWELER] You can't build a gem until you install jeweler with `gem install jeweler`"
|
31
|
-
end
|
4
|
+
require "rspec/core/rake_task"
|
5
|
+
RSpec::Core::RakeTask.new
|
data/lib/permalink.rb
CHANGED
@@ -1,92 +1,8 @@
|
|
1
1
|
require "permalink/string_ext"
|
2
|
-
require "
|
2
|
+
require "permalink/orm/base"
|
3
|
+
require "permalink/orm/active_record"
|
4
|
+
require "permalink/orm/mongo_mapper"
|
5
|
+
require "permalink/orm/mongoid"
|
3
6
|
|
4
|
-
|
5
|
-
|
6
|
-
base.extend ClassMethods
|
7
|
-
|
8
|
-
class << base
|
9
|
-
attr_accessor :permalink_options
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
module ClassMethods
|
14
|
-
# permalink :title
|
15
|
-
# permalink :title, :to => :custom_permalink_field
|
16
|
-
# permalink :title, :to => :permalink, :to_param => [:id, :permalink]
|
17
|
-
# permalink :title, :unique => true
|
18
|
-
def permalink(from, options={})
|
19
|
-
options = {
|
20
|
-
:to => :permalink,
|
21
|
-
:to_param => [:id, :permalink],
|
22
|
-
:unique => false
|
23
|
-
}.merge(options)
|
24
|
-
|
25
|
-
self.permalink_options = {
|
26
|
-
:from_column_name => from,
|
27
|
-
:to_column_name => options[:to],
|
28
|
-
:to_param => [options[:to_param]].flatten,
|
29
|
-
:unique => options[:unique]
|
30
|
-
}
|
31
|
-
|
32
|
-
include InstanceMethods
|
33
|
-
|
34
|
-
before_validation :create_permalink
|
35
|
-
before_save :create_permalink
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
module InstanceMethods
|
40
|
-
def to_param
|
41
|
-
to_param_option = self.class.permalink_options[:to_param]
|
42
|
-
|
43
|
-
to_param_option.compact.collect do |name|
|
44
|
-
if respond_to?(name)
|
45
|
-
send(name).to_s
|
46
|
-
else
|
47
|
-
name.to_s
|
48
|
-
end
|
49
|
-
end.reject(&:blank?).join('-')
|
50
|
-
end
|
51
|
-
|
52
|
-
private
|
53
|
-
def next_available_permalink(_permalink)
|
54
|
-
the_permalink = _permalink
|
55
|
-
|
56
|
-
if self.class.permalink_options[:unique]
|
57
|
-
suffix = 2
|
58
|
-
|
59
|
-
while self.class.first(:conditions => {to_permalink_name => the_permalink}, :select => self.class.primary_key)
|
60
|
-
the_permalink = "#{_permalink}-#{suffix}"
|
61
|
-
suffix += 1
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
the_permalink
|
66
|
-
end
|
67
|
-
|
68
|
-
def from_permalink_name
|
69
|
-
self.class.permalink_options[:from_column_name]
|
70
|
-
end
|
71
|
-
|
72
|
-
def to_permalink_name
|
73
|
-
self.class.permalink_options[:to_column_name]
|
74
|
-
end
|
75
|
-
|
76
|
-
def from_permalink_value
|
77
|
-
read_attribute(from_permalink_name)
|
78
|
-
end
|
79
|
-
|
80
|
-
def to_permalink_value
|
81
|
-
read_attribute(to_permalink_name)
|
82
|
-
end
|
83
|
-
|
84
|
-
def create_permalink
|
85
|
-
unless from_permalink_value.blank? || !to_permalink_value.blank?
|
86
|
-
write_attribute(to_permalink_name, next_available_permalink(from_permalink_value.to_s.to_permalink))
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
ActiveRecord::Base.send(:include, Permalink)
|
7
|
+
ActiveRecord::Base.send(:include, Permalink::Orm::ActiveRecord)
|
8
|
+
Mongoid::Document::ClassMethods.send(:include, Permalink::Orm::Mongoid) if defined?(Mongoid)
|
@@ -0,0 +1,26 @@
|
|
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
|
@@ -0,0 +1,77 @@
|
|
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
|
+
})
|
11
|
+
|
12
|
+
self.permalink_options = {
|
13
|
+
:from_column_name => from,
|
14
|
+
:to_column_name => options[:to],
|
15
|
+
:to_param => [options[:to_param]].flatten,
|
16
|
+
:unique => options[:unique]
|
17
|
+
}
|
18
|
+
|
19
|
+
before_validation :create_permalink
|
20
|
+
before_save :create_permalink
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
module InstanceMethods
|
25
|
+
def to_param
|
26
|
+
to_param_option = self.class.permalink_options[:to_param]
|
27
|
+
|
28
|
+
to_param_option.compact.collect do |name|
|
29
|
+
if respond_to?(name)
|
30
|
+
send(name).to_s
|
31
|
+
else
|
32
|
+
name.to_s
|
33
|
+
end
|
34
|
+
end.reject(&:blank?).join("-")
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
def next_available_permalink(_permalink)
|
39
|
+
the_permalink = _permalink
|
40
|
+
|
41
|
+
if self.class.permalink_options[:unique]
|
42
|
+
suffix = 2
|
43
|
+
|
44
|
+
while self.class.where(to_permalink_name => the_permalink).first
|
45
|
+
the_permalink = "#{_permalink}-#{suffix}"
|
46
|
+
suffix += 1
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
the_permalink
|
51
|
+
end
|
52
|
+
|
53
|
+
def from_permalink_name
|
54
|
+
self.class.permalink_options[:from_column_name]
|
55
|
+
end
|
56
|
+
|
57
|
+
def to_permalink_name
|
58
|
+
self.class.permalink_options[:to_column_name]
|
59
|
+
end
|
60
|
+
|
61
|
+
def from_permalink_value
|
62
|
+
read_attribute(from_permalink_name)
|
63
|
+
end
|
64
|
+
|
65
|
+
def to_permalink_value
|
66
|
+
read_attribute(to_permalink_name)
|
67
|
+
end
|
68
|
+
|
69
|
+
def create_permalink
|
70
|
+
unless from_permalink_value.blank? || !to_permalink_value.blank?
|
71
|
+
write_attribute(to_permalink_name, next_available_permalink(from_permalink_value.to_s.to_permalink))
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,28 @@
|
|
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
|
@@ -0,0 +1,24 @@
|
|
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/version.rb
CHANGED
data/permalink.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "permalink/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "permalink"
|
7
|
+
s.version = Permalink::Version::STRING
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Nando Vieira"]
|
10
|
+
s.email = ["fnando.vieira@gmail.com"]
|
11
|
+
s.homepage = "http://rubygems.org/gems/permalink"
|
12
|
+
s.summary = "Generate permalink attributes on ActiveRecord"
|
13
|
+
s.description = s.summary
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_development_dependency "activerecord"
|
21
|
+
s.add_development_dependency "sqlite3-ruby"
|
22
|
+
s.add_development_dependency "test-unit"
|
23
|
+
s.add_development_dependency "rspec", "~> 2.5.0"
|
24
|
+
s.add_development_dependency "mongoid", "~> 2.0.0.rc.7"
|
25
|
+
s.add_development_dependency "mongo_mapper", "~> 0.8.6"
|
26
|
+
s.add_development_dependency "bson_ext"
|
27
|
+
s.add_development_dependency "ruby-debug19" if RUBY_VERSION >= "1.9"
|
28
|
+
end
|
@@ -0,0 +1,13 @@
|
|
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
|
@@ -0,0 +1,13 @@
|
|
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
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Permalink::Orm::Mongoid do
|
4
|
+
before { pending }
|
5
|
+
|
6
|
+
let(:model) { Article }
|
7
|
+
it_should_behave_like "orm"
|
8
|
+
|
9
|
+
it "should override to_param method" do
|
10
|
+
model.permalink :title
|
11
|
+
|
12
|
+
record = model.create(:title => "Some nice post")
|
13
|
+
record.to_param.should == "some-nice-post"
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,21 @@
|
|
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)" => 'don-t-repeat-yourself-dry',
|
13
|
+
"Text\nwith\nline\n\n\tbreaks" => 'text-with-line-breaks'
|
14
|
+
}
|
15
|
+
|
16
|
+
it "should create permalink using to_permalink" do
|
17
|
+
SAMPLES.each do |from, to|
|
18
|
+
from.to_permalink.should == to
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
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("localhost")
|
12
|
+
MongoMapper.database = "permalink"
|
13
|
+
|
14
|
+
# Setup Mongoid
|
15
|
+
# Mongoid.configure do |config|
|
16
|
+
# name = "permalink"
|
17
|
+
# host = "localhost"
|
18
|
+
# config.master = Mongo::Connection.new.db(name)
|
19
|
+
# config.persist_in_safe_mode = false
|
20
|
+
# end
|
21
|
+
|
22
|
+
load("support/schema.rb")
|
23
|
+
require "support/page"
|
24
|
+
require "support/shared"
|
25
|
+
require "support/post"
|
26
|
+
# require "support/article"
|
@@ -0,0 +1,87 @@
|
|
1
|
+
shared_examples_for "orm" do
|
2
|
+
before do
|
3
|
+
model.delete_all
|
4
|
+
model.permalink :title
|
5
|
+
end
|
6
|
+
|
7
|
+
it "should respond to options" do
|
8
|
+
model.should respond_to(:permalink_options)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should use default options" do
|
12
|
+
model.permalink :title
|
13
|
+
record = model.create(:title => "Some nice post")
|
14
|
+
record.permalink.should == "some-nice-post"
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should use custom attribute" do
|
18
|
+
model.permalink :title, :to => :slug
|
19
|
+
record = model.create(:title => "Some nice post")
|
20
|
+
record.slug.should == "some-nice-post"
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should set permalink before_save" do
|
24
|
+
record = model.new(:title => "Some nice post")
|
25
|
+
record.permalink.should be_nil
|
26
|
+
record.valid?
|
27
|
+
record.permalink.should == "some-nice-post"
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should create unique permalinks" do
|
31
|
+
model.permalink :title, :unique => true
|
32
|
+
|
33
|
+
record = model.create(:title => "Some nice post")
|
34
|
+
record.permalink.should == "some-nice-post"
|
35
|
+
|
36
|
+
record = model.create(:title => "Some nice post")
|
37
|
+
record.permalink.should == "some-nice-post-2"
|
38
|
+
|
39
|
+
record = model.create(:title => "Some nice post")
|
40
|
+
record.permalink.should == "some-nice-post-3"
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should return to_param for unique permalink" do
|
44
|
+
model.permalink :title, :to_param => :permalink, :unique => true
|
45
|
+
|
46
|
+
record = model.create(:title => "Ruby")
|
47
|
+
record.to_param.should == "ruby"
|
48
|
+
|
49
|
+
record = model.create(:title => "Ruby")
|
50
|
+
record.to_param.should == "ruby-2"
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should override to_param with custom fields" do
|
54
|
+
model.permalink :title, :to => :slug, :to_param => [:slug, :id, "page"]
|
55
|
+
|
56
|
+
record = model.create(:title => "Some nice post")
|
57
|
+
record.to_param.should == "some-nice-post-#{record.id}-page"
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should ignore blank attributes from to_param" do
|
61
|
+
model.permalink :title, :to_param => [:id, " ", nil, "\t", :permalink]
|
62
|
+
|
63
|
+
record = model.create(:title => "Some nice post")
|
64
|
+
record.reload
|
65
|
+
record.to_param.should == "#{record.id}-some-nice-post"
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should set permalink if permalink is blank" do
|
69
|
+
record = model.create(:title => "Some nice post", :permalink => " ")
|
70
|
+
record.permalink.should == "some-nice-post"
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should keep defined permalink" do
|
74
|
+
record = model.create(:title => "Some nice post", :permalink => "awesome-post")
|
75
|
+
record.permalink.should == "awesome-post"
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should create unique permalinks for number-ended titles" do
|
79
|
+
model.permalink :title, :unique => true
|
80
|
+
|
81
|
+
record = model.create(:title => "Rails 3")
|
82
|
+
record.permalink.should == "rails-3"
|
83
|
+
|
84
|
+
record = model.create(:title => "Rails 3")
|
85
|
+
record.permalink.should == "rails-3-2"
|
86
|
+
end
|
87
|
+
end
|
metadata
CHANGED
@@ -1,12 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: permalink
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
prerelease:
|
5
|
-
|
6
|
-
- 1
|
7
|
-
- 0
|
8
|
-
- 0
|
9
|
-
version: 1.0.0
|
4
|
+
prerelease:
|
5
|
+
version: 1.1.0
|
10
6
|
platform: ruby
|
11
7
|
authors:
|
12
8
|
- Nando Vieira
|
@@ -14,38 +10,138 @@ autorequire:
|
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
12
|
|
17
|
-
date:
|
13
|
+
date: 2011-03-11 00:00:00 -03:00
|
18
14
|
default_executable:
|
19
|
-
dependencies:
|
20
|
-
|
21
|
-
|
22
|
-
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: activerecord
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: "0"
|
25
|
+
type: :development
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sqlite3-ruby
|
29
|
+
prerelease: false
|
30
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: "0"
|
36
|
+
type: :development
|
37
|
+
version_requirements: *id002
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: test-unit
|
40
|
+
prerelease: false
|
41
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
type: :development
|
48
|
+
version_requirements: *id003
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: rspec
|
51
|
+
prerelease: false
|
52
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ~>
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: 2.5.0
|
58
|
+
type: :development
|
59
|
+
version_requirements: *id004
|
60
|
+
- !ruby/object:Gem::Dependency
|
61
|
+
name: mongoid
|
62
|
+
prerelease: false
|
63
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 2.0.0.rc.7
|
69
|
+
type: :development
|
70
|
+
version_requirements: *id005
|
71
|
+
- !ruby/object:Gem::Dependency
|
72
|
+
name: mongo_mapper
|
73
|
+
prerelease: false
|
74
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ~>
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: 0.8.6
|
80
|
+
type: :development
|
81
|
+
version_requirements: *id006
|
82
|
+
- !ruby/object:Gem::Dependency
|
83
|
+
name: bson_ext
|
84
|
+
prerelease: false
|
85
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
86
|
+
none: false
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: "0"
|
91
|
+
type: :development
|
92
|
+
version_requirements: *id007
|
93
|
+
- !ruby/object:Gem::Dependency
|
94
|
+
name: ruby-debug19
|
95
|
+
prerelease: false
|
96
|
+
requirement: &id008 !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: "0"
|
102
|
+
type: :development
|
103
|
+
version_requirements: *id008
|
104
|
+
description: Generate permalink attributes on ActiveRecord
|
105
|
+
email:
|
106
|
+
- fnando.vieira@gmail.com
|
23
107
|
executables: []
|
24
108
|
|
25
109
|
extensions: []
|
26
110
|
|
27
|
-
extra_rdoc_files:
|
28
|
-
|
111
|
+
extra_rdoc_files: []
|
112
|
+
|
29
113
|
files:
|
114
|
+
- .gitignore
|
115
|
+
- .rspec
|
116
|
+
- Gemfile
|
117
|
+
- Gemfile.lock
|
30
118
|
- README.markdown
|
31
119
|
- Rakefile
|
32
120
|
- lib/permalink.rb
|
121
|
+
- lib/permalink/orm/active_record.rb
|
122
|
+
- lib/permalink/orm/base.rb
|
123
|
+
- lib/permalink/orm/mongo_mapper.rb
|
124
|
+
- lib/permalink/orm/mongoid.rb
|
33
125
|
- lib/permalink/string_ext.rb
|
34
126
|
- lib/permalink/version.rb
|
35
|
-
-
|
36
|
-
-
|
37
|
-
-
|
38
|
-
-
|
39
|
-
-
|
40
|
-
-
|
41
|
-
-
|
127
|
+
- permalink.gemspec
|
128
|
+
- spec/permalink/active_record_spec.rb
|
129
|
+
- spec/permalink/mongo_mapper_spec.rb
|
130
|
+
- spec/permalink/mongoid_spec.rb
|
131
|
+
- spec/permalink/string_spec.rb
|
132
|
+
- spec/spec_helper.rb
|
133
|
+
- spec/support/article.rb
|
134
|
+
- spec/support/page.rb
|
135
|
+
- spec/support/post.rb
|
136
|
+
- spec/support/schema.rb
|
137
|
+
- spec/support/shared.rb
|
42
138
|
has_rdoc: true
|
43
|
-
homepage: http://
|
139
|
+
homepage: http://rubygems.org/gems/permalink
|
44
140
|
licenses: []
|
45
141
|
|
46
142
|
post_install_message:
|
47
|
-
rdoc_options:
|
48
|
-
|
143
|
+
rdoc_options: []
|
144
|
+
|
49
145
|
require_paths:
|
50
146
|
- lib
|
51
147
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -53,29 +149,28 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
53
149
|
requirements:
|
54
150
|
- - ">="
|
55
151
|
- !ruby/object:Gem::Version
|
56
|
-
segments:
|
57
|
-
- 0
|
58
152
|
version: "0"
|
59
153
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
154
|
none: false
|
61
155
|
requirements:
|
62
156
|
- - ">="
|
63
157
|
- !ruby/object:Gem::Version
|
64
|
-
segments:
|
65
|
-
- 0
|
66
158
|
version: "0"
|
67
159
|
requirements: []
|
68
160
|
|
69
161
|
rubyforge_project:
|
70
|
-
rubygems_version: 1.
|
162
|
+
rubygems_version: 1.6.0
|
71
163
|
signing_key:
|
72
164
|
specification_version: 3
|
73
|
-
summary:
|
165
|
+
summary: Generate permalink attributes on ActiveRecord
|
74
166
|
test_files:
|
75
|
-
-
|
76
|
-
-
|
77
|
-
-
|
78
|
-
-
|
79
|
-
-
|
80
|
-
-
|
81
|
-
-
|
167
|
+
- spec/permalink/active_record_spec.rb
|
168
|
+
- spec/permalink/mongo_mapper_spec.rb
|
169
|
+
- spec/permalink/mongoid_spec.rb
|
170
|
+
- spec/permalink/string_spec.rb
|
171
|
+
- spec/spec_helper.rb
|
172
|
+
- spec/support/article.rb
|
173
|
+
- spec/support/page.rb
|
174
|
+
- spec/support/post.rb
|
175
|
+
- spec/support/schema.rb
|
176
|
+
- spec/support/shared.rb
|
data/test/models/beer.rb
DELETED
data/test/models/donut.rb
DELETED
data/test/models/post.rb
DELETED
data/test/models/user.rb
DELETED
data/test/permalink_test.rb
DELETED
@@ -1,110 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
require "test_helper"
|
3
|
-
|
4
|
-
class PermalinkTest < ActiveSupport::TestCase
|
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)" => 'don-t-repeat-yourself-dry',
|
13
|
-
"Text\nwith\nline\n\n\tbreaks" => 'text-with-line-breaks'
|
14
|
-
}
|
15
|
-
|
16
|
-
test "should create permalink using to_permalink" do
|
17
|
-
SAMPLES.each do |from, to|
|
18
|
-
assert_equal to, from.to_permalink
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
test "should create permalink" do
|
23
|
-
beer = create_beer
|
24
|
-
assert_equal 'duff', beer.permalink
|
25
|
-
end
|
26
|
-
|
27
|
-
test "should create permalink for custom field" do
|
28
|
-
donut = create_donut
|
29
|
-
assert_equal 'cream', donut.slug
|
30
|
-
end
|
31
|
-
|
32
|
-
test "should add permalink before_save" do
|
33
|
-
beer = Beer.new
|
34
|
-
assert_nil beer.permalink
|
35
|
-
beer.update_attribute(:name, 'Duff Premium')
|
36
|
-
beer.reload
|
37
|
-
assert_equal 'duff-premium', beer.permalink
|
38
|
-
end
|
39
|
-
|
40
|
-
test "should create unique permalinks" do
|
41
|
-
post1 = create_post(:title => 'Ruby is a beautiful language')
|
42
|
-
assert_equal "ruby-is-a-beautiful-language", post1.permalink
|
43
|
-
|
44
|
-
post2 = create_post(:title => 'Ruby is a beautiful language')
|
45
|
-
assert_equal "ruby-is-a-beautiful-language-2", post2.permalink
|
46
|
-
|
47
|
-
post3 = create_post(:title => 'Ruby is a beautiful language')
|
48
|
-
assert_equal "ruby-is-a-beautiful-language-3", post3.permalink
|
49
|
-
end
|
50
|
-
|
51
|
-
test "return to_param for unique permalink" do
|
52
|
-
post1 = create_post(:title => 'Ruby')
|
53
|
-
assert_equal post1.to_param, 'ruby'
|
54
|
-
|
55
|
-
post2 = create_post(:title => 'Ruby')
|
56
|
-
assert_equal post2.to_param, 'ruby-2'
|
57
|
-
end
|
58
|
-
|
59
|
-
test "should override to_param method" do
|
60
|
-
beer = create_beer
|
61
|
-
assert_equal "#{beer.id}-#{beer.permalink}", beer.to_param
|
62
|
-
end
|
63
|
-
|
64
|
-
test "should override to_param with custom fields" do
|
65
|
-
donut = create_donut
|
66
|
-
assert_equal "#{donut.slug}-#{donut.id}-permalink", donut.to_param
|
67
|
-
end
|
68
|
-
|
69
|
-
test "should ignore blank attributes from to_param" do
|
70
|
-
user = create_user
|
71
|
-
assert_equal "1-john-doe", user.to_param
|
72
|
-
end
|
73
|
-
|
74
|
-
test "should set permalink if permalink is blank" do
|
75
|
-
user = create_user(:permalink => " ")
|
76
|
-
user.reload
|
77
|
-
assert_equal "john-doe", user.permalink
|
78
|
-
end
|
79
|
-
|
80
|
-
test "should keep defined permalink" do
|
81
|
-
user = create_beer(:permalink => "jdoe")
|
82
|
-
user.reload
|
83
|
-
assert_equal "jdoe", user.permalink
|
84
|
-
end
|
85
|
-
|
86
|
-
test "should create unique permalinks for number-ended titles" do
|
87
|
-
post1 = create_post(:title => "Rails 3")
|
88
|
-
assert_equal "rails-3", post1.permalink
|
89
|
-
|
90
|
-
post2 = create_post(:title => "Rails 3")
|
91
|
-
assert_equal "rails-3-2", post2.permalink
|
92
|
-
end
|
93
|
-
|
94
|
-
private
|
95
|
-
def create_beer(options={})
|
96
|
-
Beer.create({:name => 'Duff'}.merge(options))
|
97
|
-
end
|
98
|
-
|
99
|
-
def create_donut(options={})
|
100
|
-
Donut.create({:flavor => 'Cream'}.merge(options))
|
101
|
-
end
|
102
|
-
|
103
|
-
def create_user(options={})
|
104
|
-
User.create({:name => 'John Doe'}.merge(options))
|
105
|
-
end
|
106
|
-
|
107
|
-
def create_post(options={})
|
108
|
-
Post.create({:title => 'Some nice post!'}.merge(options))
|
109
|
-
end
|
110
|
-
end
|
data/test/schema.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
ActiveRecord::Schema.define(:version => 0) do
|
2
|
-
create_table :beers do |t|
|
3
|
-
t.string :name, :permalink
|
4
|
-
end
|
5
|
-
|
6
|
-
create_table :users do |t|
|
7
|
-
t.string :name, :permalink
|
8
|
-
end
|
9
|
-
|
10
|
-
create_table :donuts do |t|
|
11
|
-
t.string :flavor, :slug
|
12
|
-
end
|
13
|
-
|
14
|
-
create_table :posts do |t|
|
15
|
-
t.string :title
|
16
|
-
t.string :permalink, :unique => true
|
17
|
-
end
|
18
|
-
end
|
data/test/test_helper.rb
DELETED
@@ -1,10 +0,0 @@
|
|
1
|
-
require "rubygems"
|
2
|
-
gem "test-unit"
|
3
|
-
require "test/unit"
|
4
|
-
require "active_record"
|
5
|
-
require "active_support/test_case"
|
6
|
-
require "permalink"
|
7
|
-
|
8
|
-
Dir.glob(File.dirname(__FILE__) + "/models/*.rb").each {|r| require r }
|
9
|
-
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
10
|
-
load("schema.rb")
|