friendly_id 5.4.2 → 5.5.1
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.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/.github/dependabot.yml +6 -0
- data/.github/workflows/test.yml +38 -25
- data/.yardopts +2 -0
- data/Changelog.md +10 -0
- data/Gemfile +9 -13
- data/README.md +21 -0
- data/Rakefile +24 -27
- data/bench.rb +30 -27
- data/certs/parndt.pem +25 -23
- data/friendly_id.gemspec +26 -29
- data/gemfiles/Gemfile.rails-5.2.rb +11 -16
- data/gemfiles/Gemfile.rails-6.0.rb +11 -16
- data/gemfiles/Gemfile.rails-6.1.rb +22 -0
- data/gemfiles/Gemfile.rails-7.0.rb +22 -0
- data/guide.rb +13 -6
- data/lib/friendly_id/base.rb +59 -60
- data/lib/friendly_id/candidates.rb +9 -11
- data/lib/friendly_id/configuration.rb +6 -7
- data/lib/friendly_id/finder_methods.rb +26 -11
- data/lib/friendly_id/finders.rb +66 -66
- data/lib/friendly_id/history.rb +62 -63
- data/lib/friendly_id/initializer.rb +4 -4
- data/lib/friendly_id/migration.rb +6 -6
- data/lib/friendly_id/object_utils.rb +2 -2
- data/lib/friendly_id/reserved.rb +30 -32
- data/lib/friendly_id/scoped.rb +99 -102
- data/lib/friendly_id/sequentially_slugged/calculator.rb +69 -0
- data/lib/friendly_id/sequentially_slugged.rb +17 -64
- data/lib/friendly_id/simple_i18n.rb +78 -69
- data/lib/friendly_id/slug.rb +1 -2
- data/lib/friendly_id/slug_generator.rb +1 -3
- data/lib/friendly_id/slugged.rb +236 -238
- data/lib/friendly_id/version.rb +1 -1
- data/lib/friendly_id.rb +47 -49
- data/lib/generators/friendly_id_generator.rb +9 -9
- data/test/base_test.rb +10 -13
- data/test/benchmarks/finders.rb +28 -26
- data/test/benchmarks/object_utils.rb +13 -13
- data/test/candidates_test.rb +17 -18
- data/test/configuration_test.rb +7 -11
- data/test/core_test.rb +1 -2
- data/test/databases.yml +4 -3
- data/test/finders_test.rb +52 -5
- data/test/generator_test.rb +16 -26
- data/test/helper.rb +31 -24
- data/test/history_test.rb +70 -74
- data/test/numeric_slug_test.rb +4 -4
- data/test/object_utils_test.rb +0 -2
- data/test/reserved_test.rb +9 -11
- data/test/schema.rb +5 -4
- data/test/scoped_test.rb +18 -20
- data/test/sequentially_slugged_test.rb +65 -50
- data/test/shared.rb +15 -16
- data/test/simple_i18n_test.rb +22 -12
- data/test/slugged_test.rb +102 -121
- data/test/sti_test.rb +19 -21
- data.tar.gz.sig +0 -0
- metadata +37 -32
- metadata.gz.sig +0 -0
data/test/base_test.rb
CHANGED
@@ -21,14 +21,13 @@ class CoreTest < TestCaseClass
|
|
21
21
|
klass = Class.new(ActiveRecord::Base) do
|
22
22
|
self.abstract_class = true
|
23
23
|
extend FriendlyId
|
24
|
-
friendly_id :foo, :
|
24
|
+
friendly_id :foo, use: :slugged, slug_column: :bar
|
25
25
|
end
|
26
26
|
assert klass < FriendlyId::Slugged
|
27
27
|
assert_equal :foo, klass.friendly_id_config.base
|
28
28
|
assert_equal :bar, klass.friendly_id_config.slug_column
|
29
29
|
end
|
30
30
|
|
31
|
-
|
32
31
|
test "friendly_id should accept a block" do
|
33
32
|
klass = Class.new(ActiveRecord::Base) do
|
34
33
|
self.abstract_class = true
|
@@ -56,17 +55,15 @@ class CoreTest < TestCaseClass
|
|
56
55
|
end
|
57
56
|
|
58
57
|
test "should allow defaults to be set via a block" do
|
59
|
-
|
60
|
-
|
61
|
-
config.base = :foo
|
62
|
-
end
|
63
|
-
klass = Class.new(ActiveRecord::Base) do
|
64
|
-
self.abstract_class = true
|
65
|
-
extend FriendlyId
|
66
|
-
end
|
67
|
-
assert_equal :foo, klass.friendly_id_config.base
|
68
|
-
ensure
|
69
|
-
FriendlyId.instance_variable_set :@defaults, nil
|
58
|
+
FriendlyId.defaults do |config|
|
59
|
+
config.base = :foo
|
70
60
|
end
|
61
|
+
klass = Class.new(ActiveRecord::Base) do
|
62
|
+
self.abstract_class = true
|
63
|
+
extend FriendlyId
|
64
|
+
end
|
65
|
+
assert_equal :foo, klass.friendly_id_config.base
|
66
|
+
ensure
|
67
|
+
FriendlyId.instance_variable_set :@defaults, nil
|
71
68
|
end
|
72
69
|
end
|
data/test/benchmarks/finders.rb
CHANGED
@@ -14,7 +14,11 @@ require "ffaker"
|
|
14
14
|
N = 50000
|
15
15
|
|
16
16
|
def transaction
|
17
|
-
ActiveRecord::Base.transaction
|
17
|
+
ActiveRecord::Base.transaction do
|
18
|
+
yield
|
19
|
+
|
20
|
+
raise ActiveRecord::Rollback
|
21
|
+
end
|
18
22
|
end
|
19
23
|
|
20
24
|
class Array
|
@@ -27,62 +31,60 @@ Book = Class.new ActiveRecord::Base
|
|
27
31
|
|
28
32
|
class Journalist < ActiveRecord::Base
|
29
33
|
extend FriendlyId
|
30
|
-
friendly_id :name, :
|
34
|
+
friendly_id :name, use: :slugged
|
31
35
|
end
|
32
36
|
|
33
37
|
class Manual < ActiveRecord::Base
|
34
38
|
extend FriendlyId
|
35
|
-
friendly_id :name, :
|
39
|
+
friendly_id :name, use: :history
|
36
40
|
end
|
37
41
|
|
38
42
|
class Restaurant < ActiveRecord::Base
|
39
43
|
extend FriendlyId
|
40
|
-
friendly_id :name, :
|
44
|
+
friendly_id :name, use: :finders
|
41
45
|
end
|
42
46
|
|
43
|
-
|
44
|
-
BOOKS = []
|
47
|
+
BOOKS = []
|
45
48
|
JOURNALISTS = []
|
46
|
-
MANUALS
|
49
|
+
MANUALS = []
|
47
50
|
RESTAURANTS = []
|
48
51
|
|
49
52
|
100.times do
|
50
53
|
name = FFaker::Name.name
|
51
|
-
BOOKS
|
52
|
-
JOURNALISTS << (Journalist.create! :
|
53
|
-
MANUALS
|
54
|
-
RESTAURANTS << (Restaurant.create! :
|
54
|
+
BOOKS << (Book.create! name: name).id
|
55
|
+
JOURNALISTS << (Journalist.create! name: name).friendly_id
|
56
|
+
MANUALS << (Manual.create! name: name).friendly_id
|
57
|
+
RESTAURANTS << (Restaurant.create! name: name).friendly_id
|
55
58
|
end
|
56
59
|
|
57
60
|
ActiveRecord::Base.connection.execute "UPDATE manuals SET slug = NULL"
|
58
61
|
|
59
62
|
Benchmark.bmbm do |x|
|
60
|
-
x.report
|
61
|
-
N.times {Book.where(:
|
63
|
+
x.report "ActiveRecord: where.first" do
|
64
|
+
N.times { Book.where(id: BOOKS.rand).first }
|
62
65
|
end
|
63
66
|
|
64
|
-
x.report
|
65
|
-
N.times {Book.where(:
|
67
|
+
x.report "ActiveRecord: where.take" do
|
68
|
+
N.times { Book.where(id: BOOKS.rand).take }
|
66
69
|
end
|
67
70
|
|
68
|
-
x.report
|
69
|
-
N.times {Book.find BOOKS.rand}
|
71
|
+
x.report "ActiveRecord: find" do
|
72
|
+
N.times { Book.find BOOKS.rand }
|
70
73
|
end
|
71
74
|
|
72
|
-
x.report
|
73
|
-
N.times {Book.find_by(:
|
75
|
+
x.report "ActiveRecord: find_by(:id)" do
|
76
|
+
N.times { Book.find_by(id: BOOKS.rand) }
|
74
77
|
end
|
75
78
|
|
76
|
-
x.report
|
77
|
-
N.times {Restaurant.find_by(:
|
79
|
+
x.report "ActiveRecord: find_by(:slug)" do
|
80
|
+
N.times { Restaurant.find_by(slug: RESTAURANTS.rand) }
|
78
81
|
end
|
79
82
|
|
80
|
-
x.report
|
81
|
-
N.times {Restaurant.find RESTAURANTS.rand}
|
83
|
+
x.report "FriendlyId: find (in-table slug w/ finders)" do
|
84
|
+
N.times { Restaurant.find RESTAURANTS.rand }
|
82
85
|
end
|
83
86
|
|
84
|
-
x.report
|
85
|
-
N.times {Restaurant.friendly.find RESTAURANTS.rand}
|
87
|
+
x.report "FriendlyId: friendly.find (in-table slug)" do
|
88
|
+
N.times { Restaurant.friendly.find RESTAURANTS.rand }
|
86
89
|
end
|
87
|
-
|
88
90
|
end
|
@@ -28,7 +28,7 @@ Book = Class.new ActiveRecord::Base
|
|
28
28
|
|
29
29
|
test_integer = 123
|
30
30
|
test_active_record_object = Book.new
|
31
|
-
test_hash = {:
|
31
|
+
test_hash = {name: "joe"}
|
32
32
|
test_nil = nil
|
33
33
|
test_numeric_string = "123"
|
34
34
|
test_string = "abc123"
|
@@ -36,21 +36,21 @@ test_string = "abc123"
|
|
36
36
|
N = 5_000_000
|
37
37
|
|
38
38
|
Benchmark.bmbm do |x|
|
39
|
-
x.report(
|
40
|
-
x.report(
|
39
|
+
x.report("integer friendly_id?") { N.times { test_integer.friendly_id? } }
|
40
|
+
x.report("integer unfriendly_id?") { N.times { test_integer.unfriendly_id? } }
|
41
41
|
|
42
|
-
x.report(
|
43
|
-
x.report(
|
42
|
+
x.report("AR::Base friendly_id?") { N.times { test_active_record_object.friendly_id? } }
|
43
|
+
x.report("AR::Base unfriendly_id?") { N.times { test_active_record_object.unfriendly_id? } }
|
44
44
|
|
45
|
-
x.report(
|
46
|
-
x.report(
|
45
|
+
x.report("hash friendly_id?") { N.times { test_hash.friendly_id? } }
|
46
|
+
x.report("hash unfriendly_id?") { N.times { test_hash.unfriendly_id? } }
|
47
47
|
|
48
|
-
x.report(
|
49
|
-
x.report(
|
48
|
+
x.report("nil friendly_id?") { N.times { test_nil.friendly_id? } }
|
49
|
+
x.report("nil unfriendly_id?") { N.times { test_nil.unfriendly_id? } }
|
50
50
|
|
51
|
-
x.report(
|
52
|
-
x.report(
|
51
|
+
x.report("numeric string friendly_id?") { N.times { test_numeric_string.friendly_id? } }
|
52
|
+
x.report("numeric string unfriendly_id?") { N.times { test_numeric_string.unfriendly_id? } }
|
53
53
|
|
54
|
-
x.report(
|
55
|
-
x.report(
|
54
|
+
x.report("test_string friendly_id?") { N.times { test_string.friendly_id? } }
|
55
|
+
x.report("test_string unfriendly_id?") { N.times { test_string.unfriendly_id? } }
|
56
56
|
end
|
data/test/candidates_test.rb
CHANGED
@@ -1,9 +1,16 @@
|
|
1
1
|
require "helper"
|
2
2
|
|
3
3
|
class CandidatesTest < TestCaseClass
|
4
|
-
|
5
4
|
include FriendlyId::Test
|
6
5
|
|
6
|
+
class Airport
|
7
|
+
def initialize(code)
|
8
|
+
@code = code
|
9
|
+
end
|
10
|
+
attr_reader :code
|
11
|
+
alias_method :to_s, :code
|
12
|
+
end
|
13
|
+
|
7
14
|
class City < ActiveRecord::Base
|
8
15
|
extend FriendlyId
|
9
16
|
friendly_id :slug_candidates, use: :slugged
|
@@ -16,8 +23,8 @@ class CandidatesTest < TestCaseClass
|
|
16
23
|
|
17
24
|
def with_instances_of(klass = model_class, &block)
|
18
25
|
transaction do
|
19
|
-
city1 = klass.create! :
|
20
|
-
city2 = klass.create! :
|
26
|
+
city1 = klass.create! name: "New York", code: "JFK"
|
27
|
+
city2 = klass.create! name: "New York", code: "EWR"
|
21
28
|
yield city1, city2
|
22
29
|
end
|
23
30
|
end
|
@@ -26,7 +33,7 @@ class CandidatesTest < TestCaseClass
|
|
26
33
|
test "resolves conflict with candidate" do
|
27
34
|
with_instances do |city1, city2|
|
28
35
|
assert_equal "new-york", city1.slug
|
29
|
-
assert_match(/\Anew-york-([a-z0-9]
|
36
|
+
assert_match(/\Anew-york-([a-z0-9]+-){4}[a-z0-9]+\z/, city2.slug)
|
30
37
|
end
|
31
38
|
end
|
32
39
|
|
@@ -37,7 +44,7 @@ class CandidatesTest < TestCaseClass
|
|
37
44
|
end
|
38
45
|
end
|
39
46
|
with_instances_of klass do |_, city|
|
40
|
-
assert_match(/\Anew-york-([a-z0-9]
|
47
|
+
assert_match(/\Anew-york-([a-z0-9]+-){4}[a-z0-9]+\z/, city.slug)
|
41
48
|
end
|
42
49
|
end
|
43
50
|
|
@@ -59,7 +66,7 @@ class CandidatesTest < TestCaseClass
|
|
59
66
|
end
|
60
67
|
end
|
61
68
|
with_instances_of klass do |_, city|
|
62
|
-
assert_match(/\Anew-york-([a-z0-9]
|
69
|
+
assert_match(/\Anew-york-([a-z0-9]+-){4}[a-z0-9]+\z/, city.slug)
|
63
70
|
end
|
64
71
|
end
|
65
72
|
|
@@ -70,7 +77,7 @@ class CandidatesTest < TestCaseClass
|
|
70
77
|
end
|
71
78
|
end
|
72
79
|
with_instances_of klass do |_, city|
|
73
|
-
assert_match(/\Anew-york-([a-z0-9]
|
80
|
+
assert_match(/\Anew-york-([a-z0-9]+-){4}[a-z0-9]+\z/, city.slug)
|
74
81
|
end
|
75
82
|
end
|
76
83
|
|
@@ -88,7 +95,7 @@ class CandidatesTest < TestCaseClass
|
|
88
95
|
test "accepts candidate with lambda" do
|
89
96
|
klass = Class.new City do
|
90
97
|
def slug_candidates
|
91
|
-
[name, [name, ->{ rand 1000 }]]
|
98
|
+
[name, [name, -> { rand 1000 }]]
|
92
99
|
end
|
93
100
|
end
|
94
101
|
with_instances_of klass do |_, city|
|
@@ -98,13 +105,6 @@ class CandidatesTest < TestCaseClass
|
|
98
105
|
|
99
106
|
test "accepts candidate with object" do
|
100
107
|
klass = Class.new City do
|
101
|
-
class Airport
|
102
|
-
def initialize(code)
|
103
|
-
@code = code
|
104
|
-
end
|
105
|
-
attr_reader :code
|
106
|
-
alias_method :to_s, :code
|
107
|
-
end
|
108
108
|
def slug_candidates
|
109
109
|
[name, [name, Airport.new(code)]]
|
110
110
|
end
|
@@ -122,7 +122,7 @@ class CandidatesTest < TestCaseClass
|
|
122
122
|
end
|
123
123
|
with_instances_of klass do |_, city|
|
124
124
|
candidates = FriendlyId::Candidates.new(city, city.slug_candidates)
|
125
|
-
assert_equal candidates.each, [
|
125
|
+
assert_equal candidates.each, ["new-york"]
|
126
126
|
end
|
127
127
|
end
|
128
128
|
|
@@ -136,8 +136,7 @@ class CandidatesTest < TestCaseClass
|
|
136
136
|
collected_candidates = []
|
137
137
|
candidates = FriendlyId::Candidates.new(city, city.slug_candidates)
|
138
138
|
candidates.each { |candidate| collected_candidates << candidate }
|
139
|
-
assert_equal collected_candidates, [
|
139
|
+
assert_equal collected_candidates, ["new-york"]
|
140
140
|
end
|
141
141
|
end
|
142
|
-
|
143
142
|
end
|
data/test/configuration_test.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
require "helper"
|
2
2
|
|
3
3
|
class ConfigurationTest < TestCaseClass
|
4
|
-
|
5
4
|
include FriendlyId::Test
|
6
5
|
|
7
6
|
def setup
|
@@ -16,13 +15,13 @@ class ConfigurationTest < TestCaseClass
|
|
16
15
|
end
|
17
16
|
|
18
17
|
test "should set options on initialization if present" do
|
19
|
-
config = FriendlyId::Configuration.new @model_class, :
|
18
|
+
config = FriendlyId::Configuration.new @model_class, base: "hello"
|
20
19
|
assert_equal "hello", config.base
|
21
20
|
end
|
22
21
|
|
23
22
|
test "should raise error if passed unrecognized option" do
|
24
23
|
assert_raises NoMethodError do
|
25
|
-
FriendlyId::Configuration.new @model_class, :
|
24
|
+
FriendlyId::Configuration.new @model_class, foo: "bar"
|
26
25
|
end
|
27
26
|
end
|
28
27
|
|
@@ -30,7 +29,7 @@ class ConfigurationTest < TestCaseClass
|
|
30
29
|
refute @model_class < FriendlyId::Slugged
|
31
30
|
@model_class.class_eval do
|
32
31
|
extend FriendlyId
|
33
|
-
friendly_id :hello, :
|
32
|
+
friendly_id :hello, use: :slugged
|
34
33
|
end
|
35
34
|
assert @model_class < FriendlyId::Slugged
|
36
35
|
end
|
@@ -40,7 +39,7 @@ class ConfigurationTest < TestCaseClass
|
|
40
39
|
refute @model_class < my_module
|
41
40
|
@model_class.class_eval do
|
42
41
|
extend FriendlyId
|
43
|
-
friendly_id :hello, :
|
42
|
+
friendly_id :hello, use: my_module
|
44
43
|
end
|
45
44
|
assert @model_class < my_module
|
46
45
|
end
|
@@ -48,17 +47,14 @@ class ConfigurationTest < TestCaseClass
|
|
48
47
|
test "#base should optionally set a value" do
|
49
48
|
config = FriendlyId::Configuration.new @model_class
|
50
49
|
assert_nil config.base
|
51
|
-
config.base =
|
52
|
-
assert_equal
|
50
|
+
config.base = "foo"
|
51
|
+
assert_equal "foo", config.base
|
53
52
|
end
|
54
53
|
|
55
54
|
test "#base can set the value to nil" do
|
56
55
|
config = FriendlyId::Configuration.new @model_class
|
57
|
-
config.base
|
56
|
+
config.base "foo"
|
58
57
|
config.base nil
|
59
58
|
assert_nil config.base
|
60
|
-
|
61
59
|
end
|
62
|
-
|
63
|
-
|
64
60
|
end
|
data/test/core_test.rb
CHANGED
@@ -12,7 +12,6 @@ class Author < ActiveRecord::Base
|
|
12
12
|
end
|
13
13
|
|
14
14
|
class CoreTest < TestCaseClass
|
15
|
-
|
16
15
|
include FriendlyId::Test
|
17
16
|
include FriendlyId::Test::Shared::Core
|
18
17
|
|
@@ -31,6 +30,6 @@ class CoreTest < TestCaseClass
|
|
31
30
|
end
|
32
31
|
|
33
32
|
test "instances should have a friendly id" do
|
34
|
-
with_instance_of(model_class) {|record| assert record.friendly_id}
|
33
|
+
with_instance_of(model_class) { |record| assert record.friendly_id }
|
35
34
|
end
|
36
35
|
end
|
data/test/databases.yml
CHANGED
@@ -9,9 +9,10 @@ mysql:
|
|
9
9
|
|
10
10
|
postgres:
|
11
11
|
adapter: postgresql
|
12
|
-
host: <%= ENV.fetch('PGHOST'
|
13
|
-
port: <%= ENV.fetch('PGPORT'
|
14
|
-
username: <%= ENV.fetch('PGUSER'
|
12
|
+
host: <%= ENV.fetch('PGHOST', 'localhost') %>
|
13
|
+
port: <%= ENV.fetch('PGPORT', '5432') %>
|
14
|
+
username: <%= ENV.fetch('PGUSER', 'postgres') %>
|
15
|
+
password: <%= ENV.fetch('PGPASSWORD', 'postgres') %>
|
15
16
|
database: friendly_id_test
|
16
17
|
encoding: utf8
|
17
18
|
|
data/test/finders_test.rb
CHANGED
@@ -1,29 +1,76 @@
|
|
1
1
|
require "helper"
|
2
2
|
|
3
3
|
class JournalistWithFriendlyFinders < ActiveRecord::Base
|
4
|
-
self.table_name =
|
4
|
+
self.table_name = "journalists"
|
5
5
|
extend FriendlyId
|
6
|
-
scope :existing, -> {where(
|
6
|
+
scope :existing, -> { where("1 = 1") }
|
7
7
|
friendly_id :name, use: [:slugged, :finders]
|
8
8
|
end
|
9
9
|
|
10
10
|
class Finders < TestCaseClass
|
11
|
-
|
12
11
|
include FriendlyId::Test
|
13
12
|
|
14
13
|
def model_class
|
15
14
|
JournalistWithFriendlyFinders
|
16
15
|
end
|
17
16
|
|
18
|
-
test
|
17
|
+
test "should find records with finders as class methods" do
|
19
18
|
with_instance_of(model_class) do |record|
|
20
19
|
assert model_class.find(record.friendly_id)
|
21
20
|
end
|
22
21
|
end
|
23
22
|
|
24
|
-
test
|
23
|
+
test "should find records with finders on relations" do
|
25
24
|
with_instance_of(model_class) do |record|
|
26
25
|
assert model_class.existing.find(record.friendly_id)
|
27
26
|
end
|
28
27
|
end
|
28
|
+
|
29
|
+
test "allows nil with allow_nil: true" do
|
30
|
+
with_instance_of(model_class) do |record|
|
31
|
+
assert_nil model_class.find("foo", allow_nil: true)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
test "allows nil on relations with allow_nil: true" do
|
36
|
+
with_instance_of(model_class) do |record|
|
37
|
+
assert_nil model_class.existing.find("foo", allow_nil: true)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
test "allows nil with a bad primary key ID and allow_nil: true" do
|
42
|
+
with_instance_of(model_class) do |record|
|
43
|
+
assert_nil model_class.find(0, allow_nil: true)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
test "allows nil on relations with a bad primary key ID and allow_nil: true" do
|
48
|
+
with_instance_of(model_class) do |record|
|
49
|
+
assert_nil model_class.existing.find(0, allow_nil: true)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
test "allows nil with a bad potential primary key ID and allow_nil: true" do
|
54
|
+
with_instance_of(model_class) do |record|
|
55
|
+
assert_nil model_class.find("0", allow_nil: true)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
test "allows nil on relations with a bad potential primary key ID and allow_nil: true" do
|
60
|
+
with_instance_of(model_class) do |record|
|
61
|
+
assert_nil model_class.existing.find("0", allow_nil: true)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
test "allows nil with nil ID and allow_nil: true" do
|
66
|
+
with_instance_of(model_class) do |record|
|
67
|
+
assert_nil model_class.find(nil, allow_nil: true)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
test "allows nil on relations with nil ID and allow_nil: true" do
|
72
|
+
with_instance_of(model_class) do |record|
|
73
|
+
assert_nil model_class.existing.find(nil, allow_nil: true)
|
74
|
+
end
|
75
|
+
end
|
29
76
|
end
|
data/test/generator_test.rb
CHANGED
@@ -3,46 +3,36 @@ require "rails/generators"
|
|
3
3
|
require "generators/friendly_id_generator"
|
4
4
|
|
5
5
|
class FriendlyIdGeneratorTest < Rails::Generators::TestCase
|
6
|
-
|
7
6
|
tests FriendlyIdGenerator
|
8
7
|
destination File.expand_path("../../tmp", __FILE__)
|
9
8
|
|
10
9
|
setup :prepare_destination
|
11
10
|
|
12
11
|
test "should generate a migration" do
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
FileUtils.rm_rf self.destination_root
|
18
|
-
end
|
12
|
+
run_generator
|
13
|
+
assert_migration "db/migrate/create_friendly_id_slugs"
|
14
|
+
ensure
|
15
|
+
FileUtils.rm_rf destination_root
|
19
16
|
end
|
20
17
|
|
21
18
|
test "should skip the migration when told to do so" do
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
FileUtils.rm_rf self.destination_root
|
27
|
-
end
|
19
|
+
run_generator ["--skip-migration"]
|
20
|
+
assert_no_migration "db/migrate/create_friendly_id_slugs"
|
21
|
+
ensure
|
22
|
+
FileUtils.rm_rf destination_root
|
28
23
|
end
|
29
24
|
|
30
25
|
test "should generate an initializer" do
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
FileUtils.rm_rf self.destination_root
|
36
|
-
end
|
26
|
+
run_generator
|
27
|
+
assert_file "config/initializers/friendly_id.rb"
|
28
|
+
ensure
|
29
|
+
FileUtils.rm_rf destination_root
|
37
30
|
end
|
38
31
|
|
39
32
|
test "should skip the initializer when told to do so" do
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
FileUtils.rm_rf self.destination_root
|
45
|
-
end
|
33
|
+
run_generator ["--skip-initializer"]
|
34
|
+
assert_no_file "config/initializers/friendly_id.rb"
|
35
|
+
ensure
|
36
|
+
FileUtils.rm_rf destination_root
|
46
37
|
end
|
47
|
-
|
48
38
|
end
|
data/test/helper.rb
CHANGED
@@ -1,33 +1,33 @@
|
|
1
1
|
require "bundler/setup"
|
2
2
|
|
3
|
-
if ENV[
|
4
|
-
require
|
5
|
-
if ENV[
|
6
|
-
require
|
3
|
+
if ENV["COVERALLS"] || ENV["COVERAGE"]
|
4
|
+
require "simplecov"
|
5
|
+
if ENV["COVERALLS"]
|
6
|
+
require "coveralls"
|
7
7
|
SimpleCov.formatter = Coveralls::SimpleCov::Formatter
|
8
8
|
end
|
9
9
|
SimpleCov.start do
|
10
|
-
add_filter
|
11
|
-
add_filter
|
10
|
+
add_filter "test"
|
11
|
+
add_filter "friendly_id/migration"
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
15
|
begin
|
16
|
-
require
|
16
|
+
require "minitest"
|
17
17
|
rescue LoadError
|
18
|
-
require
|
18
|
+
require "minitest/unit"
|
19
19
|
end
|
20
20
|
|
21
21
|
begin
|
22
|
-
TestCaseClass =
|
22
|
+
TestCaseClass = Minitest::Test
|
23
23
|
rescue NameError
|
24
|
-
TestCaseClass =
|
24
|
+
TestCaseClass = Minitest::Unit::TestCase
|
25
25
|
end
|
26
26
|
|
27
|
-
require "mocha/
|
27
|
+
require "mocha/minitest"
|
28
28
|
require "active_record"
|
29
|
-
require
|
30
|
-
require
|
29
|
+
require "active_support/core_ext/time/conversions"
|
30
|
+
require "erb"
|
31
31
|
|
32
32
|
I18n.enforce_available_locales = false
|
33
33
|
|
@@ -39,29 +39,32 @@ if ENV["LOG"]
|
|
39
39
|
ActiveRecord::Base.logger = Logger.new($stdout)
|
40
40
|
end
|
41
41
|
|
42
|
-
if ActiveSupport::VERSION::STRING >=
|
42
|
+
if ActiveSupport::VERSION::STRING >= "4.2"
|
43
43
|
ActiveSupport.test_order = :random
|
44
44
|
end
|
45
45
|
|
46
46
|
module FriendlyId
|
47
47
|
module Test
|
48
|
-
|
49
48
|
def self.included(base)
|
50
49
|
if Minitest.respond_to?(:autorun)
|
51
50
|
Minitest.autorun
|
52
51
|
else
|
53
|
-
require
|
52
|
+
require "minitest/autorun"
|
54
53
|
end
|
55
54
|
rescue LoadError
|
56
55
|
end
|
57
56
|
|
58
57
|
def transaction
|
59
|
-
ActiveRecord::Base.transaction
|
58
|
+
ActiveRecord::Base.transaction do
|
59
|
+
yield
|
60
|
+
|
61
|
+
raise ActiveRecord::Rollback
|
62
|
+
end
|
60
63
|
end
|
61
64
|
|
62
65
|
def with_instance_of(*args)
|
63
66
|
model_class = args.shift
|
64
|
-
args[0] ||= {:
|
67
|
+
args[0] ||= {name: "a b c"}
|
65
68
|
transaction { yield model_class.create!(*args) }
|
66
69
|
end
|
67
70
|
|
@@ -70,7 +73,11 @@ module FriendlyId
|
|
70
73
|
|
71
74
|
def connect
|
72
75
|
version = ActiveRecord::VERSION::STRING
|
73
|
-
engine
|
76
|
+
engine = begin
|
77
|
+
RUBY_ENGINE
|
78
|
+
rescue
|
79
|
+
"ruby"
|
80
|
+
end
|
74
81
|
|
75
82
|
ActiveRecord::Base.establish_connection config[driver]
|
76
83
|
message = "Using #{engine} #{RUBY_VERSION} AR #{version} with #{driver}"
|
@@ -86,7 +93,7 @@ module FriendlyId
|
|
86
93
|
end
|
87
94
|
|
88
95
|
def config
|
89
|
-
@config ||= YAML
|
96
|
+
@config ||= YAML.safe_load(
|
90
97
|
ERB.new(
|
91
98
|
File.read(File.expand_path("../databases.yml", __FILE__))
|
92
99
|
).result
|
@@ -94,9 +101,9 @@ module FriendlyId
|
|
94
101
|
end
|
95
102
|
|
96
103
|
def driver
|
97
|
-
|
98
|
-
|
99
|
-
|
104
|
+
db_driver = ENV.fetch("DB", "sqlite3").downcase
|
105
|
+
db_driver = "postgres" if %w[postgresql pg].include?(db_driver)
|
106
|
+
db_driver
|
100
107
|
end
|
101
108
|
|
102
109
|
def in_memory?
|
@@ -115,4 +122,4 @@ end
|
|
115
122
|
require "schema"
|
116
123
|
require "shared"
|
117
124
|
FriendlyId::Test::Database.connect
|
118
|
-
at_exit {ActiveRecord::Base.connection.disconnect!}
|
125
|
+
at_exit { ActiveRecord::Base.connection.disconnect! }
|