acts_as_paranoid 0.6.1 → 0.7.2

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.
@@ -1,13 +1,15 @@
1
- require 'test_helper'
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
2
4
 
3
5
  class MultipleDefaultScopesTest < ParanoidBaseTest
4
6
  def setup
5
7
  setup_db
6
8
 
7
- ParanoidPolygon.create! :sides => 3
8
- ParanoidPolygon.create! :sides => 3
9
- ParanoidPolygon.create! :sides => 3
10
- ParanoidPolygon.create! :sides => 8
9
+ ParanoidPolygon.create! sides: 3
10
+ ParanoidPolygon.create! sides: 3
11
+ ParanoidPolygon.create! sides: 3
12
+ ParanoidPolygon.create! sides: 8
11
13
 
12
14
  assert_equal 3, ParanoidPolygon.count
13
15
  assert_equal 4, ParanoidPolygon.unscoped.count
data/test/test_helper.rb CHANGED
@@ -1,23 +1,41 @@
1
- require 'bundler'
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler"
2
4
  begin
3
- Bundler.require(:default, :development)
5
+ Bundler.load
4
6
  rescue Bundler::BundlerError => e
5
- $stderr.puts e.message
6
- $stderr.puts "Run `bundle install` to install missing gems"
7
+ warn e.message
8
+ warn "Run `bundle install` to install missing gems"
7
9
  exit e.status_code
8
10
  end
9
11
 
10
- require 'acts_as_paranoid'
11
- require 'minitest/autorun'
12
+ if RUBY_ENGINE == "jruby"
13
+ # Workaround for issue in I18n/JRuby combo.
14
+ # See https://github.com/jruby/jruby/issues/6547 and
15
+ # https://github.com/ruby-i18n/i18n/issues/555
16
+ require "i18n/backend"
17
+ require "i18n/backend/simple"
18
+ end
19
+
20
+ require "simplecov"
21
+ SimpleCov.start do
22
+ enable_coverage :branch
23
+ end
24
+
25
+ require "acts_as_paranoid"
26
+ require "minitest/autorun"
27
+ require "minitest/focus"
12
28
 
13
29
  # Silence deprecation halfway through the test
14
30
  I18n.enforce_available_locales = true
15
31
 
16
- ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
32
+ ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
17
33
  ActiveRecord::Schema.verbose = false
18
34
 
35
+ # rubocop:disable Metrics/AbcSize
36
+ # rubocop:disable Metrics/MethodLength
19
37
  def setup_db
20
- ActiveRecord::Schema.define(:version => 1) do
38
+ ActiveRecord::Schema.define(version: 1) do # rubocop:disable Metrics/BlockLength
21
39
  create_table :paranoid_times do |t|
22
40
  t.string :name
23
41
  t.datetime :deleted_at
@@ -32,6 +50,8 @@ def setup_db
32
50
  t.boolean :is_deleted
33
51
  t.integer :paranoid_time_id
34
52
  t.integer :paranoid_with_counter_caches_count
53
+ t.integer :paranoid_with_touch_and_counter_caches_count
54
+ t.integer :custom_counter_cache
35
55
  timestamps t
36
56
  end
37
57
 
@@ -111,14 +131,15 @@ def setup_db
111
131
 
112
132
  create_table :super_paranoids do |t|
113
133
  t.string :type
114
- t.references :has_many_inherited_super_paranoidz, index: { name: 'index__sp_id_on_has_many_isp' }
134
+ t.references :has_many_inherited_super_paranoidz,
135
+ index: { name: "index__sp_id_on_has_many_isp" }
115
136
  t.datetime :deleted_at
116
137
 
117
138
  timestamps t
118
139
  end
119
140
 
120
141
  create_table :has_many_inherited_super_paranoidzs do |t|
121
- t.references :super_paranoidz, index: { name: 'index_has_many_isp_on_sp_id' }
142
+ t.references :super_paranoidz, index: { name: "index_has_many_isp_on_sp_id" }
122
143
  t.datetime :deleted_at
123
144
 
124
145
  timestamps t
@@ -165,7 +186,7 @@ def setup_db
165
186
  end
