File: history_test.rb

package info (click to toggle)
ruby-friendly-id 5.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 436 kB
  • sloc: ruby: 3,248; makefile: 3
file content (434 lines) | stat: -rw-r--r-- 12,165 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
require "helper"

class HistoryTest < TestCaseClass
  include FriendlyId::Test
  include FriendlyId::Test::Shared::Core

  class Manual < ActiveRecord::Base
    extend FriendlyId
    friendly_id :name, use: [:slugged, :history]
  end

  def model_class
    Manual
  end

  test "should insert record in slugs table on create" do
    with_instance_of(model_class) { |record| assert record.slugs.any? }
  end

  test "should not create new slug record if friendly_id is not changed" do
    with_instance_of(model_class) do |record|
      record.active = true
      record.save!
      assert_equal 1, FriendlyId::Slug.count
    end
  end

  test "should create new slug record when friendly_id changes" do
    with_instance_of(model_class) do |record|
      record.name = record.name + "b"
      record.slug = nil
      record.save!
      assert_equal 2, FriendlyId::Slug.count
    end
  end

  test "should be findable by old slugs" do
    with_instance_of(model_class) do |record|
      old_friendly_id = record.friendly_id
      record.name = record.name + "b"
      record.slug = nil
      record.save!
      begin
        assert model_class.friendly.find(old_friendly_id)
        assert model_class.friendly.exists?(old_friendly_id), "should exist? by old id"
      rescue ActiveRecord::RecordNotFound
        flunk "Could not find record by old id"
      end
    end
  end

  test "should create slug records on each change" do
    transaction do
      model_class.create! name: "hello"
      assert_equal 1, FriendlyId::Slug.count

      record = model_class.friendly.find("hello")
      record.name = "hello again"
      record.slug = nil
      record.save!
      assert_equal 2, FriendlyId::Slug.count
    end
  end

  test "should not be read only when found by slug" do
    with_instance_of(model_class) do |record|
      refute model_class.friendly.find(record.friendly_id).readonly?
      assert record.update name: "foo"
    end
  end

  test "should not be read only when found by old slug" do
    with_instance_of(model_class) do |record|
      old_friendly_id = record.friendly_id
      record.name = record.name + "b"
      record.save!
      assert !model_class.friendly.find(old_friendly_id).readonly?
    end
  end

  test "should handle renames" do
    with_instance_of(model_class) do |record|
      record.name = "x"
      record.slug = nil
      assert record.save
      record.name = "y"
      record.slug = nil
      assert record.save
      record.name = "x"
      record.slug = nil
      assert record.save
    end
  end

  test "should maintain history even if current slug is not the most recent one" do
    with_instance_of(model_class) do |record|
      record.name = "current"
      assert record.save

      # this feels like a hack. only thing i can get to work with the HistoryTestWithSti
      # test cases. (Editorialist vs Journalist.)
      sluggable_type = FriendlyId::Slug.first.sluggable_type
      # create several slugs for record
      # current slug does not have max id
      FriendlyId::Slug.delete_all
      FriendlyId::Slug.create(sluggable_type: sluggable_type, sluggable_id: record.id, slug: "current")
      FriendlyId::Slug.create(sluggable_type: sluggable_type, sluggable_id: record.id, slug: "outdated")

      record.reload
      record.slug = nil
      assert record.save

      assert_equal 2, FriendlyId::Slug.count
    end
  end

  test "should not create new slugs that match old slugs" do
    transaction do
      first_record = model_class.create! name: "foo"
      first_record.name = "bar"
      first_record.save!
      second_record = model_class.create! name: "foo"
      assert second_record.slug != "foo"
      assert_match(/foo-.+/, second_record.slug)
    end
  end

  test "should not fail when updating historic slugs" do
    transaction do
      first_record = model_class.create! name: "foo"
      second_record = model_class.create! name: "another"

      second_record.update name: "foo", slug: nil
      assert_match(/foo-.*/, second_record.slug)

      first_record.update name: "another", slug: nil
      assert_match(/another-.*/, first_record.slug)
    end
  end

  test "should prefer product that used slug most recently" do
    transaction do
      first_record = model_class.create! name: "foo"
      second_record = model_class.create! name: "bar"

      first_record.update! slug: "not_foo"
      second_record.update! slug: "foo" # now both records have used foo; second_record most recently
      second_record.update! slug: "not_bar"

      assert_equal model_class.friendly.find("foo"), second_record
    end
  end

  test "should name table according to prefix and suffix" do
    transaction do
      prefix = "prefix_"
      without_prefix = FriendlyId::Slug.table_name
      ActiveRecord::Base.table_name_prefix = prefix
      FriendlyId::Slug.reset_table_name
      assert_equal prefix + without_prefix, FriendlyId::Slug.table_name
    ensure
      ActiveRecord::Base.table_name_prefix = ""
      FriendlyId::Slug.table_name = without_prefix
    end
  end
