friendly_id 5.4.2 → 5.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/.github/dependabot.yml +6 -0
  4. data/.github/workflows/test.yml +34 -25
  5. data/Changelog.md +6 -0
  6. data/Gemfile +9 -13
  7. data/README.md +21 -0
  8. data/Rakefile +24 -27
  9. data/bench.rb +30 -27
  10. data/certs/parndt.pem +25 -23
  11. data/friendly_id.gemspec +26 -29
  12. data/gemfiles/Gemfile.rails-5.2.rb +11 -16
  13. data/gemfiles/Gemfile.rails-6.0.rb +11 -16
  14. data/gemfiles/Gemfile.rails-6.1.rb +22 -0
  15. data/gemfiles/Gemfile.rails-7.0.rb +22 -0
  16. data/guide.rb +5 -5
  17. data/lib/friendly_id/base.rb +57 -60
  18. data/lib/friendly_id/candidates.rb +9 -11
  19. data/lib/friendly_id/configuration.rb +6 -7
  20. data/lib/friendly_id/finder_methods.rb +26 -11
  21. data/lib/friendly_id/finders.rb +63 -66
  22. data/lib/friendly_id/history.rb +59 -63
  23. data/lib/friendly_id/initializer.rb +4 -4
  24. data/lib/friendly_id/migration.rb +6 -6
  25. data/lib/friendly_id/object_utils.rb +2 -2
  26. data/lib/friendly_id/reserved.rb +28 -32
  27. data/lib/friendly_id/scoped.rb +97 -102
  28. data/lib/friendly_id/sequentially_slugged/calculator.rb +69 -0
  29. data/lib/friendly_id/sequentially_slugged.rb +17 -64
  30. data/lib/friendly_id/simple_i18n.rb +75 -69
  31. data/lib/friendly_id/slug.rb +1 -2
  32. data/lib/friendly_id/slug_generator.rb +1 -3
  33. data/lib/friendly_id/slugged.rb +234 -238
  34. data/lib/friendly_id/version.rb +1 -1
  35. data/lib/friendly_id.rb +41 -45
  36. data/lib/generators/friendly_id_generator.rb +9 -9
  37. data/test/base_test.rb +10 -13
  38. data/test/benchmarks/finders.rb +28 -26
  39. data/test/benchmarks/object_utils.rb +13 -13
  40. data/test/candidates_test.rb +17 -18
  41. data/test/configuration_test.rb +7 -11
  42. data/test/core_test.rb +1 -2
  43. data/test/databases.yml +4 -3
  44. data/test/finders_test.rb +52 -5
  45. data/test/generator_test.rb +16 -26
  46. data/test/helper.rb +29 -22
  47. data/test/history_test.rb +70 -74
  48. data/test/numeric_slug_test.rb +4 -4
  49. data/test/object_utils_test.rb +0 -2
  50. data/test/reserved_test.rb +9 -11
  51. data/test/schema.rb +5 -4
  52. data/test/scoped_test.rb +18 -20
  53. data/test/sequentially_slugged_test.rb +65 -50
  54. data/test/shared.rb +15 -16
  55. data/test/simple_i18n_test.rb +22 -12
  56. data/test/slugged_test.rb +102 -121
  57. data/test/sti_test.rb +19 -21
  58. data.tar.gz.sig +0 -0
  59. metadata +35 -30
  60. metadata.gz.sig +1 -1
data/test/scoped_test.rb CHANGED
@@ -2,14 +2,14 @@ require "helper"
2
2
 
3
3
  class Novelist < ActiveRecord::Base
4
4
  extend FriendlyId
5
- friendly_id :name, :use => :slugged
5
+ friendly_id :name, use: :slugged
6
6
  end
7
7
 
8
8
  class Novel < ActiveRecord::Base
9
9
  extend FriendlyId
10
10
  belongs_to :novelist
11
11
  belongs_to :publisher