166
187
 
167
188
  create_table :paranoid_polygons do |t|
168
- t.integer :sides
189
+ t.integer :sides
169
190
  t.datetime :deleted_at
170
191
 
171
192
  timestamps t
@@ -184,7 +205,7 @@ def setup_db
184
205
 
185
206
  create_table :paranoid_boolean_not_nullables do |t|
186
207
  t.string :name
187
- t.boolean :deleted, :boolean, :null => false, :default => false
208
+ t.boolean :deleted, :boolean, null: false, default: false
188
209
  end
189
210
 
190
211
  create_table :paranoid_belongs_to_polymorphics do |t|
@@ -222,19 +243,18 @@ def setup_db
222
243
  end
223
244
  end
224
245
  end
246
+ # rubocop:enable Metrics/AbcSize
247
+ # rubocop:enable Metrics/MethodLength
225
248
 
226
249
  def timestamps(table)
227
- table.column :created_at , :timestamp, :null => false
228
- table.column :updated_at , :timestamp, :null => false
250
+ table.column :created_at, :timestamp, null: false
251
+ table.column :updated_at, :timestamp, null: false
229
252
  end
230
253
 
231
254
  def teardown_db
232
- tables = if ActiveRecord::VERSION::MAJOR < 5
233
- ActiveRecord::Base.connection.tables
234
- else
235
- ActiveRecord::Base.connection.data_sources
255
+ ActiveRecord::Base.connection.data_sources.each do |table|
256
+ ActiveRecord::Base.connection.drop_table(table)
236
257
  end
237
- tables.each { |table| ActiveRecord::Base.connection.drop_table(table) }
238
258
  end
239
259
 
240
260
  class ParanoidTime < ActiveRecord::Base
@@ -242,60 +262,106 @@ class ParanoidTime < ActiveRecord::Base
242
262
 
243
263
  validates_uniqueness_of :name
244
264
 
245
- has_many :paranoid_has_many_dependants, :dependent => :destroy
246
- has_many :paranoid_booleans, :dependent => :destroy
247
- has_many :not_paranoids, :dependent => :delete_all
248
- has_many :paranoid_sections, :dependent => :destroy
265
+ has_many :paranoid_has_many_dependants, dependent: :destroy
266
+ has_many :paranoid_booleans, dependent: :destroy
267
+ has_many :not_paranoids, dependent: :delete_all
268
+ has_many :paranoid_sections, dependent: :destroy
249
269
 
250
- has_one :has_one_not_paranoid, :dependent => :destroy
270
+ has_one :has_one_not_paranoid, dependent: :destroy
251
271
 
252
- belongs_to :not_paranoid, :dependent => :destroy
272
+ belongs_to :not_paranoid, dependent: :destroy
253
273
  end
254
274
 
255
275
  class ParanoidBoolean < ActiveRecord::Base
256
- acts_as_paranoid :column_type => "boolean", :column => "is_deleted"
276
+ acts_as_paranoid column_type: "boolean", column: "is_deleted"
257
277
  validates_as_paranoid
258
278
  validates_uniqueness_of_without_deleted :name
259
279
 
260
280
  belongs_to :paranoid_time
261
- has_one :paranoid_has_one_dependant, :dependent => :destroy
262
- has_many :paranoid_with_counter_cache, :dependent => :destroy
281
+ has_one :paranoid_has_one_dependant, dependent: :destroy
282
+ has_many :paranoid_with_counter_cache, dependent: :destroy
283
+ has_many :paranoid_with_custom_counter_cache, dependent: :destroy
284
+ has_many :paranoid_with_touch_and_counter_cache, dependent: :destroy
285
+ has_many :paranoid_with_touch, dependent: :destroy
263
286
  end
264
287
 
265
288
  class ParanoidString < ActiveRecord::Base
266
- acts_as_paranoid :column_type => "string", :column => "deleted", :deleted_value => "dead"
289
+ acts_as_paranoid column_type: "string", column: "deleted", deleted_value: "dead"
267
290
  end
268
291
 
269
292
  class NotParanoid < ActiveRecord::Base
270
293
  end
271
294
 