end

class HistoryTestWithAutomaticSlugRegeneration < HistoryTest
  class Manual < ActiveRecord::Base
    extend FriendlyId
    friendly_id :name, use: [:slugged, :history]

    def should_generate_new_friendly_id?
      slug.blank? or name_changed?
    end
  end

  def model_class
    Manual
  end

  test "should allow reversion back to a previously used slug" do
    with_instance_of(model_class, name: "foo") do |record|
      record.name = "bar"
      record.save!
      assert_equal "bar", record.friendly_id
      record.name = "foo"
      record.save!
      assert_equal "foo", record.friendly_id
    end
  end
end

class DependentDestroyTest < TestCaseClass
  include FriendlyId::Test

  class FalseManual < ActiveRecord::Base
    self.table_name = "manuals"

    extend FriendlyId
    friendly_id :name, use: :history, dependent: false
  end

  class DefaultManual < ActiveRecord::Base
    self.table_name = "manuals"

    extend FriendlyId
    friendly_id :name, use: :history
  end

  test "should allow disabling of dependent destroy" do
    transaction do
      assert FriendlyId::Slug.find_by_slug("foo").nil?
      l = FalseManual.create! name: "foo"
      assert FriendlyId::Slug.find_by_slug("foo").present?
      l.destroy
      assert FriendlyId::Slug.find_by_slug("foo").present?
    end
  end

  test "should dependently destroy by default" do
    transaction do
      assert FriendlyId::Slug.find_by_slug("baz").nil?
      l = DefaultManual.create! name: "baz"
      assert FriendlyId::Slug.find_by_slug("baz").present?
      l.destroy
      assert FriendlyId::Slug.find_by_slug("baz").nil?
    end
  end
end

if ActiveRecord::VERSION::STRING >= "5.0"
  class HistoryTestWithParanoidDeletes < HistoryTest
    class ParanoidRecord < ActiveRecord::Base
      extend FriendlyId
      friendly_id :name, use: :history, dependent: false

      default_scope { where(deleted_at: nil) }
    end

    def model_class
      ParanoidRecord
    end

    test "slug should have a sluggable even when soft deleted by a library" do
      transaction do
        assert FriendlyId::Slug.find_by_slug("paranoid").nil?
        record = model_class.create(name: "paranoid")
        assert FriendlyId::Slug.find_by_slug("paranoid").present?

        record.update deleted_at: Time.now

        orphan_slug = FriendlyId::Slug.find_by_slug("paranoid")
        assert orphan_slug.present?, "Orphaned slug should exist"

        assert orphan_slug.valid?, "Errors: #{orphan_slug.errors.full_messages}"
        assert orphan_slug.sluggable.present?, "Orphaned slug should still find corresponding paranoid sluggable"
      end
    end
  end
end

class HistoryTestWithSti < HistoryTest
  class Journalist < ActiveRecord::Base
    extend FriendlyId
    friendly_id :name, use: [:slugged, :history]
  end

  class Editorialist < Journalist
  end

  def model_class
    Editorialist
  end
end