12
- friendly_id :name, :use => :scoped, :scope => [:publisher, :novelist]
12
+ friendly_id :name, use: :scoped, scope: [:publisher, :novelist]
13
13
 
14
14
  def should_generate_new_friendly_id?
15
15
  new_record? || super
@@ -21,7 +21,6 @@ class Publisher < ActiveRecord::Base
21
21
  end
22
22
 
23
23
  class ScopedTest < TestCaseClass
24
-
25
24
  include FriendlyId::Test
26
25
  include FriendlyId::Test::Shared::Core
27
26
 
@@ -37,42 +36,42 @@ class ScopedTest < TestCaseClass
37
36
  model_class = Class.new(ActiveRecord::Base) do
38
37
  self.abstract_class = true
39
38
  extend FriendlyId
40
- friendly_id :empty, :use => :scoped, :scope => :dummy
39
+ friendly_id :empty, use: :scoped, scope: :dummy
41
40
  end
42
41
  assert_equal ["dummy"], model_class.friendly_id_config.scope_columns
43
42
  end
44
43
 
45
44
  test "should allow duplicate slugs outside scope" do
46
45
  transaction do
47
- novel1 = Novel.create! :name => "a", :novelist => Novelist.create!(:name => "a")
48
- novel2 = Novel.create! :name => "a", :novelist => Novelist.create!(:name => "b")
46
+ novel1 = Novel.create! name: "a", novelist: Novelist.create!(name: "a")
47
+ novel2 = Novel.create! name: "a", novelist: Novelist.create!(name: "b")
49
48
  assert_equal novel1.friendly_id, novel2.friendly_id
50
49
  end
51
50
  end
52
51
 
53
52
  test "should not allow duplicate slugs inside scope" do
54
53
  with_instance_of Novelist do |novelist|
55
- novel1 = Novel.create! :name => "a", :novelist => novelist
56
- novel2 = Novel.create! :name => "a", :novelist => novelist
54
+ novel1 = Novel.create! name: "a", novelist: novelist
55
+ novel2 = Novel.create! name: "a", novelist: novelist
57
56
  assert novel1.friendly_id != novel2.friendly_id
58
57
  end
59
58
  end
60
59
 
61
60
  test "should apply scope with multiple columns" do
62
61
  transaction do
63
- novelist = Novelist.create! :name => "a"
64
- publisher = Publisher.create! :name => "b"
65
- novel1 = Novel.create! :name => "c", :novelist => novelist, :publisher => publisher
66
- novel2 = Novel.create! :name => "c", :novelist => novelist, :publisher => Publisher.create(:name => "d")
67
- novel3 = Novel.create! :name => "c", :novelist => Novelist.create(:name => "e"), :publisher => publisher
68
- novel4 = Novel.create! :name => "c", :novelist => novelist, :publisher => publisher
62
+ novelist = Novelist.create! name: "a"
63
+ publisher = Publisher.create! name: "b"
64
+ novel1 = Novel.create! name: "c", novelist: novelist, publisher: publisher
65
+ novel2 = Novel.create! name: "c", novelist: novelist, publisher: Publisher.create(name: "d")
66
+ novel3 = Novel.create! name: "c", novelist: Novelist.create(name: "e"), publisher: publisher
67
+ novel4 = Novel.create! name: "c", novelist: novelist, publisher: publisher
69
68
  assert_equal novel1.friendly_id, novel2.friendly_id
70
69
  assert_equal novel2.friendly_id, novel3.friendly_id
71
70
  assert novel3.friendly_id != novel4.friendly_id
72
71
  end
73
72
  end
74
73
 
75
- test 'should allow a record to reuse its own slug' do
74
+ test "should allow a record to reuse its own slug" do
76
75
  with_instance_of(model_class) do |record|
77
76
  old_id = record.friendly_id
78
77
  record.slug = nil
@@ -83,15 +82,14 @@ class ScopedTest < TestCaseClass
83
82
 
84
83
  test "should generate new slug when scope changes" do
85
84
  transaction do
