friendly_id4 4.0.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +11 -0
- data/Guide.md +368 -0
- data/README.md +92 -0
- data/Rakefile +111 -0
- data/WhatsNew.md +142 -0
- data/bench.rb +71 -0
- data/friendly_id.gemspec +31 -0
- data/lib/friendly_id.rb +14 -0
- data/lib/friendly_id/base.rb +29 -0
- data/lib/friendly_id/configuration.rb +31 -0
- data/lib/friendly_id/finder_methods.rb +14 -0
- data/lib/friendly_id/history.rb +27 -0
- data/lib/friendly_id/migration.rb +17 -0
- data/lib/friendly_id/model.rb +22 -0
- data/lib/friendly_id/object_utils.rb +27 -0
- data/lib/friendly_id/scoped.rb +54 -0
- data/lib/friendly_id/slug.rb +3 -0
- data/lib/friendly_id/slug_sequencer.rb +80 -0
- data/lib/friendly_id/slugged.rb +51 -0
- data/lib/friendly_id/version.rb +9 -0
- data/lib/generators/friendly_id_generator.rb +21 -0
- data/test/config/mysql.yml +5 -0
- data/test/config/postgres.yml +6 -0
- data/test/config/sqlite3.yml +3 -0
- data/test/core_test.rb +43 -0
- data/test/helper.rb +86 -0
- data/test/history_test.rb +46 -0
- data/test/object_utils_test.rb +26 -0
- data/test/schema.rb +53 -0
- data/test/scoped_test.rb +49 -0
- data/test/shared.rb +76 -0
- data/test/slugged_test.rb +111 -0
- metadata +163 -0
@@ -0,0 +1,46 @@
|
|
1
|
+
require File.expand_path("../helper.rb", __FILE__)
|
2
|
+
|
3
|
+
class Manual < ActiveRecord::Base
|
4
|
+
include FriendlyId::History
|
5
|
+
has_friendly_id :name
|
6
|
+
end
|
7
|
+
|
8
|
+
class HistoryTest < MiniTest::Unit::TestCase
|
9
|
+
|
10
|
+
include FriendlyId::Test
|
11
|
+
include FriendlyId::Test::Shared
|
12
|
+
|
13
|
+
def klass
|
14
|
+
Manual
|
15
|
+
end
|
16
|
+
|
17
|
+
test "should insert record in slugs table on create" do
|
18
|
+
with_instance_of(klass) {|record| assert !record.friendly_id_slugs.empty?}
|
19
|
+
end
|
20
|
+
|
21
|
+
test "should not create new slug record if friendly_id is not changed" do
|
22
|
+
with_instance_of(klass) do |record|
|
23
|
+
record.active = true
|
24
|
+
record.save!
|
25
|
+
assert_equal 1, FriendlyIdSlug.count
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
test "should create new slug record when friendly_id changes" do
|
30
|
+
with_instance_of(klass) do |record|
|
31
|
+
record.name = record.name + "b"
|
32
|
+
record.save!
|
33
|
+
assert_equal 2, FriendlyIdSlug.count
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
test "should be findable by old slugs" do
|
38
|
+
with_instance_of(klass) do |record|
|
39
|
+
old_friendly_id = record.friendly_id
|
40
|
+
record.name = record.name + "b"
|
41
|
+
record.save!
|
42
|
+
assert found = klass.find_by_friendly_id(old_friendly_id)
|
43
|
+
assert !found.readonly?
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require File.expand_path("../helper.rb", __FILE__)
|
2
|
+
|
3
|
+
|
4
|
+
class ObjectUtilsTest < MiniTest::Unit::TestCase
|
5
|
+
|
6
|
+
include FriendlyId::Test
|
7
|
+
|
8
|
+
test "strings with letters are friendly_ids" do
|
9
|
+
assert "a".friendly_id?
|
10
|
+
end
|
11
|
+
|
12
|
+
test "integers should be unfriendly ids" do
|
13
|
+
assert 1.unfriendly_id?
|
14
|
+
end
|
15
|
+
|
16
|
+
test "numeric strings are neither friendly nor unfriendly" do
|
17
|
+
assert_equal nil, "1".friendly_id?
|
18
|
+
assert_equal nil, "1".unfriendly_id?
|
19
|
+
end
|
20
|
+
|
21
|
+
test "ActiveRecord::Base instances should be unfriendly_ids" do
|
22
|
+
klass = Class.new(ActiveRecord::Base)
|
23
|
+
klass.table_name = "authors"
|
24
|
+
assert klass.new.unfriendly_id?
|
25
|
+
end
|
26
|
+
end
|
data/test/schema.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require "friendly_id/migration"
|
2
|
+
|
3
|
+
module FriendlyId
|
4
|
+
module Test
|
5
|
+
class Schema < ActiveRecord::Migration
|
6
|
+
class << self
|
7
|
+
def down
|
8
|
+
CreateFriendlyIdSlugs.down
|
9
|
+
tables.each do |name|
|
10
|
+
drop_table name
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def up
|
15
|
+
CreateFriendlyIdSlugs.up
|
16
|
+
|
17
|
+
tables.each do |table_name|
|
18
|
+
create_table table_name do |t|
|
19
|
+
t.string :name
|
20
|
+
t.boolean :active
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
slugged_tables.each do |table_name|
|
25
|
+
add_column table_name, :slug, :string
|
26
|
+
add_index table_name, :slug, :unique => true
|
27
|
+
end
|
28
|
+
|
29
|
+
# This will be used to test scopes
|
30
|
+
add_column :novels, :novelist_id, :integer
|
31
|
+
remove_index :novels, :slug
|
32
|
+
|
33
|
+
# This will be used to test column name quoting
|
34
|
+
add_column :journalists, "strange name", :string
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def slugged_tables
|
40
|
+
["journalists", "articles", "novelists", "novels", "manuals"]
|
41
|
+
end
|
42
|
+
|
43
|
+
def simple_tables
|
44
|
+
["authors", "books"]
|
45
|
+
end
|
46
|
+
|
47
|
+
def tables
|
48
|
+
simple_tables + slugged_tables
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/test/scoped_test.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require File.expand_path("../helper", __FILE__)
|
2
|
+
|
3
|
+
class Novelist < ActiveRecord::Base
|
4
|
+
include FriendlyId::Slugged
|
5
|
+
has_friendly_id :name
|
6
|
+
end
|
7
|
+
|
8
|
+
class Novel < ActiveRecord::Base
|
9
|
+
include FriendlyId::Scoped
|
10
|
+
belongs_to :novelist
|
11
|
+
has_friendly_id :name, :scope => :novelist
|
12
|
+
end
|
13
|
+
|
14
|
+
class ScopedTest < MiniTest::Unit::TestCase
|
15
|
+
|
16
|
+
include FriendlyId::Test
|
17
|
+
include FriendlyId::Test::Shared
|
18
|
+
|
19
|
+
def klass
|
20
|
+
Novel
|
21
|
+
end
|
22
|
+
|
23
|
+
test "should detect scope column from belongs_to relation" do
|
24
|
+
assert_equal "novelist_id", Novel.friendly_id_config.scope_column
|
25
|
+
end
|
26
|
+
|
27
|
+
test "should detect scope column from explicit column name" do
|
28
|
+
klass = Class.new(ActiveRecord::Base)
|
29
|
+
klass.has_friendly_id :empty, :scope => :dummy
|
30
|
+
assert_equal "dummy", klass.friendly_id_config.scope_column
|
31
|
+
end
|
32
|
+
|
33
|
+
test "should allow duplicate slugs outside scope" do
|
34
|
+
transaction do
|
35
|
+
novel1 = Novel.create! :name => "a", :novelist => Novelist.create!(:name => "a")
|
36
|
+
novel2 = Novel.create! :name => "a", :novelist => Novelist.create!(:name => "b")
|
37
|
+
assert_equal novel1.friendly_id, novel2.friendly_id
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
test "should not allow duplicate slugs inside scope" do
|
42
|
+
with_instance_of Novelist do |novelist|
|
43
|
+
novel1 = Novel.create! :name => "a", :novelist => novelist
|
44
|
+
novel2 = Novel.create! :name => "a", :novelist => novelist
|
45
|
+
assert novel1.friendly_id != novel2.friendly_id
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
data/test/shared.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
module FriendlyId
|
2
|
+
module Test
|
3
|
+
module Shared
|
4
|
+
|
5
|
+
test "finds should respect conditions" do
|
6
|
+
with_instance_of(klass) do |record|
|
7
|
+
assert_raises(ActiveRecord::RecordNotFound) do
|
8
|
+
klass.where("1 = 2").find record.friendly_id
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
test "should be findable by friendly id" do
|
14
|
+
with_instance_of(klass) {|record| assert klass.find record.friendly_id}
|
15
|
+
end
|
16
|
+
|
17
|
+
test "should be findable by id as integer" do
|
18
|
+
with_instance_of(klass) {|record| assert klass.find record.id.to_i}
|
19
|
+
end
|
20
|
+
|
21
|
+
test "should be findable by id as string" do
|
22
|
+
with_instance_of(klass) {|record| assert klass.find record.id.to_s}
|
23
|
+
end
|
24
|
+
|
25
|
+
test "should be findable by numeric friendly_id" do
|
26
|
+
with_instance_of(klass, :name => "206") {|record| assert klass.find record.friendly_id}
|
27
|
+
end
|
28
|
+
|
29
|
+
test "to_param should return the friendly_id" do
|
30
|
+
with_instance_of(klass) {|record| assert_equal record.friendly_id, record.to_param}
|
31
|
+
end
|
32
|
+
|
33
|
+
test "should be findable by themselves" do
|
34
|
+
with_instance_of(klass) {|record| assert_equal record, klass.find(record)}
|
35
|
+
end
|
36
|
+
|
37
|
+
test "updating record's other values should not change the friendly_id" do
|
38
|
+
with_instance_of klass do |record|
|
39
|
+
old = record.friendly_id
|
40
|
+
record.update_attributes! :active => false
|
41
|
+
assert klass.find old
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
test "instances found by a single id should not be read-only" do
|
46
|
+
with_instance_of(klass) {|record| assert !klass.find(record.friendly_id).readonly?}
|
47
|
+
end
|
48
|
+
|
49
|
+
test "failing finds with unfriendly_id should raise errors normally" do
|
50
|
+
assert_raises(ActiveRecord::RecordNotFound) {klass.find 0}
|
51
|
+
end
|
52
|
+
|
53
|
+
test "should return numeric id if the friendly_id is nil" do
|
54
|
+
with_instance_of(klass) do |record|
|
55
|
+
record.expects(:friendly_id).returns(nil)
|
56
|
+
assert_equal record.id.to_s, record.to_param
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
test "should return numeric id if the friendly_id is an empty string" do
|
61
|
+
with_instance_of(klass) do |record|
|
62
|
+
record.expects(:friendly_id).returns("")
|
63
|
+
assert_equal record.id.to_s, record.to_param
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
test "should return numeric id if the friendly_id is blank" do
|
68
|
+
with_instance_of(klass) do |record|
|
69
|
+
record.expects(:friendly_id).returns(" ")
|
70
|
+
assert_equal record.id.to_s, record.to_param
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
@@ -0,0 +1,111 @@
|
|
1
|
+
require File.expand_path("../helper.rb", __FILE__)
|
2
|
+
|
3
|
+
Journalist, Article = 2.times.map do
|
4
|
+
Class.new(ActiveRecord::Base) do
|
5
|
+
include FriendlyId::Slugged
|
6
|
+
has_friendly_id :name
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class SluggedTest < MiniTest::Unit::TestCase
|
11
|
+
|
12
|
+
include FriendlyId::Test
|
13
|
+
include FriendlyId::Test::Shared
|
14
|
+
|
15
|
+
def klass
|
16
|
+
Journalist
|
17
|
+
end
|
18
|
+
|
19
|
+
test "configuration should have a sequence_separator" do
|
20
|
+
assert !klass.friendly_id_config.sequence_separator.empty?
|
21
|
+
end
|
22
|
+
|
23
|
+
test "should make a new slug if the friendly_id method value has changed" do
|
24
|
+
with_instance_of klass do |record|
|
25
|
+
record.name = "Changed Value"
|
26
|
+
record.save!
|
27
|
+
assert_equal "changed-value", record.slug
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
test "should increment the slug sequence for duplicate friendly ids" do
|
32
|
+
with_instance_of klass do |record|
|
33
|
+
record2 = klass.create! :name => record.name
|
34
|
+
assert record2.friendly_id.match(/2\z/)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
test "should not add slug sequence on update after other conflicting slugs were added" do
|
39
|
+
with_instance_of klass do |record|
|
40
|
+
old = record.friendly_id
|
41
|
+
record2 = klass.create! :name => record.name
|
42
|
+
record.save!
|
43
|
+
record.reload
|
44
|
+
assert_equal old, record.to_param
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
class SlugSequencerTest < MiniTest::Unit::TestCase
|
50
|
+
|
51
|
+
include FriendlyId::Test
|
52
|
+
|
53
|
+
test "should quote column names" do
|
54
|
+
klass = Class.new(ActiveRecord::Base)
|
55
|
+
klass.table_name = "journalists"
|
56
|
+
klass.send :include, FriendlyId::Slugged
|
57
|
+
klass.has_friendly_id :name, :slug_column => "strange name"
|
58
|
+
begin
|
59
|
+
with_instance_of(klass) {|record| assert klass.find(record.friendly_id)}
|
60
|
+
rescue ActiveRecord::StatementInvalid
|
61
|
+
flunk "column name was not quoted"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
class SlugSeparatorTest < MiniTest::Unit::TestCase
|
67
|
+
|
68
|
+
include FriendlyId::Test
|
69
|
+
|
70
|
+
class Journalist < ActiveRecord::Base
|
71
|
+
include FriendlyId::Slugged
|
72
|
+
has_friendly_id :name, :sequence_separator => ":"
|
73
|
+
end
|
74
|
+
|
75
|
+
def klass
|
76
|
+
Journalist
|
77
|
+
end
|
78
|
+
|
79
|
+
test "should increment sequence with configured sequence separator" do
|
80
|
+
with_instance_of klass do |record|
|
81
|
+
record2 = klass.create! :name => record.name
|
82
|
+
assert record2.friendly_id.match(/:2\z/)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
test "should detect when a sequenced slug has changed" do
|
87
|
+
with_instance_of klass do |record|
|
88
|
+
record2 = klass.create! :name => record.name
|
89
|
+
assert !record2.slug_sequencer.slug_changed?
|
90
|
+
record2.name = "hello world"
|
91
|
+
assert record2.slug_sequencer.slug_changed?
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
class SluggedRegressionsTest < MiniTest::Unit::TestCase
|
97
|
+
include FriendlyId::Test
|
98
|
+
|
99
|
+
def klass
|
100
|
+
Journalist
|
101
|
+
end
|
102
|
+
|
103
|
+
test "should increment the slug sequence for duplicate friendly ids beyond 10" do
|
104
|
+
with_instance_of klass do |record|
|
105
|
+
(2..12).each do |i|
|
106
|
+
r = klass.create! :name => record.name
|
107
|
+
assert r.friendly_id.match(/#{i}\z/)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
metadata
ADDED
@@ -0,0 +1,163 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: friendly_id4
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: 6
|
5
|
+
version: 4.0.0.beta1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Norman Clarke
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-07-14 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activerecord
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ~>
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "3.0"
|
24
|
+
type: :development
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: sqlite3
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: "1.3"
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id002
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: cutest
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 1.1.2
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id003
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: ffaker
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
type: :development
|
58
|
+
version_requirements: *id004
|
59
|
+
- !ruby/object:Gem::Dependency
|
60
|
+
name: maruku
|
61
|
+
prerelease: false
|
62
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: "0"
|
68
|
+
type: :development
|
69
|
+
version_requirements: *id005
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: yard
|
72
|
+
prerelease: false
|
73
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: "0"
|
79
|
+
type: :development
|
80
|
+
version_requirements: *id006
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
name: mocha
|
83
|
+
prerelease: false
|
84
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: "0"
|
90
|
+
type: :development
|
91
|
+
version_requirements: *id007
|
92
|
+
description: " FriendlyId is the \"Swiss Army bulldozer\" of slugging and permalink plugins\n for Ruby on Rails. It allows you to create pretty URL's and work with\n human-friendly strings as if they were numeric ids for ActiveRecord models.\n"
|
93
|
+
email:
|
94
|
+
- norman@njclarke.com
|
95
|
+
executables: []
|
96
|
+
|
97
|
+
extensions: []
|
98
|
+
|
99
|
+
extra_rdoc_files: []
|
100
|
+
|
101
|
+
files:
|
102
|
+
- .gitignore
|
103
|
+
- Gemfile
|
104
|
+
- Guide.md
|
105
|
+
- README.md
|
106
|
+
- Rakefile
|
107
|
+
- WhatsNew.md
|
108
|
+
- bench.rb
|
109
|
+
- friendly_id.gemspec
|
110
|
+
- lib/friendly_id.rb
|
111
|
+
- lib/friendly_id/base.rb
|
112
|
+
- lib/friendly_id/configuration.rb
|
113
|
+
- lib/friendly_id/finder_methods.rb
|
114
|
+
- lib/friendly_id/history.rb
|
115
|
+
- lib/friendly_id/migration.rb
|
116
|
+
- lib/friendly_id/model.rb
|
117
|
+
- lib/friendly_id/object_utils.rb
|
118
|
+
- lib/friendly_id/scoped.rb
|
119
|
+
- lib/friendly_id/slug.rb
|
120
|
+
- lib/friendly_id/slug_sequencer.rb
|
121
|
+
- lib/friendly_id/slugged.rb
|
122
|
+
- lib/friendly_id/version.rb
|
123
|
+
- lib/generators/friendly_id_generator.rb
|
124
|
+
- test/config/mysql.yml
|
125
|
+
- test/config/postgres.yml
|
126
|
+
- test/config/sqlite3.yml
|
127
|
+
- test/core_test.rb
|
128
|
+
- test/helper.rb
|
129
|
+
- test/history_test.rb
|
130
|
+
- test/object_utils_test.rb
|
131
|
+
- test/schema.rb
|
132
|
+
- test/scoped_test.rb
|
133
|
+
- test/shared.rb
|
134
|
+
- test/slugged_test.rb
|
135
|
+
homepage: http://norman.github.com/friendly_id
|
136
|
+
licenses: []
|
137
|
+
|
138
|
+
post_install_message:
|
139
|
+
rdoc_options: []
|
140
|
+
|
141
|
+
require_paths:
|
142
|
+
- lib
|
143
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
144
|
+
none: false
|
145
|
+
requirements:
|
146
|
+
- - ">="
|
147
|
+
- !ruby/object:Gem::Version
|
148
|
+
version: "0"
|
149
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
150
|
+
none: false
|
151
|
+
requirements:
|
152
|
+
- - ">"
|
153
|
+
- !ruby/object:Gem::Version
|
154
|
+
version: 1.3.1
|
155
|
+
requirements: []
|
156
|
+
|
157
|
+
rubyforge_project: friendly_id
|
158
|
+
rubygems_version: 1.8.5
|
159
|
+
signing_key:
|
160
|
+
specification_version: 3
|
161
|
+
summary: A comprehensive slugging and pretty-URL plugin.
|
162
|
+
test_files: []
|
163
|
+
|