class HistoryTestWithFriendlyFinders < HistoryTest
  class Journalist < ActiveRecord::Base
    extend FriendlyId
    friendly_id :name, use: [:slugged, :finders, :history]
  end

  class Restaurant < ActiveRecord::Base
    extend FriendlyId
    belongs_to :city
    friendly_id :name, use: [:slugged, :history, :finders]
  end

  test "should be findable by old slugs" do
    [Journalist, Restaurant].each do |model_class|
      with_instance_of(model_class) do |record|
        old_friendly_id = record.friendly_id
        record.name = record.name + "b"
        record.slug = nil
        record.save!
        begin
          assert model_class.find(old_friendly_id)
          assert model_class.exists?(old_friendly_id), "should exist? by old id for #{model_class.name}"
        rescue ActiveRecord::RecordNotFound
          flunk "Could not find record by old id for #{model_class.name}"
        end
      end
    end
  end
end

class HistoryTestWithFindersBeforeHistory < HistoryTest
  class Novelist < ActiveRecord::Base
    has_many :novels
  end

  class Novel < ActiveRecord::Base
    extend FriendlyId

    belongs_to :novelist

    friendly_id :name, use: [:finders, :history]

    def should_generate_new_friendly_id?
      slug.blank? || name_changed?
    end
  end

  test "should be findable by old slug through has_many association" do
    transaction do
      novelist = Novelist.create!(name: "Stephen King")
      novel = novelist.novels.create(name: "Rita Hayworth and Shawshank Redemption")
      slug = novel.slug
      novel.name = "Shawshank Redemption"
      novel.save!
      assert_equal novel, Novel.find(slug)
      assert_equal novel, novelist.novels.find(slug)
    end
  end
end

class City < ActiveRecord::Base
  has_many :restaurants
end

class Restaurant < ActiveRecord::Base
  extend FriendlyId
  belongs_to :city
  friendly_id :name, use: [:scoped, :history], scope: :city
end

class ScopedHistoryTest < TestCaseClass
  include FriendlyId::Test
  include FriendlyId::Test::Shared::Core

  def model_class
    Restaurant
  end

  test "should find old scoped slugs" do
    transaction do
      city = City.create!
      with_instance_of(Restaurant) do |record|
        record.city = city

        record.name = "x"
        record.slug = nil
        record.save!

        record.name = "y"
        record.slug = nil
        record.save!

        assert_equal city.restaurants.friendly.find("x"), city.restaurants.friendly.find("y")
      end
    end
  end

  test "should consider old scoped slugs when creating slugs" do
    transaction do
      city = City.create!
      with_instance_of(Restaurant) do |record|
        record.city = city

        record.name = "x"
        record.slug = nil
        record.save!

        record.name = "y"
        record.slug = nil
        record.save!

        second_record = model_class.create! city: city, name: "x"
        assert_match(/x-.+/, second_record.friendly_id)

        third_record = model_class.create! city: city, name: "y"
        assert_match(/y-.+/, third_record.friendly_id)
      end
    end
  end

  test "should record history when scope changes" do
    transaction do
      city1 = City.create!
      city2 = City.create!
      with_instance_of(Restaurant) do |record|
        record.name = "x"
        record.slug = nil

        record.city = city1
        record.save!
        assert_equal("city_id:#{city1.id}", record.slugs.reload.first.scope)
        assert_equal("x", record.slugs.reload.first.slug)

        record.city = city2
        record.save!
        assert_equal("city_id:#{city2.id}", record.slugs.reload.first.scope)

        record.name = "y"
        record.slug = nil
        record.city = city1
        record.save!
        assert_equal("city_id:#{city1.id}", record.slugs.reload.first.scope)
        assert_equal("y", record.slugs.reload.first.slug)
      end
    end
  end

  test "should allow equal slugs in different scopes" do
    transaction do
      city = City.create!
      second_city = City.create!
      record = model_class.create! city: city, name: "x"
      second_record = model_class.create! city: second_city, name: "x"

      assert_equal record.slug, second_record.slug
    end
  end
end