86
- novelist = Novelist.create! :name => "a"
87
- publisher = Publisher.create! :name => "b"
88
- novel1 = Novel.create! :name => "c", :novelist => novelist, :publisher => publisher
89
- novel2 = Novel.create! :name => "c", :novelist => novelist, :publisher => Publisher.create(:name => "d")
85
+ novelist = Novelist.create! name: "a"
86
+ publisher = Publisher.create! name: "b"
87
+ novel1 = Novel.create! name: "c", novelist: novelist, publisher: publisher
88
+ novel2 = Novel.create! name: "c", novelist: novelist, publisher: Publisher.create(name: "d")
90
89
  assert_equal novel1.friendly_id, novel2.friendly_id
91
90
  novel2.publisher = publisher
92
91
  novel2.save!
93
92
  assert novel2.friendly_id != novel1.friendly_id
94
93
  end
95
94
  end
96
-
97
95
  end
@@ -1,8 +1,8 @@
1
- require 'helper'
1
+ require "helper"
2
2
 
3
3
  class Article < ActiveRecord::Base
4
4
  extend FriendlyId
5
- friendly_id :name, :use => :sequentially_slugged
5
+ friendly_id :name, use: :sequentially_slugged
6
6
  end
7
7
 
8
8
  class SequentiallySluggedTest < TestCaseClass
@@ -15,27 +15,27 @@ class SequentiallySluggedTest < TestCaseClass
15
15
 
16
16
  test "should generate numerically sequential slugs" do
17
17
  transaction do
18
- records = 12.times.map { model_class.create! :name => "Some news" }
18
+ records = 12.times.map { model_class.create! name: "Some news" }
19
19
  assert_equal "some-news", records[0].slug
20
- (1...12).each {|i| assert_equal "some-news-#{i + 1}", records[i].slug}
20
+ (1...12).each { |i| assert_equal "some-news-#{i + 1}", records[i].slug }
21
21
  end
22
22
  end
23
23
 
24
24
  test "should cope when slugs are missing from the sequence" do
25
25
  transaction do
26
- record_1 = model_class.create!(:name => 'A thing')
27
- record_2 = model_class.create!(:name => 'A thing')
28
- record_3 = model_class.create!(:name => 'A thing')
26
+ record_1 = model_class.create!(name: "A thing")
27
+ record_2 = model_class.create!(name: "A thing")
28
+ record_3 = model_class.create!(name: "A thing")
29
29
 
30
- assert_equal 'a-thing', record_1.slug
31
- assert_equal 'a-thing-2', record_2.slug
32
- assert_equal 'a-thing-3', record_3.slug
30
+ assert_equal "a-thing", record_1.slug
31
+ assert_equal "a-thing-2", record_2.slug
32
+ assert_equal "a-thing-3", record_3.slug
33
33
 
34
34
  record_2.destroy
35
35
 
36
- record_4 = model_class.create!(:name => 'A thing')
36
+ record_4 = model_class.create!(name: "A thing")
37
37
 
38
- assert_equal 'a-thing-4', record_4.slug
38
+ assert_equal "a-thing-4", record_4.slug
39
39
  end
40
40
  end
41
41
 
@@ -43,32 +43,32 @@ class SequentiallySluggedTest < TestCaseClass
43
43
  model_class = Class.new(ActiveRecord::Base) do
44
44
  self.table_name = "journalists"
45
45
  extend FriendlyId
46
- friendly_id :name, :use => :sequentially_slugged, :slug_column => "strange name"
46
+ friendly_id :name, use: :sequentially_slugged, slug_column: "strange name"
47
47
  end
48
48
 
49
49
  transaction do
50
- record_1 = model_class.create! name: "Julian Assange"
51
- record_2 = model_class.create! name: "Julian Assange"
50
+ record_1 = model_class.create! name: "Lois Lane"
51
+ record_2 = model_class.create! name: "Lois Lane"
52
52
 
