permalink 1.2.1 → 1.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +1 -1
- data/Gemfile +1 -1
- data/Gemfile.lock +54 -62
- data/{README.markdown → README.md} +34 -25
- data/lib/permalink.rb +3 -6
- data/lib/permalink/active_record.rb +91 -0
- data/lib/permalink/version.rb +1 -1
- data/permalink.gemspec +3 -9
- data/spec/permalink/active_record_spec.rb +118 -4
- data/spec/permalink/string_spec.rb +4 -6
- data/spec/spec_helper.rb +3 -19
- metadata +39 -89
- 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/spec/permalink/mongo_mapper_spec.rb +0 -13
- data/spec/permalink/mongoid_spec.rb +0 -13
- data/spec/support/article.rb +0 -4
- data/spec/support/page.rb +0 -5
- data/spec/support/shared.rb +0 -117
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e6134036196c8dacbfa748c16d8f492810708376
|
4
|
+
data.tar.gz: 66baea744850d6cdece44c322a459dcdff13ce5c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4692d97753d72d8b6ffc8045a121977ec801d91ccd72f08cb3e0fe85c4bd1e4814aed1df77993e2e09e5b46a3677e8950268800b6c586c49b17979f043dc77d5
|
7
|
+
data.tar.gz: 7056c29ddbd87866bfbcf2516bb4fd7e73ba262d76925c90c3a6a70e279165635d6ee57a5663e8139a50d55ede00e091e2563cfd88d5e58a30b7e44a22b7251e
|
data/.rspec
CHANGED
@@ -1 +1 @@
|
|
1
|
-
--colour
|
1
|
+
--colour
|
data/Gemfile
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
source
|
1
|
+
source "https://rubygems.org"
|
2
2
|
gemspec
|
data/Gemfile.lock
CHANGED
@@ -1,79 +1,71 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
permalink (1.2.
|
4
|
+
permalink (1.2.2)
|
5
5
|
|
6
6
|
GEM
|
7
|
-
remote:
|
7
|
+
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
|
-
activemodel (
|
10
|
-
activesupport (=
|
11
|
-
builder (~> 3.
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
pry (0.
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
rspec-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
rspec-
|
54
|
-
|
55
|
-
|
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)
|
9
|
+
activemodel (4.1.1)
|
10
|
+
activesupport (= 4.1.1)
|
11
|
+
builder (~> 3.1)
|
12
|
+
activerecord (4.1.1)
|
13
|
+
activemodel (= 4.1.1)
|
14
|
+
activesupport (= 4.1.1)
|
15
|
+
arel (~> 5.0.0)
|
16
|
+
activesupport (4.1.1)
|
17
|
+
i18n (~> 0.6, >= 0.6.9)
|
18
|
+
json (~> 1.7, >= 1.7.7)
|
19
|
+
minitest (~> 5.1)
|
20
|
+
thread_safe (~> 0.1)
|
21
|
+
tzinfo (~> 1.1)
|
22
|
+
arel (5.0.1.20140414130214)
|
23
|
+
awesome_print (1.2.0)
|
24
|
+
builder (3.2.2)
|
25
|
+
coderay (1.1.0)
|
26
|
+
diff-lcs (1.2.5)
|
27
|
+
i18n (0.6.9)
|
28
|
+
json (1.8.1)
|
29
|
+
method_source (0.8.2)
|
30
|
+
minitest (5.3.3)
|
31
|
+
pry (0.9.12.6)
|
32
|
+
coderay (~> 1.0)
|
33
|
+
method_source (~> 0.8)
|
34
|
+
slop (~> 3.4)
|
35
|
+
pry-meta (0.0.7)
|
36
|
+
awesome_print
|
37
|
+
pry
|
38
|
+
pry-nav
|
39
|
+
pry-remote
|
40
|
+
pry-nav (0.2.3)
|
41
|
+
pry (~> 0.9.10)
|
42
|
+
pry-remote (0.1.8)
|
43
|
+
pry (~> 0.9)
|
44
|
+
slop (~> 3.0)
|
45
|
+
rake (10.3.1)
|
46
|
+
rspec (2.14.1)
|
47
|
+
rspec-core (~> 2.14.0)
|
48
|
+
rspec-expectations (~> 2.14.0)
|
49
|
+
rspec-mocks (~> 2.14.0)
|
50
|
+
rspec-core (2.14.8)
|
51
|
+
rspec-expectations (2.14.5)
|
52
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
53
|
+
rspec-mocks (2.14.6)
|
54
|
+
slop (3.5.0)
|
55
|
+
sqlite3 (1.3.9)
|
61
56
|
sqlite3-ruby (1.3.3)
|
62
57
|
sqlite3 (>= 1.3.3)
|
63
|
-
|
64
|
-
tzinfo (
|
58
|
+
thread_safe (0.3.3)
|
59
|
+
tzinfo (1.1.0)
|
60
|
+
thread_safe (~> 0.1)
|
65
61
|
|
66
62
|
PLATFORMS
|
67
63
|
ruby
|
68
64
|
|
69
65
|
DEPENDENCIES
|
70
66
|
activerecord
|
71
|
-
bson_ext
|
72
|
-
mongo_mapper (~> 0.9)
|
73
|
-
mongoid (~> 2.3)
|
74
67
|
permalink!
|
75
|
-
pry
|
68
|
+
pry-meta
|
76
69
|
rake
|
77
|
-
rspec
|
70
|
+
rspec
|
78
71
|
sqlite3-ruby
|
79
|
-
test-unit
|
@@ -1,48 +1,55 @@
|
|
1
|
-
Permalink
|
2
|
-
=========
|
1
|
+
# Permalink
|
3
2
|
|
4
|
-
Instalation
|
5
|
-
-----------
|
3
|
+
## Instalation
|
6
4
|
|
7
5
|
gem install permalink
|
8
6
|
|
9
|
-
Usage
|
10
|
-
-----
|
7
|
+
## Usage
|
11
8
|
|
12
9
|
Add the method call `permalink` to your model. Your model should have a `permalink` attribute.
|
13
10
|
|
14
|
-
|
15
|
-
|
16
|
-
|
11
|
+
```ruby
|
12
|
+
class Page < ActiveRecord::Base
|
13
|
+
permalink :title
|
14
|
+
end
|
15
|
+
```
|
17
16
|
|
18
17
|
You can specify the permalink field:
|
19
18
|
|
20
|
-
|
21
|
-
|
22
|
-
|
19
|
+
```ruby
|
20
|
+
class page < ActiveRecord::Base
|
21
|
+
permalink :title, :to => :title_permalink
|
22
|
+
end
|
23
|
+
```
|
23
24
|
|
24
25
|
If you don't want to use `permalink`, you can call `'some text'.to_permalink` string method and
|
25
26
|
manage the permalink process by yourself.
|
26
27
|
|
27
28
|
Permalinks are not unique by default. `permalink` overrides `to_param` as following:
|
28
29
|
|
29
|
-
|
30
|
-
|
31
|
-
|
30
|
+
```ruby
|
31
|
+
def to_param
|
32
|
+
"#{id}-#{permalink}"
|
33
|
+
end
|
34
|
+
```
|
32
35
|
|
33
36
|
You can define the `to_param` format:
|
34
37
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
+
```ruby
|
39
|
+
class Page < ActiveRecord::Base
|
40
|
+
permalink :title, :to_param => %w(id permalink page)
|
41
|
+
end
|
42
|
+
```
|
38
43
|
|
39
44
|
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
45
|
|
41
46
|
If you want to generate unique permalink, use the `:unique` option:
|
42
47
|
|
43
|
-
|
44
|
-
|
45
|
-
|
48
|
+
```ruby
|
49
|
+
class Page < ActiveRecord::Base
|
50
|
+
permalink :title, :unique => true, :to_param => :permalink
|
51
|
+
end
|
52
|
+
```
|
46
53
|
|
47
54
|
The permalink is generated using `ActiveSupport::Multibyte::Chars` class; this means that characters will properly replaced from `áéíó` to `aeio`, for instance.
|
48
55
|
|
@@ -51,9 +58,11 @@ to generate a permalink when `before_save` callback is evaluated and the instanc
|
|
51
58
|
|
52
59
|
You can force the permalink generation by setting the `:force` option.
|
53
60
|
|
54
|
-
|
55
|
-
|
56
|
-
|
61
|
+
```ruby
|
62
|
+
class Page < ActiveRecord::Base
|
63
|
+
permalink :title, :force => true
|
64
|
+
end
|
65
|
+
```
|
57
66
|
|
58
67
|
## License
|
59
68
|
|
@@ -76,4 +85,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
76
85
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
77
86
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
78
87
|
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.
|
88
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/lib/permalink.rb
CHANGED
@@ -1,8 +1,5 @@
|
|
1
|
+
require "active_record"
|
1
2
|
require "permalink/string_ext"
|
2
|
-
require "permalink/
|
3
|
-
require "permalink/orm/active_record"
|
4
|
-
require "permalink/orm/mongo_mapper"
|
5
|
-
require "permalink/orm/mongoid"
|
3
|
+
require "permalink/active_record"
|
6
4
|
|
7
|
-
ActiveRecord::Base.send(:include, Permalink::
|
8
|
-
Mongoid::Document::ClassMethods.send(:include, Permalink::Orm::Mongoid) if defined?(Mongoid)
|
5
|
+
ActiveRecord::Base.send(:include, Permalink::ActiveRecord)
|
@@ -0,0 +1,91 @@
|
|
1
|
+
module Permalink
|
2
|
+
module ActiveRecord
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
base.extend(Permalink::ActiveRecord::ClassMethods)
|
6
|
+
class << base; attr_accessor :permalink_options; end
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
# permalink :title
|
11
|
+
# permalink :title, :to => :custom_permalink_field
|
12
|
+
# permalink :title, :to => :permalink, :to_param => [:id, :permalink]
|
13
|
+
# permalink :title, :unique => true
|
14
|
+
def permalink(from_column, options = {})
|
15
|
+
include InstanceMethods
|
16
|
+
|
17
|
+
options.reverse_merge!({
|
18
|
+
to_param: [:id, :permalink],
|
19
|
+
to: :permalink,
|
20
|
+
unique: false,
|
21
|
+
force: false
|
22
|
+
})
|
23
|
+
|
24
|
+
self.permalink_options = {
|
25
|
+
from_column_name: from_column,
|
26
|
+
to_column_name: options[:to],
|
27
|
+
to_param: [options[:to_param]].flatten,
|
28
|
+
unique: options[:unique],
|
29
|
+
force: options[:force]
|
30
|
+
}
|
31
|
+
|
32
|
+
before_validation :create_permalink
|
33
|
+
before_save :create_permalink
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
module InstanceMethods
|
38
|
+
def to_param
|
39
|
+
to_param_option = self.class.permalink_options[:to_param]
|
40
|
+
|
41
|
+
to_param_option.compact.map {|name|
|
42
|
+
respond_to?(name) ? public_send(name).to_s : name.to_s
|
43
|
+
}.reject(&:blank?).join("-")
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
def next_available_permalink(permalink)
|
48
|
+
unique_permalink = permalink
|
49
|
+
|
50
|
+
if self.class.permalink_options[:unique]
|
51
|
+
suffix = 2
|
52
|
+
|
53
|
+
while self.class.where(to_permalink_name => unique_permalink).first
|
54
|
+
unique_permalink = "#{permalink}-#{suffix}"
|
55
|
+
suffix += 1
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
unique_permalink
|
60
|
+
end
|
61
|
+
|
62
|
+
def from_permalink_name
|
63
|
+
self.class.permalink_options[:from_column_name]
|
64
|
+
end
|
65
|
+
|
66
|
+
def to_permalink_name
|
67
|
+
self.class.permalink_options[:to_column_name]
|
68
|
+
end
|
69
|
+
|
70
|
+
def from_permalink_value
|
71
|
+
read_attribute(from_permalink_name)
|
72
|
+
end
|
73
|
+
|
74
|
+
def to_permalink_value
|
75
|
+
read_attribute(to_permalink_name)
|
76
|
+
end
|
77
|
+
|
78
|
+
def update_permalink?
|
79
|
+
changes[from_permalink_name] &&
|
80
|
+
(self.class.permalink_options[:force] || to_permalink_value.blank?)
|
81
|
+
end
|
82
|
+
|
83
|
+
def create_permalink
|
84
|
+
write_attribute(
|
85
|
+
to_permalink_name,
|
86
|
+
next_available_permalink(from_permalink_value.to_s.to_permalink)
|
87
|
+
) if update_permalink?
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
data/lib/permalink/version.rb
CHANGED
data/permalink.gemspec
CHANGED
@@ -1,6 +1,4 @@
|
|
1
|
-
|
2
|
-
$:.push File.expand_path("../lib", __FILE__)
|
3
|
-
require "permalink/version"
|
1
|
+
require "./lib/permalink/version"
|
4
2
|
|
5
3
|
Gem::Specification.new do |s|
|
6
4
|
s.name = "permalink"
|
@@ -19,11 +17,7 @@ Gem::Specification.new do |s|
|
|
19
17
|
|
20
18
|
s.add_development_dependency "activerecord"
|
21
19
|
s.add_development_dependency "sqlite3-ruby"
|
22
|
-
s.add_development_dependency "
|
23
|
-
s.add_development_dependency "
|
24
|
-
s.add_development_dependency "mongoid", "~> 2.3"
|
25
|
-
s.add_development_dependency "mongo_mapper", "~> 0.9"
|
26
|
-
s.add_development_dependency "bson_ext"
|
27
|
-
s.add_development_dependency "pry"
|
20
|
+
s.add_development_dependency "rspec"
|
21
|
+
s.add_development_dependency "pry-meta"
|
28
22
|
s.add_development_dependency "rake"
|
29
23
|
end
|
@@ -1,13 +1,127 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
|
-
describe Permalink::
|
3
|
+
describe Permalink::ActiveRecord do
|
4
4
|
let(:model) { Post }
|
5
|
-
it_should_behave_like "orm"
|
6
5
|
|
7
|
-
|
6
|
+
before do
|
7
|
+
model.delete_all
|
8
|
+
model.permalink :title
|
9
|
+
end
|
10
|
+
|
11
|
+
it "responds to options" do
|
12
|
+
expect(model).to respond_to(:permalink_options)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "uses default options" do
|
16
|
+
model.permalink :title
|
17
|
+
record = model.create(:title => "Some nice post")
|
18
|
+
expect(record.permalink).to eq("some-nice-post")
|
19
|
+
end
|
20
|
+
|
21
|
+
it "uses custom attribute" do
|
22
|
+
model.permalink :title, :to => :slug
|
23
|
+
record = model.create(:title => "Some nice post")
|
24
|
+
expect(record.slug).to eq("some-nice-post")
|
25
|
+
end
|
26
|
+
|
27
|
+
it "sets permalink before_save" do
|
28
|
+
record = model.new(:title => "Some nice post")
|
29
|
+
expect(record.permalink).to be_nil
|
30
|
+
record.valid?
|
31
|
+
expect(record.permalink).to eq("some-nice-post")
|
32
|
+
end
|
33
|
+
|
34
|
+
it "creates unique permalinks" do
|
35
|
+
model.permalink :title, :unique => true
|
36
|
+
|
37
|
+
record = model.create(:title => "Some nice post")
|
38
|
+
expect(record.permalink).to eq("some-nice-post")
|
39
|
+
|
40
|
+
record = model.create(:title => "Some nice post")
|
41
|
+
expect(record.permalink).to eq("some-nice-post-2")
|
42
|
+
|
43
|
+
record = model.create(:title => "Some nice post")
|
44
|
+
expect(record.permalink).to eq("some-nice-post-3")
|
45
|
+
end
|
46
|
+
|
47
|
+
it "returns param for unique permalink" do
|
48
|
+
model.permalink :title, :to_param => :permalink, :unique => true
|
49
|
+
|
50
|
+
record = model.create(:title => "Ruby")
|
51
|
+
expect(record.to_param).to eq("ruby")
|
52
|
+
|
53
|
+
record = model.create(:title => "Ruby")
|
54
|
+
expect(record.to_param).to eq("ruby-2")
|
55
|
+
end
|
56
|
+
|
57
|
+
it "overrides to_param with custom fields" do
|
58
|
+
model.permalink :title, :to => :slug, :to_param => [:slug, :id, "page"]
|
59
|
+
|
60
|
+
record = model.create(:title => "Some nice post")
|
61
|
+
expect(record.to_param).to eq("some-nice-post-#{record.id}-page")
|
62
|
+
end
|
63
|
+
|
64
|
+
it "ignores blank attributes from to_param" do
|
65
|
+
model.permalink :title, :to_param => [:id, " ", nil, "\t", :permalink]
|
66
|
+
|
67
|
+
record = model.create(:title => "Some nice post")
|
68
|
+
expect(record.to_param).to eq("#{record.id}-some-nice-post")
|
69
|
+
end
|
70
|
+
|
71
|
+
it "sets permalink if permalink is blank" do
|
72
|
+
record = model.create(:title => "Some nice post", :permalink => " ")
|
73
|
+
expect(record.permalink).to eq("some-nice-post")
|
74
|
+
end
|
75
|
+
|
76
|
+
it "keeps defined permalink" do
|
77
|
+
record = model.create(:title => "Some nice post", :permalink => "awesome-post")
|
78
|
+
expect(record.permalink).to eq("awesome-post")
|
79
|
+
end
|
80
|
+
|
81
|
+
it "creates unique permalinks for number-ended titles" do
|
82
|
+
model.permalink :title, :unique => true
|
83
|
+
|
84
|
+
record = model.create(:title => "Rails 3")
|
85
|
+
expect(record.permalink).to eq("rails-3")
|
86
|
+
|
87
|
+
record = model.create(:title => "Rails 3")
|
88
|
+
expect(record.permalink).to eq("rails-3-2")
|
89
|
+
end
|
90
|
+
|
91
|
+
it "forces permalink" do
|
92
|
+
model.permalink :title, :force => true
|
93
|
+
|
94
|
+
record = model.create(:title => "Some nice post")
|
95
|
+
record.update_attributes :title => "Awesome post"
|
96
|
+
|
97
|
+
expect(record.permalink).to eq("awesome-post")
|
98
|
+
end
|
99
|
+
|
100
|
+
it "forces permalink and keep unique" do
|
101
|
+
model.permalink :title, :force => true, :unique => true
|
102
|
+
|
103
|
+
record = model.create(:title => "Some nice post")
|
104
|
+
|
105
|
+
record.update_attributes :title => "Awesome post"
|
106
|
+
expect(record.permalink).to eq("awesome-post")
|
107
|
+
|
108
|
+
record = model.create(:title => "Awesome post")
|
109
|
+
expect(record.permalink).to eq("awesome-post-2")
|
110
|
+
end
|
111
|
+
|
112
|
+
it "keeps same permalink when another field changes" do
|
113
|
+
model.permalink :title, :force => true, :unique => true
|
114
|
+
|
115
|
+
record = model.create(:title => "Some nice post")
|
116
|
+
record.update_attributes :description => "some description"
|
117
|
+
|
118
|
+
expect(record.permalink).to eq("some-nice-post")
|
119
|
+
end
|
120
|
+
|
121
|
+
it "overrides to_param method" do
|
8
122
|
model.permalink :title
|
9
123
|
|
10
124
|
record = model.create(:title => "Some nice post")
|
11
|
-
record.to_param.
|
125
|
+
expect(record.to_param).to eql("#{record.id}-some-nice-post")
|
12
126
|
end
|
13
127
|
end
|
@@ -2,7 +2,7 @@
|
|
2
2
|
require "spec_helper"
|
3
3
|
|
4
4
|
describe "String#to_permalink" do
|
5
|
-
|
5
|
+
{
|
6
6
|
'This IS a Tripped out title!!.!1 (well/ not really)' => 'this-is-a-tripped-out-title-1-well-not-really',
|
7
7
|
'////// meph1sto r0x ! \\\\\\' => 'meph1sto-r0x',
|
8
8
|
'āčēģīķļņū' => 'acegiklnu',
|
@@ -13,11 +13,9 @@ describe "String#to_permalink" do
|
|
13
13
|
"Text\nwith\nline\n\n\tbreaks" => 'text-with-line-breaks',
|
14
14
|
"can't do it" => "cant-do-it",
|
15
15
|
"i'm a dog" => "im-a-dog"
|
16
|
-
}
|
17
|
-
|
18
|
-
|
19
|
-
SAMPLES.each do |from, to|
|
20
|
-
from.to_permalink.should == to
|
16
|
+
}.each do |from, to|
|
17
|
+
it "creates permalink for #{from}" do
|
18
|
+
expect(from.to_permalink).to eql(to)
|
21
19
|
end
|
22
20
|
end
|
23
21
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,24 +1,8 @@
|
|
1
|
-
require "
|
2
|
-
require "mongoid"
|
3
|
-
require "rspec"
|
4
|
-
require "active_record"
|
1
|
+
require "bundler/setup"
|
5
2
|
require "permalink"
|
6
3
|
|
7
|
-
|
8
|
-
|
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
|
4
|
+
ActiveRecord::Base
|
5
|
+
.establish_connection(adapter: "sqlite3", database: ":memory:")
|
19
6
|
|
20
7
|
load("support/schema.rb")
|
21
|
-
require "support/page"
|
22
|
-
require "support/shared"
|
23
8
|
require "support/post"
|
24
|
-
require "support/article"
|
metadata
CHANGED
@@ -1,115 +1,85 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: permalink
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
5
|
-
prerelease:
|
4
|
+
version: 1.2.2
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Nando Vieira
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-05-14 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: activerecord
|
16
|
-
requirement:
|
17
|
-
none: false
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - ">="
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '0'
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
|
-
version_requirements:
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
25
27
|
- !ruby/object:Gem::Dependency
|
26
28
|
name: sqlite3-ruby
|
27
|
-
requirement:
|
28
|
-
none: false
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
29
30
|
requirements:
|
30
|
-
- -
|
31
|
+
- - ">="
|
31
32
|
- !ruby/object:Gem::Version
|
32
33
|
version: '0'
|
33
34
|
type: :development
|
34
35
|
prerelease: false
|
35
|
-
version_requirements:
|
36
|
-
- !ruby/object:Gem::Dependency
|
37
|
-
name: test-unit
|
38
|
-
requirement: &70228101510700 !ruby/object:Gem::Requirement
|
39
|
-
none: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
37
|
requirements:
|
41
|
-
- -
|
38
|
+
- - ">="
|
42
39
|
- !ruby/object:Gem::Version
|
43
40
|
version: '0'
|
44
|
-
type: :development
|
45
|
-
prerelease: false
|
46
|
-
version_requirements: *70228101510700
|
47
41
|
- !ruby/object:Gem::Dependency
|
48
42
|
name: rspec
|
49
|
-
requirement:
|
50
|
-
none: false
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
51
44
|
requirements:
|
52
|
-
- -
|
45
|
+
- - ">="
|
53
46
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
47
|
+
version: '0'
|
55
48
|
type: :development
|
56
49
|
prerelease: false
|
57
|
-
version_requirements:
|
58
|
-
- !ruby/object:Gem::Dependency
|
59
|
-
name: mongoid
|
60
|
-
requirement: &70228101525920 !ruby/object:Gem::Requirement
|
61
|
-
none: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
62
51
|
requirements:
|
63
|
-
- -
|
52
|
+
- - ">="
|
64
53
|
- !ruby/object:Gem::Version
|
65
|
-
version: '
|
66
|
-
type: :development
|
67
|
-
prerelease: false
|
68
|
-
version_requirements: *70228101525920
|
54
|
+
version: '0'
|
69
55
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
71
|
-
requirement:
|
72
|
-
none: false
|
56
|
+
name: pry-meta
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
73
58
|
requirements:
|
74
|
-
- -
|
59
|
+
- - ">="
|
75
60
|
- !ruby/object:Gem::Version
|
76
|
-
version: '0
|
61
|
+
version: '0'
|
77
62
|
type: :development
|
78
63
|
prerelease: false
|
79
|
-
version_requirements:
|
80
|
-
- !ruby/object:Gem::Dependency
|
81
|
-
name: bson_ext
|
82
|
-
requirement: &70228101524280 !ruby/object:Gem::Requirement
|
83
|
-
none: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
84
65
|
requirements:
|
85
|
-
- -
|
66
|
+
- - ">="
|
86
67
|
- !ruby/object:Gem::Version
|
87
68
|
version: '0'
|
88
|
-
type: :development
|
89
|
-
prerelease: false
|
90
|
-
version_requirements: *70228101524280
|
91
69
|
- !ruby/object:Gem::Dependency
|
92
|
-
name:
|
93
|
-
requirement:
|
94
|
-
none: false
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
95
72
|
requirements:
|
96
|
-
- -
|
73
|
+
- - ">="
|
97
74
|
- !ruby/object:Gem::Version
|
98
75
|
version: '0'
|
99
76
|
type: :development
|
100
77
|
prerelease: false
|
101
|
-
version_requirements:
|
102
|
-
- !ruby/object:Gem::Dependency
|
103
|
-
name: rake
|
104
|
-
requirement: &70228101522800 !ruby/object:Gem::Requirement
|
105
|
-
none: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
106
79
|
requirements:
|
107
|
-
- -
|
80
|
+
- - ">="
|
108
81
|
- !ruby/object:Gem::Version
|
109
82
|
version: '0'
|
110
|
-
type: :development
|
111
|
-
prerelease: false
|
112
|
-
version_requirements: *70228101522800
|
113
83
|
description: Generate permalink attributes on ActiveRecord
|
114
84
|
email:
|
115
85
|
- fnando.vieira@gmail.com
|
@@ -117,68 +87,48 @@ executables: []
|
|
117
87
|
extensions: []
|
118
88
|
extra_rdoc_files: []
|
119
89
|
files:
|
120
|
-
- .gitignore
|
121
|
-
- .rspec
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
122
92
|
- Gemfile
|
123
93
|
- Gemfile.lock
|
124
|
-
- README.
|
94
|
+
- README.md
|
125
95
|
- Rakefile
|
126
96
|
- lib/permalink.rb
|
127
|
-
- lib/permalink/
|
128
|
-
- lib/permalink/orm/base.rb
|
129
|
-
- lib/permalink/orm/mongo_mapper.rb
|
130
|
-
- lib/permalink/orm/mongoid.rb
|
97
|
+
- lib/permalink/active_record.rb
|
131
98
|
- lib/permalink/string_ext.rb
|
132
99
|
- lib/permalink/version.rb
|
133
100
|
- permalink.gemspec
|
134
101
|
- spec/permalink/active_record_spec.rb
|
135
|
-
- spec/permalink/mongo_mapper_spec.rb
|
136
|
-
- spec/permalink/mongoid_spec.rb
|
137
102
|
- spec/permalink/string_spec.rb
|
138
103
|
- spec/spec_helper.rb
|
139
|
-
- spec/support/article.rb
|
140
|
-
- spec/support/page.rb
|
141
104
|
- spec/support/post.rb
|
142
105
|
- spec/support/schema.rb
|
143
|
-
- spec/support/shared.rb
|
144
106
|
homepage: http://rubygems.org/gems/permalink
|
145
107
|
licenses: []
|
108
|
+
metadata: {}
|
146
109
|
post_install_message:
|
147
110
|
rdoc_options: []
|
148
111
|
require_paths:
|
149
112
|
- lib
|
150
113
|
required_ruby_version: !ruby/object:Gem::Requirement
|
151
|
-
none: false
|
152
114
|
requirements:
|
153
|
-
- -
|
115
|
+
- - ">="
|
154
116
|
- !ruby/object:Gem::Version
|
155
117
|
version: '0'
|
156
|
-
segments:
|
157
|
-
- 0
|
158
|
-
hash: -1346400587143548814
|
159
118
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
160
|
-
none: false
|
161
119
|
requirements:
|
162
|
-
- -
|
120
|
+
- - ">="
|
163
121
|
- !ruby/object:Gem::Version
|
164
122
|
version: '0'
|
165
|
-
segments:
|
166
|
-
- 0
|
167
|
-
hash: -1346400587143548814
|
168
123
|
requirements: []
|
169
124
|
rubyforge_project:
|
170
|
-
rubygems_version:
|
125
|
+
rubygems_version: 2.2.2
|
171
126
|
signing_key:
|
172
|
-
specification_version:
|
127
|
+
specification_version: 4
|
173
128
|
summary: Generate permalink attributes on ActiveRecord
|
174
129
|
test_files:
|
175
130
|
- spec/permalink/active_record_spec.rb
|
176
|
-
- spec/permalink/mongo_mapper_spec.rb
|
177
|
-
- spec/permalink/mongoid_spec.rb
|
178
131
|
- spec/permalink/string_spec.rb
|
179
132
|
- spec/spec_helper.rb
|
180
|
-
- spec/support/article.rb
|
181
|
-
- spec/support/page.rb
|
182
133
|
- spec/support/post.rb
|
183
134
|
- spec/support/schema.rb
|
184
|
-
- spec/support/shared.rb
|
@@ -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
|
-
unique_permalink = permalink
|
42
|
-
|
43
|
-
if self.class.permalink_options[:unique]
|
44
|
-
suffix = 2
|
45
|
-
|
46
|
-
while self.class.where(to_permalink_name => unique_permalink).first
|
47
|
-
unique_permalink = "#{permalink}-#{suffix}"
|
48
|
-
suffix += 1
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
unique_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
|
-
changes[from_permalink_name] && (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
|
@@ -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
|
data/spec/support/article.rb
DELETED
data/spec/support/page.rb
DELETED
data/spec/support/shared.rb
DELETED
@@ -1,117 +0,0 @@
|
|
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.to_param.should == "#{record.id}-some-nice-post"
|
65
|
-
end
|
66
|
-
|
67
|
-
it "should set permalink if permalink is blank" do
|
68
|
-
record = model.create(:title => "Some nice post", :permalink => " ")
|
69
|
-
record.permalink.should == "some-nice-post"
|
70
|
-
end
|
71
|
-
|
72
|
-
it "should keep defined permalink" do
|
73
|
-
record = model.create(:title => "Some nice post", :permalink => "awesome-post")
|
74
|
-
record.permalink.should == "awesome-post"
|
75
|
-
end
|
76
|
-
|
77
|
-
it "should create unique permalinks for number-ended titles" do
|
78
|
-
model.permalink :title, :unique => true
|
79
|
-
|
80
|
-
record = model.create(:title => "Rails 3")
|
81
|
-
record.permalink.should == "rails-3"
|
82
|
-
|
83
|
-
record = model.create(:title => "Rails 3")
|
84
|
-
record.permalink.should == "rails-3-2"
|
85
|
-
end
|
86
|
-
|
87
|
-
it "should force permalink" do
|
88
|
-
model.permalink :title, :force => true
|
89
|
-
|
90
|
-
record = model.create(:title => "Some nice post")
|
91
|
-
record.update_attributes :title => "Awesome post"
|
92
|
-
|
93
|
-
record.permalink.should == "awesome-post"
|
94
|
-
end
|
95
|
-
|
96
|
-
it "should force permalink and keep unique" do
|
97
|
-
model.permalink :title, :force => true, :unique => true
|
98
|
-
|
99
|
-
record = model.create(:title => "Some nice post")
|
100
|
-
|
101
|
-
record.update_attributes :title => "Awesome post"
|
102
|
-
record.permalink.should == "awesome-post"
|
103
|
-
|
104
|
-
record = model.create(:title => "Awesome post")
|
105
|
-
record.permalink.should == "awesome-post-2"
|
106
|
-
end
|
107
|
-
|
108
|
-
it "should keep same permalink when another field changes" do
|
109
|
-
model.permalink :title, :force => true, :unique => true
|
110
|
-
|
111
|
-
record = model.create(:title => "Some nice post")
|
112
|
-
record.update_attributes :description => "some description"
|
113
|
-
|
114
|
-
record.permalink.should == "some-nice-post"
|
115
|
-
end
|
116
|
-
|
117
|
-
end
|