272
295
  class ParanoidNoDoubleTapDestroysFully < ActiveRecord::Base
273
- acts_as_paranoid :double_tap_destroys_fully => false
296
+ acts_as_paranoid double_tap_destroys_fully: false
274
297
  end
275
298
 
276
299
  class HasOneNotParanoid < ActiveRecord::Base
277
- belongs_to :paranoid_time, :with_deleted => true
300
+ belongs_to :paranoid_time, with_deleted: true
278
301
  end
279
302
 
280
303
  class DoubleHasOneNotParanoid < HasOneNotParanoid
281
- belongs_to :paranoid_time, :with_deleted => true
282
- belongs_to :paranoid_time, :with_deleted => true
304
+ belongs_to :paranoid_time, with_deleted: true
305
+ begin
306
+ verbose = $VERBOSE
307
+ $VERBOSE = false
308
+ belongs_to :paranoid_time, with_deleted: true
309
+ ensure
310
+ $VERBOSE = verbose
311
+ end
283
312
  end
284
313
 
285
314
  class ParanoidWithCounterCache < ActiveRecord::Base
286
315
  acts_as_paranoid
287
- belongs_to :paranoid_boolean, :counter_cache => true
316
+ belongs_to :paranoid_boolean, counter_cache: true
317
+ end
318
+
319
+ class ParanoidWithCustomCounterCache < ActiveRecord::Base
320
+ self.table_name = "paranoid_with_counter_caches"
321
+
322
+ acts_as_paranoid
323
+ belongs_to :paranoid_boolean, counter_cache: :custom_counter_cache
324
+ end
325
+
326
+ class ParanoidWithCounterCacheOnOptionalBelognsTo < ActiveRecord::Base
327
+ self.table_name = "paranoid_with_counter_caches"
328
+
329
+ acts_as_paranoid
330
+ if ActiveRecord::VERSION::MAJOR < 5
331
+ belongs_to :paranoid_boolean, counter_cache: true, required: false
332
+ else
333
+ belongs_to :paranoid_boolean, counter_cache: true, optional: true
334
+ end
335
+ end
336
+
337
+ class ParanoidWithTouch < ActiveRecord::Base
338
+ self.table_name = "paranoid_with_counter_caches"
339
+ acts_as_paranoid
340
+ belongs_to :paranoid_boolean, touch: true
341
+ end
342
+
343
+ class ParanoidWithTouchAndCounterCache < ActiveRecord::Base
344
+ self.table_name = "paranoid_with_counter_caches"
345
+ acts_as_paranoid
346
+ belongs_to :paranoid_boolean, touch: true, counter_cache: true
288
347
  end
289
348
 
290
349
  class ParanoidHasManyDependant < ActiveRecord::Base
291
350
  acts_as_paranoid
292
351
  belongs_to :paranoid_time
293
- belongs_to :paranoid_time_with_scope, -> { where(name: 'hello').includes(:not_paranoid) }, :class_name => 'ParanoidTime', :foreign_key => :paranoid_time_id
294
- belongs_to :paranoid_time_with_deleted, :class_name => 'ParanoidTime', :foreign_key => :paranoid_time_id, :with_deleted => true
295
- belongs_to :paranoid_time_with_scope_with_deleted, -> { where(name: 'hello').includes(:not_paranoid) }, :class_name => 'ParanoidTime', :foreign_key => :paranoid_time_id, :with_deleted => true
296
- belongs_to :paranoid_time_polymorphic_with_deleted, :class_name => 'ParanoidTime', :foreign_key => :paranoid_time_id, :polymorphic => true, :with_deleted => true
352
+ belongs_to :paranoid_time_with_scope,
353
+ -> { where(name: "hello").includes(:not_paranoid) },
354
+ class_name: "ParanoidTime", foreign_key: :paranoid_time_id
355
+ belongs_to :paranoid_time_with_deleted, class_name: "ParanoidTime",
356
+ foreign_key: :paranoid_time_id, with_deleted: true
357
+ belongs_to :paranoid_time_with_scope_with_deleted,
358
+ -> { where(name: "hello").includes(:not_paranoid) },
359
+ class_name: "ParanoidTime", foreign_key: :paranoid_time_id, with_deleted: true
360
+ belongs_to :paranoid_time_polymorphic_with_deleted, class_name: "ParanoidTime",
361
+ foreign_key: :paranoid_time_id,
362
+ polymorphic: true, with_deleted: true
297
363
 