53
- assert_equal 'julian-assange', record_1.attributes["strange name"]
54
- assert_equal 'julian-assange-2', record_2.attributes["strange name"]
53
+ assert_equal "lois-lane", record_1.attributes["strange name"]
54
+ assert_equal "lois-lane-2", record_2.attributes["strange name"]
55
55
  end
56
56
  end
57
57
 
58
58
  test "should correctly sequence slugs that end in a number" do
59
59
  transaction do
60
- record1 = model_class.create! :name => "Peugeuot 206"
60
+ record1 = model_class.create! name: "Peugeuot 206"
61
61
  assert_equal "peugeuot-206", record1.slug
62
- record2 = model_class.create! :name => "Peugeuot 206"
62
+ record2 = model_class.create! name: "Peugeuot 206"
63
63
  assert_equal "peugeuot-206-2", record2.slug
64
64
  end
65
65
  end
66
66
 
67
67
  test "should correctly sequence slugs that begin with a number" do
68
68
  transaction do
69
- record1 = model_class.create! :name => "2010 to 2015 Records"
69
+ record1 = model_class.create! name: "2010 to 2015 Records"
70
70
  assert_equal "2010-to-2015-records", record1.slug
71
- record2 = model_class.create! :name => "2010 to 2015 Records"
71
+ record2 = model_class.create! name: "2010 to 2015 Records"
72
72
  assert_equal "2010-to-2015-records-2", record2.slug
73
73
  end
74
74
  end
@@ -77,15 +77,15 @@ class SequentiallySluggedTest < TestCaseClass
77
77
  model_class = Class.new(ActiveRecord::Base) do
78
78
  self.table_name = "novelists"
79
79
  extend FriendlyId
80
- friendly_id :name, :use => :sequentially_slugged, :sequence_separator => ':'
80
+ friendly_id :name, use: :sequentially_slugged, sequence_separator: ":"
81
81
  end
82
82
 
83
83
  transaction do
84
84
  record_1 = model_class.create! name: "Julian Barnes"
85
85
  record_2 = model_class.create! name: "Julian Barnes"
86
86
 
87
- assert_equal 'julian-barnes', record_1.slug
88
- assert_equal 'julian-barnes:2', record_2.slug
87
+ assert_equal "julian-barnes", record_1.slug
88
+ assert_equal "julian-barnes:2", record_2.slug
89
89
  end
90
90
  end
91
91
 
@@ -93,20 +93,20 @@ class SequentiallySluggedTest < TestCaseClass
93
93
  model_class = Class.new(ActiveRecord::Base) do
94
94
  self.table_name = "cities"
95
95
  extend FriendlyId
96
- friendly_id :slug_candidates, :use => [ :sequentially_slugged ]
96
+ friendly_id :slug_candidates, use: [:sequentially_slugged]
97
97
 
98
98
  def slug_candidates
99
99
  [name, [name, code]]
100
100
  end
101
101
  end
102
102
  transaction do
103
- record = model_class.create!(:name => nil, :code => nil)
103
+ record = model_class.create!(name: nil, code: nil)
104
104
  assert_nil record.slug
105
105
  end
106
106
  end
107
107
 
108
108
  test "should not generate a slug when the sluggable attribute is blank" do
109
- record = model_class.create!(:name => '')
109
+ record = model_class.create!(name: "")
110
110
  assert_nil record.slug
111
111
  end
112
112
  end
@@ -117,7 +117,12 @@ class SequentiallySluggedTestWithHistory < TestCaseClass
117
117
 
118
118
  class Article < ActiveRecord::Base
119
119
  extend FriendlyId
120
- friendly_id :name, :use => [:sequentially_slugged, :history]
120
+ friendly_id :name, use: [:sequentially_slugged, :history]
121
+ end
122
+
123
+ Journalist = Class.new(ActiveRecord::Base) do
124
+ extend FriendlyId
125
+ friendly_id :name, use: [:sequentially_slugged, :history], slug_column: "strange name"
121
126
  end
