permalink 0.1.1 → 1.0.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/README.markdown +10 -15
- data/Rakefile +4 -4
- data/lib/permalink.rb +69 -71
- data/lib/permalink/version.rb +6 -8
- data/test/models/beer.rb +1 -1
- data/test/models/donut.rb +1 -1
- data/test/models/post.rb +2 -2
- data/test/models/user.rb +1 -1
- data/test/test_helper.rb +2 -2
- metadata +5 -6
- data/init.rb +0 -1
data/README.markdown
CHANGED
@@ -1,35 +1,30 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
Permalink
|
2
|
+
=========
|
3
3
|
|
4
4
|
Instalation
|
5
5
|
-----------
|
6
6
|
|
7
|
-
You can use I18n-JS as plugin and gem. Choose what's best for you!
|
8
|
-
|
9
|
-
script/plugin install git://github.com/fnando/has_permalink.git
|
10
|
-
|
11
|
-
or
|
12
7
|
gem install permalink
|
13
8
|
|
14
9
|
Usage
|
15
10
|
-----
|
16
11
|
|
17
|
-
Add the method call `
|
12
|
+
Add the method call `permalink` to your model. Your model should have a `permalink` attribute.
|
18
13
|
|
19
14
|
class Page < ActiveRecord::Base
|
20
|
-
|
15
|
+
permalink :title
|
21
16
|
end
|
22
17
|
|
23
18
|
You can specify the permalink field:
|
24
19
|
|
25
20
|
class page < ActiveRecord::Base
|
26
|
-
|
21
|
+
permalink :title, :to => :title_permalink
|
27
22
|
end
|
28
23
|
|
29
|
-
If you don't want to use `
|
24
|
+
If you don't want to use `permalink`, you can call `'some text'.to_permalink` string method and
|
30
25
|
manage the permalink process by yourself.
|
31
26
|
|
32
|
-
Permalinks are not unique by default. `
|
27
|
+
Permalinks are not unique by default. `permalink` overrides `to_param` as following:
|
33
28
|
|
34
29
|
def to_param
|
35
30
|
"#{id}-#{permalink}"
|
@@ -38,15 +33,15 @@ Permalinks are not unique by default. `has_permalink` overrides `to_param` as fo
|
|
38
33
|
You can define the `to_param` format:
|
39
34
|
|
40
35
|
class Page < ActiveRecord::Base
|
41
|
-
|
36
|
+
permalink :title, :to_param => %w(id permalink page)
|
42
37
|
end
|
43
38
|
|
44
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.
|
45
40
|
|
46
|
-
If you want to generate unique permalink, use the
|
41
|
+
If you want to generate unique permalink, use the `:unique` option:
|
47
42
|
|
48
43
|
class Page < ActiveRecord::Base
|
49
|
-
|
44
|
+
permalink :title, :unique => true, :to_param => :permalink
|
50
45
|
end
|
51
46
|
|
52
47
|
The permalink is generated using `ActiveSupport::Multibyte::Chars` class; this means that characters will properly replaced from `áéíó` to `aeio`, for instance.
|
data/Rakefile
CHANGED
@@ -4,7 +4,7 @@ require File.dirname(__FILE__) + "/lib/permalink/version"
|
|
4
4
|
desc "Default: run unit tests."
|
5
5
|
task :default => :test
|
6
6
|
|
7
|
-
desc "
|
7
|
+
desc "Run tests"
|
8
8
|
Rake::TestTask.new(:test) do |t|
|
9
9
|
t.libs << "lib"
|
10
10
|
t.libs << "test"
|
@@ -18,11 +18,11 @@ begin
|
|
18
18
|
JEWEL = Jeweler::Tasks.new do |gem|
|
19
19
|
gem.name = "permalink"
|
20
20
|
gem.email = "fnando.vieira@gmail.com"
|
21
|
-
gem.homepage = "http://github.com/fnando/
|
21
|
+
gem.homepage = "http://github.com/fnando/permalink"
|
22
22
|
gem.authors = ["Nando Vieira"]
|
23
|
-
gem.version =
|
23
|
+
gem.version = Permalink::Version::STRING
|
24
24
|
gem.summary = "ActiveRecord plugin for automatically converting fields to permalinks."
|
25
|
-
gem.files = FileList["README.markdown", "
|
25
|
+
gem.files = FileList["README.markdown", "{lib,test}/**/*", "Rakefile"]
|
26
26
|
end
|
27
27
|
|
28
28
|
Jeweler::GemcutterTasks.new
|
data/lib/permalink.rb
CHANGED
@@ -1,94 +1,92 @@
|
|
1
1
|
require "permalink/string_ext"
|
2
2
|
require "active_record"
|
3
3
|
|
4
|
-
module
|
5
|
-
|
6
|
-
|
7
|
-
base.extend ClassMethods
|
4
|
+
module Permalink
|
5
|
+
def self.included(base)
|
6
|
+
base.extend ClassMethods
|
8
7
|
|
9
|
-
|
10
|
-
|
11
|
-
end
|
8
|
+
class << base
|
9
|
+
attr_accessor :permalink_options
|
12
10
|
end
|
11
|
+
end
|
13
12
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
end
|
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
|
38
36
|
end
|
37
|
+
end
|
39
38
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
to_param_option.compact.collect do |name|
|
45
|
-
if respond_to?(name)
|
46
|
-
send(name).to_s
|
47
|
-
else
|
48
|
-
name.to_s
|
49
|
-
end
|
50
|
-
end.reject(&:blank?).join('-')
|
51
|
-
end
|
39
|
+
module InstanceMethods
|
40
|
+
def to_param
|
41
|
+
to_param_option = self.class.permalink_options[:to_param]
|
52
42
|
|
53
|
-
|
54
|
-
|
55
|
-
|
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
|
56
51
|
|
57
|
-
|
58
|
-
|
52
|
+
private
|
53
|
+
def next_available_permalink(_permalink)
|
54
|
+
the_permalink = _permalink
|
59
55
|
|
60
|
-
|
61
|
-
|
62
|
-
suffix += 1
|
63
|
-
end
|
64
|
-
end
|
56
|
+
if self.class.permalink_options[:unique]
|
57
|
+
suffix = 2
|
65
58
|
|
66
|
-
the_permalink
|
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
|
67
63
|
end
|
68
64
|
|
69
|
-
|
70
|
-
|
71
|
-
end
|
65
|
+
the_permalink
|
66
|
+
end
|
72
67
|
|
73
|
-
|
74
|
-
|
75
|
-
|
68
|
+
def from_permalink_name
|
69
|
+
self.class.permalink_options[:from_column_name]
|
70
|
+
end
|
76
71
|
|
77
|
-
|
78
|
-
|
79
|
-
|
72
|
+
def to_permalink_name
|
73
|
+
self.class.permalink_options[:to_column_name]
|
74
|
+
end
|
80
75
|
|
81
|
-
|
82
|
-
|
83
|
-
|
76
|
+
def from_permalink_value
|
77
|
+
read_attribute(from_permalink_name)
|
78
|
+
end
|
84
79
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
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))
|
89
87
|
end
|
90
88
|
end
|
91
89
|
end
|
92
90
|
end
|
93
91
|
|
94
|
-
ActiveRecord::Base.send(:include,
|
92
|
+
ActiveRecord::Base.send(:include, Permalink)
|
data/lib/permalink/version.rb
CHANGED
@@ -1,10 +1,8 @@
|
|
1
|
-
module
|
2
|
-
module
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
|
8
|
-
end
|
1
|
+
module Permalink
|
2
|
+
module Version
|
3
|
+
MAJOR = 1
|
4
|
+
MINOR = 0
|
5
|
+
PATCH = 0
|
6
|
+
STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
|
9
7
|
end
|
10
8
|
end
|
data/test/models/beer.rb
CHANGED
data/test/models/donut.rb
CHANGED
data/test/models/post.rb
CHANGED
data/test/models/user.rb
CHANGED
data/test/test_helper.rb
CHANGED
@@ -6,5 +6,5 @@ require "active_support/test_case"
|
|
6
6
|
require "permalink"
|
7
7
|
|
8
8
|
Dir.glob(File.dirname(__FILE__) + "/models/*.rb").each {|r| require r }
|
9
|
-
ActiveRecord::Base.establish_connection(:adapter =>
|
10
|
-
load(
|
9
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
10
|
+
load("schema.rb")
|
metadata
CHANGED
@@ -3,10 +3,10 @@ name: permalink
|
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
|
-
- 0
|
7
|
-
- 1
|
8
6
|
- 1
|
9
|
-
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
version: 1.0.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Nando Vieira
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-10-06 00:00:00 -03:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
@@ -29,7 +29,6 @@ extra_rdoc_files:
|
|
29
29
|
files:
|
30
30
|
- README.markdown
|
31
31
|
- Rakefile
|
32
|
-
- init.rb
|
33
32
|
- lib/permalink.rb
|
34
33
|
- lib/permalink/string_ext.rb
|
35
34
|
- lib/permalink/version.rb
|
@@ -41,7 +40,7 @@ files:
|
|
41
40
|
- test/schema.rb
|
42
41
|
- test/test_helper.rb
|
43
42
|
has_rdoc: true
|
44
|
-
homepage: http://github.com/fnando/
|
43
|
+
homepage: http://github.com/fnando/permalink
|
45
44
|
licenses: []
|
46
45
|
|
47
46
|
post_install_message:
|
data/init.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require "permalink"
|