298
- belongs_to :paranoid_belongs_dependant, :dependent => :destroy
364
+ belongs_to :paranoid_belongs_dependant, dependent: :destroy
299
365
  end
300
366
 
301
367
  class ParanoidBelongsDependant < ActiveRecord::Base
@@ -313,13 +379,14 @@ end
313
379
  class ParanoidWithCallback < ActiveRecord::Base
314
380
  acts_as_paranoid
315
381
 
316
- attr_accessor :called_before_destroy, :called_after_destroy, :called_after_commit_on_destroy
317
- attr_accessor :called_before_recover, :called_after_recover
382
+ attr_accessor :called_before_destroy, :called_after_destroy,
383
+ :called_after_commit_on_destroy, :called_before_recover,
384
+ :called_after_recover
318
385
 
319
386
  before_destroy :call_me_before_destroy
320
387
  after_destroy :call_me_after_destroy
321
388
 
322
- after_commit :call_me_after_commit_on_destroy, :on => :destroy
389
+ after_commit :call_me_after_commit_on_destroy, on: :destroy
323
390
 
324
391
  before_recover :call_me_before_recover
325
392
  after_recover :call_me_after_recover
@@ -352,14 +419,14 @@ end
352
419
 
353
420
  class ParanoidDestroyCompany < ActiveRecord::Base
354
421
  acts_as_paranoid
355
- validates :name, :presence => true
356
- has_many :paranoid_products, :dependent => :destroy
422
+ validates :name, presence: true
423
+ has_many :paranoid_products, dependent: :destroy
357
424
  end
358
425
 
359
426
  class ParanoidDeleteCompany < ActiveRecord::Base
360
427
  acts_as_paranoid
361
- validates :name, :presence => true
362
- has_many :paranoid_products, :dependent => :delete_all
428
+ validates :name, presence: true
429
+ has_many :paranoid_products, dependent: :delete_all
363
430
  end
364
431
 
365
432
  class ParanoidProduct < ActiveRecord::Base
@@ -375,22 +442,21 @@ class SuperParanoid < ActiveRecord::Base
375
442
  end
376
443
 
377
444
  class HasManyInheritedSuperParanoidz < ActiveRecord::Base
378
- has_many :super_paranoidz, :class_name => "InheritedParanoid", :dependent => :destroy
445
+ has_many :super_paranoidz, class_name: "InheritedParanoid", dependent: :destroy
379
446
  end
380
447
 
381
448
  class InheritedParanoid < SuperParanoid
382
449
  acts_as_paranoid
383
450
  end
384
451
 
385
-
386
452
  class ParanoidManyManyParentLeft < ActiveRecord::Base
387
453
  has_many :paranoid_many_many_children
388
- has_many :paranoid_many_many_parent_rights, :through => :paranoid_many_many_children
454
+ has_many :paranoid_many_many_parent_rights, through: :paranoid_many_many_children
389
455
  end
390
456
 
391
457
  class ParanoidManyManyParentRight < ActiveRecord::Base
392
458
  has_many :paranoid_many_many_children
393
- has_many :paranoid_many_many_parent_lefts, :through => :paranoid_many_many_children
459
+ has_many :paranoid_many_many_parent_lefts, through: :paranoid_many_many_children
394
460
  end
395
461
 
396
462
  class ParanoidManyManyChild < ActiveRecord::Base
@@ -401,21 +467,21 @@ end
401
467
 
402
468
  class ParanoidWithScopedValidation < ActiveRecord::Base
403
469
  acts_as_paranoid
404
- validates_uniqueness_of :name, :scope => :category
470
+ validates_uniqueness_of :name, scope: :category
405
471
  end
406
472
 
407
473
  class ParanoidBelongsToPolymorphic < ActiveRecord::Base
408
474
  acts_as_paranoid