122
127
 
123
128
  def model_class
@@ -126,36 +131,46 @@ class SequentiallySluggedTestWithHistory < TestCaseClass
126
131
 
127
132
  test "should work with regeneration with history when slug already exists" do
128
133
  transaction do
129
- record1 = model_class.create! :name => "Test name"
130
- record2 = model_class.create! :name => "Another test name"
131
- assert_equal 'test-name', record1.slug
132
- assert_equal 'another-test-name', record2.slug
134
+ record1 = model_class.create! name: "Test name"
135
+ record2 = model_class.create! name: "Another test name"
136
+ assert_equal "test-name", record1.slug
137
+ assert_equal "another-test-name", record2.slug
133
138
 
134
139
  record2.name = "Test name"
135
140
  record2.slug = nil
136
141
  record2.save!
137
- assert_equal 'test-name-2', record2.slug
142
+ assert_equal "test-name-2", record2.slug
138
143
  end
139
144
  end
140
145
 
141
146
  test "should work with regeneration with history when 2 slugs already exists and the second is changed" do
142
147
  transaction do
143
- record1 = model_class.create! :name => "Test name"
144
- record2 = model_class.create! :name => "Test name"
145
- record3 = model_class.create! :name => "Another test name"
146
- assert_equal 'test-name', record1.slug
147
- assert_equal 'test-name-2', record2.slug
148
- assert_equal 'another-test-name', record3.slug
148
+ record1 = model_class.create! name: "Test name"
149
+ record2 = model_class.create! name: "Test name"
150
+ record3 = model_class.create! name: "Another test name"
151
+ assert_equal "test-name", record1.slug
152
+ assert_equal "test-name-2", record2.slug
153
+ assert_equal "another-test-name", record3.slug
149
154
 
150
155
  record2.name = "One more test name"
151
156
  record2.slug = nil
152
157
  record2.save!
153
- assert_equal 'one-more-test-name', record2.slug
158
+ assert_equal "one-more-test-name", record2.slug
154
159
 
155
160
  record3.name = "Test name"
156
161
  record3.slug = nil
157
162
  record3.save!
158
- assert_equal 'test-name-3', record3.slug
163
+ assert_equal "test-name-3", record3.slug
164
+ end
165
+ end
166
+
167
+ test "should cope with strange column names" do
168
+ transaction do
169
+ record_1 = Journalist.create! name: "Lois Lane"
170
+ record_2 = Journalist.create! name: "Lois Lane"
171
+
172
+ assert_equal "lois-lane", record_1.attributes["strange name"]
173
+ assert_equal "lois-lane-2", record_2.attributes["strange name"]
159
174
  end
160
175
  end
161
176
  end
@@ -167,7 +182,7 @@ end
167
182
  class Restaurant < ActiveRecord::Base
168
183
  extend FriendlyId
169
184
  belongs_to :city
170
- friendly_id :name, :use => [:sequentially_slugged, :scoped, :history], :scope => :city
185
+ friendly_id :name, use: [:sequentially_slugged, :scoped, :history], scope: :city
171
186
  end
172
187
 
173
188
  class SequentiallySluggedTestWithScopedHistory < TestCaseClass
@@ -181,19 +196,19 @@ class SequentiallySluggedTestWithScopedHistory < TestCaseClass
181
196
  test "should work with regeneration with scoped history" do
182
197
  transaction do
183
198
  city1 = City.create!
184
- city2 = City.create!
185
- record1 = model_class.create! :name => "Test name", :city => city1
186
- record2 = model_class.create! :name => "Test name", :city => city1
199
+ City.create!
200
+ record1 = model_class.create! name: "Test name", city: city1
201
+ record2 = model_class.create! name: "Test name", city: city1
187
202
 
188
- assert_equal 'test-name', record1.slug
189
- assert_equal 'test-name-2', record2.slug
203
+ assert_equal "test-name", record1.slug
204
+ assert_equal "test-name-2", record2.slug
190
205
 
