friendly_id 3.3.3.0 → 4.0.0.beta7
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +11 -0
- data/.travis.yml +24 -0
- data/.yardopts +4 -0
- data/Changelog.md +9 -10
- data/README.md +39 -48
- data/Rakefile +56 -58
- data/WhatsNew.md +95 -0
- data/bench.rb +63 -0
- data/friendly_id.gemspec +40 -0
- data/gemfiles/Gemfile.rails-3.0.rb +18 -0
- data/gemfiles/Gemfile.rails-3.0.rb.lock +52 -0
- data/gemfiles/Gemfile.rails-3.1.rb +18 -0
- data/gemfiles/Gemfile.rails-3.1.rb.lock +57 -0
- data/lib/friendly_id.rb +126 -80
- data/lib/friendly_id/active_record_adapter/relation.rb +10 -2
- data/lib/friendly_id/active_record_adapter/slugged_model.rb +3 -9
- data/lib/friendly_id/base.rb +132 -0
- data/lib/friendly_id/configuration.rb +65 -152
- data/lib/friendly_id/finder_methods.rb +20 -0
- data/lib/friendly_id/history.rb +88 -0
- data/lib/friendly_id/migration.rb +18 -0
- data/lib/friendly_id/model.rb +22 -0
- data/lib/friendly_id/object_utils.rb +40 -0
- data/lib/friendly_id/reserved.rb +46 -0
- data/lib/friendly_id/scoped.rb +131 -0
- data/lib/friendly_id/slug.rb +9 -0
- data/lib/friendly_id/slug_sequencer.rb +82 -0
- data/lib/friendly_id/slugged.rb +191 -76
- data/lib/friendly_id/version.rb +2 -2
- data/test/base_test.rb +54 -0
- data/test/configuration_test.rb +27 -0
- data/test/core_test.rb +30 -0
- data/test/databases.yml +19 -0
- data/test/helper.rb +88 -0
- data/test/history_test.rb +55 -0
- data/test/object_utils_test.rb +26 -0
- data/test/reserved_test.rb +26 -0
- data/test/schema.rb +59 -0
- data/test/scoped_test.rb +57 -0
- data/test/shared.rb +118 -0
- data/test/slugged_test.rb +83 -0
- data/test/sti_test.rb +48 -0
- metadata +110 -102
- data/Contributors.md +0 -46
- data/Guide.md +0 -626
- data/extras/README.txt +0 -3
- data/extras/bench.rb +0 -40
- data/extras/extras.rb +0 -38
- data/extras/prof.rb +0 -19
- data/extras/template-gem.rb +0 -26
- data/extras/template-plugin.rb +0 -28
- data/generators/friendly_id/friendly_id_generator.rb +0 -30
- data/generators/friendly_id/templates/create_slugs.rb +0 -18
- data/lib/tasks/friendly_id.rake +0 -19
- data/rails/init.rb +0 -2
- data/test/active_record_adapter/ar_test_helper.rb +0 -149
- data/test/active_record_adapter/basic_slugged_model_test.rb +0 -14
- data/test/active_record_adapter/cached_slug_test.rb +0 -76
- data/test/active_record_adapter/core.rb +0 -138
- data/test/active_record_adapter/custom_normalizer_test.rb +0 -20
- data/test/active_record_adapter/custom_table_name_test.rb +0 -22
- data/test/active_record_adapter/default_scope_test.rb +0 -30
- data/test/active_record_adapter/optimistic_locking_test.rb +0 -18
- data/test/active_record_adapter/scoped_model_test.rb +0 -129
- data/test/active_record_adapter/simple_test.rb +0 -76
- data/test/active_record_adapter/slug_test.rb +0 -34
- data/test/active_record_adapter/slugged.rb +0 -33
- data/test/active_record_adapter/slugged_status_test.rb +0 -28
- data/test/active_record_adapter/sti_test.rb +0 -22
- data/test/active_record_adapter/support/database.jdbcsqlite3.yml +0 -2
- data/test/active_record_adapter/support/database.mysql.yml +0 -4
- data/test/active_record_adapter/support/database.mysql2.yml +0 -4
- data/test/active_record_adapter/support/database.postgres.yml +0 -6
- data/test/active_record_adapter/support/database.sqlite3.yml +0 -2
- data/test/active_record_adapter/support/models.rb +0 -104
- data/test/active_record_adapter/tasks_test.rb +0 -82
- data/test/compatibility/ancestry/Gemfile.lock +0 -34
- data/test/friendly_id_test.rb +0 -96
- data/test/test_helper.rb +0 -13
@@ -1,33 +0,0 @@
|
|
1
|
-
require File.expand_path("../ar_test_helper", __FILE__)
|
2
|
-
|
3
|
-
module FriendlyId
|
4
|
-
module Test
|
5
|
-
module ActiveRecordAdapter
|
6
|
-
module Slugged
|
7
|
-
|
8
|
-
test "should allow eager loading of slugs" do
|
9
|
-
assert_nothing_raised do
|
10
|
-
klass.find(instance.friendly_id, :include => :slugs)
|
11
|
-
end
|
12
|
-
|
13
|
-
assert_nothing_raised do
|
14
|
-
klass.find(instance.friendly_id, :include => :slug)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def klass
|
19
|
-
Post
|
20
|
-
end
|
21
|
-
|
22
|
-
def other_class
|
23
|
-
District
|
24
|
-
end
|
25
|
-
|
26
|
-
def instance
|
27
|
-
@instance ||= klass.create! :name => "hello world"
|
28
|
-
end
|
29
|
-
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
require File.expand_path("../ar_test_helper", __FILE__)
|
2
|
-
|
3
|
-
|
4
|
-
module FriendlyId
|
5
|
-
module Test
|
6
|
-
module ActiveRecordAdapter
|
7
|
-
|
8
|
-
class StatusTest < ::Test::Unit::TestCase
|
9
|
-
|
10
|
-
include FriendlyId::Test::Status
|
11
|
-
include FriendlyId::Test::SluggedStatus
|
12
|
-
|
13
|
-
def klass
|
14
|
-
Post
|
15
|
-
end
|
16
|
-
|
17
|
-
def instance
|
18
|
-
@instance ||= klass.create! :name => "hello world"
|
19
|
-
end
|
20
|
-
|
21
|
-
def find_method
|
22
|
-
:find
|
23
|
-
end
|
24
|
-
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
@@ -1,22 +0,0 @@
|
|
1
|
-
require File.expand_path('../ar_test_helper', __FILE__)
|
2
|
-
|
3
|
-
module FriendlyId
|
4
|
-
module Test
|
5
|
-
module ActiveRecordAdapter
|
6
|
-
|
7
|
-
class StiTest < ::Test::Unit::TestCase
|
8
|
-
|
9
|
-
include FriendlyId::Test::Generic
|
10
|
-
include FriendlyId::Test::Slugged
|
11
|
-
include FriendlyId::Test::ActiveRecordAdapter::Slugged
|
12
|
-
include FriendlyId::Test::ActiveRecordAdapter::Core
|
13
|
-
|
14
|
-
def klass
|
15
|
-
Novel
|
16
|
-
end
|
17
|
-
|
18
|
-
end
|
19
|
-
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
@@ -1,104 +0,0 @@
|
|
1
|
-
class CreateSupportModels < ActiveRecord::Migration
|
2
|
-
def self.up
|
3
|
-
|
4
|
-
create_table :authors do |t|
|
5
|
-
t.string :name
|
6
|
-
end
|
7
|
-
|
8
|
-
create_table :blocks do |t|
|
9
|
-
t.string :name
|
10
|
-
t.string :note
|
11
|
-
end
|
12
|
-
|
13
|
-
create_table :books do |t|
|
14
|
-
t.string :name
|
15
|
-
t.string :type
|
16
|
-
t.string :note
|
17
|
-
end
|
18
|
-
|
19
|
-
create_table :cities do |t|
|
20
|
-
t.string :name
|
21
|
-
t.string :my_slug
|
22
|
-
t.integer :population
|
23
|
-
end
|
24
|
-
add_index :cities, :my_slug, :unique => true
|
25
|
-
|
26
|
-
create_table :regions do |t|
|
27
|
-
t.string :name
|
28
|
-
t.string :cached_slug
|
29
|
-
t.string :note
|
30
|
-
t.integer :lock_version, :null => false, :default => 0
|
31
|
-
end
|
32
|
-
add_index :regions, :cached_slug, :unique => true
|
33
|
-
|
34
|
-
create_table :countries do |t|
|
35
|
-
t.string :name
|
36
|
-
end
|
37
|
-
|
38
|
-
create_table :districts do |t|
|
39
|
-
t.string :name
|
40
|
-
t.string :note
|
41
|
-
t.string :cached_slug
|
42
|
-
end
|
43
|
-
add_index :districts, :cached_slug, :unique => true
|
44
|
-
|
45
|
-
create_table :events do |t|
|
46
|
-
t.string :name
|
47
|
-
t.datetime :event_date
|
48
|
-
end
|
49
|
-
|
50
|
-
create_table :houses do |t|
|
51
|
-
t.string :name
|
52
|
-
t.integer :user_id
|
53
|
-
end
|
54
|
-
|
55
|
-
create_table :legacy_table do |t|
|
56
|
-
t.string :name
|
57
|
-
t.string :note
|
58
|
-
end
|
59
|
-
|
60
|
-
create_table :people do |t|
|
61
|
-
t.string :name
|
62
|
-
t.string :note
|
63
|
-
end
|
64
|
-
|
65
|
-
create_table :posts do |t|
|
66
|
-
t.string :name
|
67
|
-
t.boolean :published
|
68
|
-
t.string :note
|
69
|
-
end
|
70
|
-
|
71
|
-
create_table :residents do |t|
|
72
|
-
t.string :name
|
73
|
-
t.integer :country_id
|
74
|
-
end
|
75
|
-
|
76
|
-
create_table :tourists do |t|
|
77
|
-
t.string :name
|
78
|
-
t.integer :country_id
|
79
|
-
t.string :cached_slug
|
80
|
-
end
|
81
|
-
|
82
|
-
create_table :users do |t|
|
83
|
-
t.string :name
|
84
|
-
end
|
85
|
-
add_index :users, :name, :unique => true
|
86
|
-
|
87
|
-
create_table :sites do |t|
|
88
|
-
t.string :name
|
89
|
-
t.integer :owner_id
|
90
|
-
t.string :owner_type
|
91
|
-
end
|
92
|
-
|
93
|
-
create_table :companies do |t|
|
94
|
-
t.string :name
|
95
|
-
end
|
96
|
-
|
97
|
-
create_table :unfriendlies do |t|
|
98
|
-
t.string :name
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
def self.down
|
103
|
-
end
|
104
|
-
end
|
@@ -1,82 +0,0 @@
|
|
1
|
-
require "friendly_id/active_record_adapter/tasks"
|
2
|
-
require File.expand_path('../ar_test_helper', __FILE__)
|
3
|
-
|
4
|
-
class TasksTest < Test::Unit::TestCase
|
5
|
-
|
6
|
-
def teardown
|
7
|
-
ENV["MODEL"] = nil
|
8
|
-
[City, District, Post, Slug].map(&:delete_all)
|
9
|
-
end
|
10
|
-
|
11
|
-
test "should make slugs" do
|
12
|
-
City.create! :name => "Nairobi"
|
13
|
-
City.create! :name => "Buenos Aires"
|
14
|
-
Slug.delete_all
|
15
|
-
ENV["MODEL"] = "City"
|
16
|
-
FriendlyId::TaskRunner.new.make_slugs
|
17
|
-
assert_equal 2, Slug.count
|
18
|
-
end
|
19
|
-
|
20
|
-
test "should admit lower case, plural model names" do
|
21
|
-
ENV["MODEL"] = "cities"
|
22
|
-
assert_equal City, FriendlyId::TaskRunner.new.klass
|
23
|
-
end
|
24
|
-
|
25
|
-
test "make_slugs should raise error if no model given" do
|
26
|
-
assert_raise(RuntimeError) { FriendlyId::TaskRunner.new.make_slugs }
|
27
|
-
end
|
28
|
-
|
29
|
-
test "make_slugs should raise error if class doesn't use FriendlyId" do
|
30
|
-
ENV["MODEL"] = "String"
|
31
|
-
assert_raise(RuntimeError) { FriendlyId::TaskRunner.new.make_slugs }
|
32
|
-
end
|
33
|
-
|
34
|
-
test"delete_slugs delete only slugs for the specified model" do
|
35
|
-
Post.create! :name => "Slugs Considered Harmful"
|
36
|
-
City.create! :name => "Buenos Aires"
|
37
|
-
ENV["MODEL"] = "city"
|
38
|
-
FriendlyId::TaskRunner.new.delete_slugs
|
39
|
-
assert_equal 1, Slug.count
|
40
|
-
end
|
41
|
-
|
42
|
-
test "delete_slugs should set the cached_slug column to NULL" do
|
43
|
-
ENV["MODEL"] = "district"
|
44
|
-
District.create! :name => "Garment"
|
45
|
-
FriendlyId::TaskRunner.new.delete_slugs
|
46
|
-
assert_nil District.first.cached_slug
|
47
|
-
end
|
48
|
-
|
49
|
-
|
50
|
-
test "delete_old_slugs should delete slugs older than 45 days by default" do
|
51
|
-
set_up_old_slugs
|
52
|
-
FriendlyId::TaskRunner.new.delete_old_slugs
|
53
|
-
assert_equal 2, Slug.count
|
54
|
-
end
|
55
|
-
|
56
|
-
test "delete_old_slugs should respect the days argument" do
|
57
|
-
set_up_old_slugs
|
58
|
-
ENV["DAYS"] = "100"
|
59
|
-
FriendlyId::TaskRunner.new.delete_old_slugs
|
60
|
-
assert_equal 3, Slug.count
|
61
|
-
end
|
62
|
-
|
63
|
-
test "delete_old_slugs should respect the class argument" do
|
64
|
-
set_up_old_slugs
|
65
|
-
ENV["MODEL"] = "post"
|
66
|
-
FriendlyId::TaskRunner.new.delete_old_slugs
|
67
|
-
assert_equal 3, Slug.count
|
68
|
-
end
|
69
|
-
|
70
|
-
private
|
71
|
-
|
72
|
-
def set_up_old_slugs
|
73
|
-
Post.create! :name => "Slugs Considered Harmful"
|
74
|
-
city = City.create! :name => "Buenos Aires"
|
75
|
-
City.connection.execute "UPDATE slugs SET created_at = '%s' WHERE id = %d" % [
|
76
|
-
45.days.ago.strftime("%Y-%m-%d"), city.slug.id
|
77
|
-
]
|
78
|
-
city.update_attributes :name => "Ciudad de Buenos Aires"
|
79
|
-
assert_equal 3, Slug.count
|
80
|
-
end
|
81
|
-
|
82
|
-
end
|
@@ -1,34 +0,0 @@
|
|
1
|
-
GEM
|
2
|
-
remote: http://rubygems.org/
|
3
|
-
specs:
|
4
|
-
activemodel (3.0.10)
|
5
|
-
activesupport (= 3.0.10)
|
6
|
-
builder (~> 2.1.2)
|
7
|
-
i18n (~> 0.5.0)
|
8
|
-
activerecord (3.0.10)
|
9
|
-
activemodel (= 3.0.10)
|
10
|
-
activesupport (= 3.0.10)
|
11
|
-
arel (~> 2.0.10)
|
12
|
-
tzinfo (~> 0.3.23)
|
13
|
-
activesupport (3.0.10)
|
14
|
-
ancestry (1.2.4)
|
15
|
-
activerecord (>= 2.2.2)
|
16
|
-
arel (2.0.10)
|
17
|
-
builder (2.1.2)
|
18
|
-
i18n (0.5.0)
|
19
|
-
minitest (2.4.0)
|
20
|
-
mocha (0.9.12)
|
21
|
-
rake (0.9.2)
|
22
|
-
sqlite3 (1.3.4)
|
23
|
-
tzinfo (0.3.29)
|
24
|
-
|
25
|
-
PLATFORMS
|
26
|
-
ruby
|
27
|
-
|
28
|
-
DEPENDENCIES
|
29
|
-
activerecord (= 3.0.10)
|
30
|
-
ancestry
|
31
|
-
minitest (~> 2.4.0)
|
32
|
-
mocha (~> 0.9.12)
|
33
|
-
rake
|
34
|
-
sqlite3 (~> 1.3.4)
|
data/test/friendly_id_test.rb
DELETED
@@ -1,96 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
require File.expand_path('../test_helper', __FILE__)
|
3
|
-
|
4
|
-
module FriendlyId
|
5
|
-
module Test
|
6
|
-
|
7
|
-
class ConfigurationTest < ::Test::Unit::TestCase
|
8
|
-
test "should validate sequence separator name" do
|
9
|
-
["-", " ", "\n", "\t"].each do |string|
|
10
|
-
assert_raise ArgumentError do
|
11
|
-
Configuration.new(NilClass, :hello, :sequence_separator => string)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
test "should validate cached slug name" do
|
17
|
-
["slug", "slugs", " "].each do |string|
|
18
|
-
assert_raise ArgumentError do
|
19
|
-
Configuration.new(NilClass, :hello, :cache_column => string)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
class SlugStringTest < ::Test::Unit::TestCase
|
26
|
-
test "should not transliterate by default" do
|
27
|
-
s = SlugString.new("über")
|
28
|
-
assert_equal "über", s.normalize_for!(Configuration.new(nil, :name))
|
29
|
-
end
|
30
|
-
|
31
|
-
test "should transliterate if specified" do
|
32
|
-
s = SlugString.new("über")
|
33
|
-
options = {:approximate_ascii => true}
|
34
|
-
assert_equal "uber", s.normalize_for!(Configuration.new(nil, :name, options))
|
35
|
-
end
|
36
|
-
|
37
|
-
test "should strip non-ascii if specified" do
|
38
|
-
s = SlugString.new("über")
|
39
|
-
options = {:strip_non_ascii => true}
|
40
|
-
assert_equal "ber", s.normalize_for!(Configuration.new(nil, :name, options))
|
41
|
-
end
|
42
|
-
|
43
|
-
test "should use transliterations if given" do
|
44
|
-
s = SlugString.new("über")
|
45
|
-
options = {:approximate_ascii => true, :ascii_approximation_options => :german}
|
46
|
-
assert_equal "ueber", s.normalize_for!(Configuration.new(nil, :name, options))
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
class FriendlyIdTest < ::Test::Unit::TestCase
|
51
|
-
test "should parse a friendly_id name and sequence" do
|
52
|
-
assert_equal ["test", 2], "test--2".parse_friendly_id
|
53
|
-
end
|
54
|
-
|
55
|
-
test "should parse a friendly_id name and a double digit sequence" do
|
56
|
-
assert_equal ["test", 12], "test--12".parse_friendly_id
|
57
|
-
end
|
58
|
-
|
59
|
-
test "should parse with a default sequence of 1" do
|
60
|
-
assert_equal ["test", 1], "test".parse_friendly_id
|
61
|
-
end
|
62
|
-
|
63
|
-
test "should be parseable with a custom separator" do
|
64
|
-
assert_equal ["test", 2], "test:2".parse_friendly_id(":")
|
65
|
-
end
|
66
|
-
|
67
|
-
test "should be parseable with a custom separator and a double digit sequence" do
|
68
|
-
assert_equal ["test", 12], "test:12".parse_friendly_id(":")
|
69
|
-
end
|
70
|
-
|
71
|
-
test "should parse when default sequence separator occurs in friendly_id name" do
|
72
|
-
assert_equal ["test--test", 2], "test--test--2".parse_friendly_id
|
73
|
-
end
|
74
|
-
|
75
|
-
test "should parse when custom sequence separator occurs in friendly_id name" do
|
76
|
-
assert_equal ["test:test", 2], "test:test:2".parse_friendly_id(":")
|
77
|
-
end
|
78
|
-
|
79
|
-
test "should parse when sequence separator and number occur in friendly_id name" do
|
80
|
-
assert_equal ["test--2--test", 1], "test--2--test".parse_friendly_id
|
81
|
-
end
|
82
|
-
|
83
|
-
test "should parse when sequence separator, number and sequence occur in friendly_id name" do
|
84
|
-
assert_equal ["test--2--test", 2], "test--2--test--2".parse_friendly_id
|
85
|
-
end
|
86
|
-
|
87
|
-
test "should parse when double digit sequence separator, number and sequence occur in friendly_id name" do
|
88
|
-
assert_equal ["test--2--test", 12], "test--2--test--12".parse_friendly_id
|
89
|
-
end
|
90
|
-
|
91
|
-
test "should parse with a separator and no sequence" do
|
92
|
-
assert_equal ["test", 1], "test--".parse_friendly_id
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|
data/test/test_helper.rb
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
$:.unshift File.expand_path("../lib", File.dirname(__FILE__))
|
2
|
-
$:.unshift File.expand_path(File.dirname(__FILE__))
|
3
|
-
$:.uniq!
|
4
|
-
|
5
|
-
$KCODE = "UTF8" if RUBY_VERSION < "1.9"
|
6
|
-
$VERBOSE = false
|
7
|
-
require "rubygems"
|
8
|
-
require "bundler/setup"
|
9
|
-
require "test/unit"
|
10
|
-
require "mocha"
|
11
|
-
require "active_support"
|
12
|
-
require "friendly_id"
|
13
|
-
require "friendly_id/test"
|