409
- belongs_to :parent, :polymorphic => true, :with_deleted => true
475
+ belongs_to :parent, polymorphic: true, with_deleted: true
410
476
  end
411
477
 
412
478
  class NotParanoidHasManyAsParent < ActiveRecord::Base
413
- has_many :paranoid_belongs_to_polymorphics, :as => :parent, :dependent => :destroy
479
+ has_many :paranoid_belongs_to_polymorphics, as: :parent, dependent: :destroy
414
480
  end
415
481
 
416
482
  class ParanoidHasManyAsParent < ActiveRecord::Base
417
483
  acts_as_paranoid
418
- has_many :paranoid_belongs_to_polymorphics, :as => :parent, :dependent => :destroy
484
+ has_many :paranoid_belongs_to_polymorphics, as: :parent, dependent: :destroy
419
485
  end
420
486
 
421
487
  class ParanoidBaseTest < ActiveSupport::TestCase
@@ -423,13 +489,13 @@ class ParanoidBaseTest < ActiveSupport::TestCase
423
489
  setup_db
424
490
 
425
491
  ["paranoid", "really paranoid", "extremely paranoid"].each do |name|
426
- ParanoidTime.create! :name => name
427
- ParanoidBoolean.create! :name => name
492
+ ParanoidTime.create! name: name
493
+ ParanoidBoolean.create! name: name
428
494
  end
429
495
 
430
- ParanoidString.create! :name => "strings can be paranoid"
431
- NotParanoid.create! :name => "no paranoid goals"
432
- ParanoidWithCallback.create! :name => "paranoid with callbacks"
496
+ ParanoidString.create! name: "strings can be paranoid"
497
+ NotParanoid.create! name: "no paranoid goals"
498
+ ParanoidWithCallback.create! name: "paranoid with callbacks"
433
499
  end
434
500
 
435
501
  def teardown
@@ -463,9 +529,9 @@ class ParanoidForest < ActiveRecord::Base
463
529
 
464
530
  ActiveRecord::Base.logger = Logger.new(StringIO.new)
465
531
 
466
- scope :rainforest, lambda{ where(:rainforest => true) }
532
+ scope :rainforest, -> { where(rainforest: true) }
467
533
 
468
- has_many :paranoid_trees, :dependent => :destroy
534
+ has_many :paranoid_trees, dependent: :destroy
469
535
  end
470
536
 
471
537
  class ParanoidTree < ActiveRecord::Base
@@ -476,7 +542,7 @@ end
476
542
 
477
543
  class ParanoidPolygon < ActiveRecord::Base
478
544
  acts_as_paranoid
479
- default_scope { where('sides = ?', 3) }
545
+ default_scope { where("sides = ?", 3) }
480
546
  end
481
547
 
482
548
  class ParanoidAndroid < ActiveRecord::Base
@@ -486,10 +552,14 @@ end
486
552
  class ParanoidSection < ActiveRecord::Base
487
553
  acts_as_paranoid
488
554
  belongs_to :paranoid_time
489
- belongs_to :paranoid_thing, :polymorphic => true, :dependent => :destroy
555
+ belongs_to :paranoid_thing, polymorphic: true, dependent: :destroy
490
556
  end
491
557
 
492
558
  class ParanoidBooleanNotNullable < ActiveRecord::Base
493
- acts_as_paranoid column: 'deleted', column_type: 'boolean', allow_nulls: false
559
+ acts_as_paranoid column: "deleted", column_type: "boolean", allow_nulls: false
494
560
  end
495
561
 
562
+ class ParanoidWithExplicitTableNameAfterMacro < ActiveRecord::Base
563
+ acts_as_paranoid
564
+ self.table_name = "explicit_table"
565
+ end
@@ -1,4 +1,6 @@
1
- require 'test_helper'
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
2
4
 
3
5
  class InheritanceTest < ParanoidBaseTest
4
6
  def test_destroy_dependents_with_inheritance
@@ -1,20 +1,22 @@
1
- require 'test_helper'
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
2
4
 
3
5
  class RelationsTest < ParanoidBaseTest
4
6
  def setup
5
7
  setup_db
6
8
 