191
- record2.name = 'Another test name'
206
+ record2.name = "Another test name"
192
207
  record2.slug = nil
193
208
  record2.save!
194
209
 
195
- record3 = model_class.create! :name => "Test name", :city => city1
196
- assert_equal 'test-name-3', record3.slug
210
+ record3 = model_class.create! name: "Test name", city: city1
211
+ assert_equal "test-name-3", record3.slug
197
212
  end
198
213
  end
199
214
  end
data/test/shared.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  module FriendlyId
2
2
  module Test
3
3
  module Shared
4
-
5
4
  module Slugged
6
5
  test "configuration should have a sequence_separator" do
7
6
  assert !model_class.friendly_id_config.sequence_separator.empty?
@@ -18,15 +17,15 @@ module FriendlyId
18
17
 
19
18
  test "should add a UUID for duplicate friendly ids" do
20
19
  with_instance_of model_class do |record|
21
- record2 = model_class.create! :name => record.name
22
- assert record2.friendly_id.match(/([0-9a-z]+\-){4}[0-9a-z]+\z/)
20
+ record2 = model_class.create! name: record.name
21
+ assert record2.friendly_id.match(/([0-9a-z]+-){4}[0-9a-z]+\z/)
23
22
  end
24
23
  end
25
24
 
26
25
  test "should not add slug sequence on update after other conflicting slugs were added" do
27
26
  with_instance_of model_class do |record|
28
27
  old = record.friendly_id
29
- model_class.create! :name => record.name
28
+ model_class.create! name: record.name
30
29
  record.save!
31
30
  record.reload
32
31
  assert_equal old, record.to_param
@@ -35,7 +34,7 @@ module FriendlyId
35
34
 
36
35
  test "should not change the sequence on save" do
37
36
  with_instance_of model_class do |record|
38
- record2 = model_class.create! :name => record.name
37
+ record2 = model_class.create! name: record.name
39
38
  friendly_id = record2.friendly_id
40
39
  record2.active = !record2.active
41
40
  record2.save!
@@ -64,7 +63,7 @@ module FriendlyId
64
63
  with_instance_of my_model_class do |record|
65
64
  record.update my_model_class.friendly_id_config.slug_column => nil
66
65
  record = my_model_class.friendly.find(record.id)
67
- record.class.validate Proc.new {errors.add(:name, "FAIL")}
66
+ record.class.validate proc { errors.add(:name, "FAIL") }
68
67
  record.save
69
68
  assert_equal record.to_param, record.friendly_id
70
69
  end
@@ -84,7 +83,7 @@ module FriendlyId
84
83
  end
85
84
 
86
85
  test "should be findable by friendly id" do
87
- with_instance_of(model_class) {|record| assert model_class.friendly.find record.friendly_id}
86
+ with_instance_of(model_class) { |record| assert model_class.friendly.find record.friendly_id }
88
87
  end
89
88
 
90
89
  test "should exist? by friendly id" do
@@ -92,19 +91,19 @@ module FriendlyId
92
91
  assert model_class.friendly.exists? record.id
93
92
  assert model_class.friendly.exists? record.id.to_s
94
93
  assert model_class.friendly.exists? record.friendly_id
95
- assert model_class.friendly.exists?({:id => record.id})
96
- assert model_class.friendly.exists?(['id = ?', record.id])
94
+ assert model_class.friendly.exists?({id: record.id})
95
+ assert model_class.friendly.exists?(["id = ?", record.id])
97
96
  assert !model_class.friendly.exists?(record.friendly_id + "-hello")
98
97
  assert !model_class.friendly.exists?(0)
99
98
  end
100
99
  end
101
100
 
102
101
  test "should be findable by id as integer" do
103
- with_instance_of(model_class) {|record| assert model_class.friendly.find record.id.to_i}
102
+ with_instance_of(model_class) { |record| assert model_class.friendly.find record.id.to_i }
104
103
  end
105
104
 
106
105
  test "should be findable by id as string" do
107
- with_instance_of(model_class) {|record| assert model_class.friendly.find record.id.to_s}
106
+ with_instance_of(model_class) { |record| assert model_class.friendly.find record.id.to_s }
108
107
  end
109
108
 
110
109
  test "should treat numeric part of string as an integer id" do
@@ -116,16 +115,16 @@ module FriendlyId
116
115
  end
117
116
 
118
117
  test "should be findable by numeric friendly_id" do
119
- with_instance_of(model_class, :name => "206") {|record| assert model_class.friendly.find record.friendly_id}
118
+ with_instance_of(model_class, name: "206") { |record| assert model_class.friendly.find record.friendly_id }
120
119
  end
121
120
 
122
121
  test "to_param should return the friendly_id" do
123
- with_instance_of(model_class) {|record| assert_equal record.friendly_id, record.to_param}
122
+ with_instance_of(model_class) { |record| assert_equal record.friendly_id, record.to_param }
124
123
  end
125
124
 
126
125
  if ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR < 2
127
126
  test "should be findable by themselves" do
128
- with_instance_of(model_class) {|record| assert_equal record, model_class.friendly.find(record)}
127
+ with_instance_of(model_class) { |record| assert_equal record, model_class.friendly.find(record) }
129
128
  end
130
129
  end
131
130
 
@@ -138,11 +137,11 @@ module FriendlyId
138
137
  end
139
138
 
140
139
  test "instances found by a single id should not be read-only" do
141
- with_instance_of(model_class) {|record| assert !model_class.friendly.find(record.friendly_id).readonly?}
140
+ with_instance_of(model_class) { |record| assert !model_class.friendly.find(record.friendly_id).readonly? }
142
141
  end
143
142
 
144
143
  test "failing finds with unfriendly_id should raise errors normally" do
145
- assert_raises(ActiveRecord::RecordNotFound) {model_class.friendly.find 0}
144
+ assert_raises(ActiveRecord::RecordNotFound) { model_class.friendly.find 0 }
146
145
  end
147
146
 
148
147
  test "should return numeric id if the friendly_id is nil" do
@@ -5,7 +5,7 @@ class SimpleI18nTest < TestCaseClass
5
5
 
6
6
  class Journalist < ActiveRecord::Base
7
7
  extend FriendlyId
8
- friendly_id :name, :use => :simple_i18n
8
+ friendly_id :name, use: :simple_i18n
9
9
  end
10
10
 
11
11
  def setup
@@ -13,8 +13,9 @@ class SimpleI18nTest < TestCaseClass
13
13
  end
14
14
 
15
15
  test "friendly_id should return the current locale's slug" do
16
- journalist = Journalist.new(:name => "John Doe")
16
+ journalist = Journalist.new(name: "John Doe")
17
17
  journalist.slug_es = "juan-fulano"
18
+ journalist.slug_fr_ca = "jean-dupont"
18
19
  journalist.valid?
19
20
  I18n.with_locale(I18n.default_locale) do
20
21
  assert_equal "john-doe", journalist.friendly_id
@@ -22,17 +23,20 @@ class SimpleI18nTest < TestCaseClass
22
23
  I18n.with_locale(:es) do
23
24
  assert_equal "juan-fulano", journalist.friendly_id
24
25
  end
26
+ I18n.with_locale(:"fr-CA") do
27
+ assert_equal "jean-dupont", journalist.friendly_id
28
+ end
25
29
  end
26
30
 
27
31
  test "should create record with slug in column for the current locale" do
28
32
  I18n.with_locale(I18n.default_locale) do
29
- journalist = Journalist.new(:name => "John Doe")
33
+ journalist = Journalist.new(name: "John Doe")
30
34
  journalist.valid?
31
35
  assert_equal "john-doe", journalist.slug_en
32
36
  assert_nil journalist.slug_es
33
37
  end
34
38
  I18n.with_locale(:es) do