7
- @paranoid_forest_1 = ParanoidForest.create! :name => "ParanoidForest #1"
8
- @paranoid_forest_2 = ParanoidForest.create! :name => "ParanoidForest #2", :rainforest => true
9
- @paranoid_forest_3 = ParanoidForest.create! :name => "ParanoidForest #3", :rainforest => true
9
+ @paranoid_forest_1 = ParanoidForest.create! name: "ParanoidForest #1"
10
+ @paranoid_forest_2 = ParanoidForest.create! name: "ParanoidForest #2", rainforest: true
11
+ @paranoid_forest_3 = ParanoidForest.create! name: "ParanoidForest #3", rainforest: true
10
12
 
11
13
  assert_equal 3, ParanoidForest.count
12
14
  assert_equal 2, ParanoidForest.rainforest.count
13
15
 
14
- @paranoid_forest_1.paranoid_trees.create! :name => 'ParanoidTree #1'
15
- @paranoid_forest_1.paranoid_trees.create! :name => 'ParanoidTree #2'
16
- @paranoid_forest_2.paranoid_trees.create! :name => 'ParanoidTree #3'
17
- @paranoid_forest_2.paranoid_trees.create! :name => 'ParanoidTree #4'
16
+ @paranoid_forest_1.paranoid_trees.create! name: "ParanoidTree #1"
17
+ @paranoid_forest_1.paranoid_trees.create! name: "ParanoidTree #2"
18
+ @paranoid_forest_2.paranoid_trees.create! name: "ParanoidTree #3"
19
+ @paranoid_forest_2.paranoid_trees.create! name: "ParanoidTree #4"
18
20
 
19
21
  assert_equal 4, ParanoidTree.count
20
22
  end
@@ -86,13 +88,15 @@ class RelationsTest < ParanoidBaseTest
86
88
  assert_equal 2, @paranoid_forest_2.paranoid_trees.with_deleted.count
87
89
  end
88
90
 
89
- def test_real_removal_through_relation
90
- # destroy!: aliased to delete
91
+ def test_real_removal_through_relation_with_destroy_bang
92
+ # Relation.destroy!: aliased to delete
91
93
  ParanoidForest.rainforest.destroy!(@paranoid_forest_3)
92
94
  assert_equal 1, ParanoidForest.rainforest.count
93
95
  assert_equal 1, ParanoidForest.rainforest.with_deleted.count
94
96
  assert_equal 0, ParanoidForest.rainforest.only_deleted.count
97
+ end
95
98
 
99
+ def test_two_step_real_removal_through_relation_with_destroy
96
100
  # destroy: two-step through a relation
97
101
  paranoid_tree = @paranoid_forest_1.paranoid_trees.first
98
102
  @paranoid_forest_1.paranoid_trees.order(:id).destroy(paranoid_tree.id)
@@ -100,14 +104,18 @@ class RelationsTest < ParanoidBaseTest
100
104
  assert_equal 1, @paranoid_forest_1.paranoid_trees.count
101
105
  assert_equal 1, @paranoid_forest_1.paranoid_trees.with_deleted.count
102
106
  assert_equal 0, @paranoid_forest_1.paranoid_trees.only_deleted.count
107
+ end
103
108
 
109
+ def test_two_step_real_removal_through_relation_with_destroy_all
104
110
  # destroy_all: two-step through a relation
105
111
  @paranoid_forest_1.paranoid_trees.order(:id).destroy_all
106
112
  @paranoid_forest_1.paranoid_trees.only_deleted.destroy_all
107
113
  assert_equal 0, @paranoid_forest_1.paranoid_trees.count
108
114
  assert_equal 0, @paranoid_forest_1.paranoid_trees.with_deleted.count
109
115
  assert_equal 0, @paranoid_forest_1.paranoid_trees.only_deleted.count
116
+ end
110
117
 
118
+ def test_real_removal_through_relation_with_delete_all_bang
111
119
  # delete_all!: through a relation
112
120
  @paranoid_forest_2.paranoid_trees.order(:id).delete_all!
113
121
  assert_equal 0, @paranoid_forest_2.paranoid_trees.count