35
- journalist = Journalist.new(:name => "John Doe")
39
+ journalist = Journalist.new(name: "John Doe")
36
40
  journalist.valid?
37
41
  assert_equal "john-doe", journalist.slug_es
38
42
  assert_nil journalist.slug_en
@@ -41,7 +45,7 @@ class SimpleI18nTest < TestCaseClass
41
45
 
42
46
  test "to_param should return the numeric id when there's no slug for the current locale" do
43
47
  transaction do
44
- journalist = Journalist.new(:name => "Juan Fulano")
48
+ journalist = Journalist.new(name: "Juan Fulano")
45
49
  I18n.with_locale(:es) do
46
50
  journalist.save!
47
51
  assert_equal "juan-fulano", journalist.to_param
@@ -52,7 +56,7 @@ class SimpleI18nTest < TestCaseClass
52
56
 
53
57
  test "should set friendly id for locale" do
54
58
  transaction do
55
- journalist = Journalist.create!(:name => "John Smith")
59
+ journalist = Journalist.create!(name: "John Smith")
56
60
  journalist.set_friendly_id("Juan Fulano", :es)
57
61
  journalist.save!
58
62
  assert_equal "juan-fulano", journalist.slug_es
@@ -65,7 +69,7 @@ class SimpleI18nTest < TestCaseClass
65
69
  test "set friendly_id should fall back default locale when none is given" do
66
70
  transaction do
67
71
  journalist = I18n.with_locale(:es) do
68
- Journalist.create!(:name => "Juan Fulano")
72
+ Journalist.create!(name: "Juan Fulano")
69
73
  end
70
74
  journalist.set_friendly_id("John Doe")
71
75
  journalist.save!
@@ -75,9 +79,9 @@ class SimpleI18nTest < TestCaseClass
75
79
 
76
80
  test "should sequence localized slugs" do
77
81
  transaction do
78
- journalist = Journalist.create!(:name => "John Smith")
82
+ journalist = Journalist.create!(name: "John Smith")
79
83
  I18n.with_locale(:es) do
80
- Journalist.create!(:name => "Juan Fulano")
84
+ Journalist.create!(name: "Juan Fulano")
81
85
  end
82
86
  journalist.set_friendly_id("Juan Fulano", :es)
83
87
  journalist.save!
@@ -93,12 +97,12 @@ class SimpleI18nTest < TestCaseClass
93
97
 
94
98
  test "should not overwrite other locale's slugs on update" do
95
99
  transaction do
96
- journalist = Journalist.create!(:name => "John Smith")
100
+ journalist = Journalist.create!(name: "John Smith")
97
101
  journalist.set_friendly_id("Juan Fulano", :es)
98
102
  journalist.save!
99
103
  assert_equal "john-smith", journalist.to_param
100
104
  journalist.slug = nil
101
- journalist.update :name => "Johnny Smith"
105
+ journalist.update name: "Johnny Smith"
102
106
  assert_equal "johnny-smith", journalist.to_param
103
107
  I18n.with_locale(:es) do
104
108
  assert_equal "juan-fulano", journalist.to_param
@@ -114,11 +118,17 @@ class SimpleI18nTest < TestCaseClass
114
118
  end
115
119
  end
116
120
 
121
+ test "should add locale to slug column for a locale with a region subtag" do
122
+ I18n.with_locale :"fr-CA" do
123
+ assert_equal "slug_fr_ca", Journalist.friendly_id_config.slug_column
124
+ end
125
+ end
126
+
117
127
  test "should add locale to non-default slug column and non-default locale" do
118
128
  model_class = Class.new(ActiveRecord::Base) do
119
129
  self.abstract_class = true
120
130
  extend FriendlyId
121
- friendly_id :name, :use => :simple_i18n, :slug_column => :foo
131
+ friendly_id :name, use: :simple_i18n, slug_column: :foo
122
132
  end
123
133
  I18n.with_locale :es do
124
134
  assert_equal "foo_es", model_class.friendly_id_config.slug_column