acts_as_paranoid 0.6.3 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -1,23 +1,32 @@
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
+ require "simplecov"
13
+ SimpleCov.start do
14
+ enable_coverage :branch
15
+ end
16
+
17
+ require "acts_as_paranoid"
18
+ require "minitest/autorun"
12
19
 
13
20
  # Silence deprecation halfway through the test
14
21
  I18n.enforce_available_locales = true
15
22
 
16
- ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
23
+ ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
17
24
  ActiveRecord::Schema.verbose = false
18
25
 
26
+ # rubocop:disable Metrics/AbcSize
27
+ # rubocop:disable Metrics/MethodLength
19
28
  def setup_db
20
- ActiveRecord::Schema.define(:version => 1) do
29
+ ActiveRecord::Schema.define(version: 1) do # rubocop:disable Metrics/BlockLength
21
30
  create_table :paranoid_times do |t|
22
31
  t.string :name
23
32
  t.datetime :deleted_at
@@ -112,14 +121,15 @@ def setup_db
112
121
 
113
122
  create_table :super_paranoids do |t|
114
123
  t.string :type
115
- t.references :has_many_inherited_super_paranoidz, index: { name: 'index__sp_id_on_has_many_isp' }
124
+ t.references :has_many_inherited_super_paranoidz,
125
+ index: { name: "index__sp_id_on_has_many_isp" }
116
126
  t.datetime :deleted_at
117
127
 
118
128
  timestamps t
119
129
  end
120
130
 
121
131
  create_table :has_many_inherited_super_paranoidzs do |t|
122
- t.references :super_paranoidz, index: { name: 'index_has_many_isp_on_sp_id' }
132
+ t.references :super_paranoidz, index: { name: "index_has_many_isp_on_sp_id" }
123
133
  t.datetime :deleted_at
124
134
 
125
135
  timestamps t
@@ -166,7 +176,7 @@ def setup_db
166
176
  end
167
177
 
168
178
  create_table :paranoid_polygons do |t|
169
- t.integer :sides
179
+ t.integer :sides
170
180
  t.datetime :deleted_at
171
181
 
172
182
  timestamps t
@@ -185,7 +195,7 @@ def setup_db
185
195
 
186
196
  create_table :paranoid_boolean_not_nullables do |t|
187
197
  t.string :name
188
- t.boolean :deleted, :boolean, :null => false, :default => false
198
+ t.boolean :deleted, :boolean, null: false, default: false
189
199
  end
190
200
 
191
201
  create_table :paranoid_belongs_to_polymorphics do |t|
@@ -223,19 +233,18 @@ def setup_db
223
233
  end
224
234
  end
225
235
  end
236
+ # rubocop:enable Metrics/AbcSize
237
+ # rubocop:enable Metrics/MethodLength
226
238
 
227
239
  def timestamps(table)
228
- table.column :created_at , :timestamp, :null => false
229
- table.column :updated_at , :timestamp, :null => false
240
+ table.column :created_at, :timestamp, null: false
241
+ table.column :updated_at, :timestamp, null: false
230
242
  end
231
243
 
232
244
  def teardown_db
233
- tables = if ActiveRecord::VERSION::MAJOR < 5
234
- ActiveRecord::Base.connection.tables
235
- else
236
- ActiveRecord::Base.connection.data_sources
245
+ ActiveRecord::Base.connection.data_sources.each do |table|
246
+ ActiveRecord::Base.connection.drop_table(table)
237
247
  end
238
- tables.each { |table| ActiveRecord::Base.connection.drop_table(table) }
239
248
  end
240
249
 
241
250
  class ParanoidTime < ActiveRecord::Base
@@ -243,50 +252,56 @@ class ParanoidTime < ActiveRecord::Base
243
252
 
244
253
  validates_uniqueness_of :name
245
254
 
246
- has_many :paranoid_has_many_dependants, :dependent => :destroy
247
- has_many :paranoid_booleans, :dependent => :destroy
248
- has_many :not_paranoids, :dependent => :delete_all
249
- has_many :paranoid_sections, :dependent => :destroy
255
+ has_many :paranoid_has_many_dependants, dependent: :destroy
256
+ has_many :paranoid_booleans, dependent: :destroy
257
+ has_many :not_paranoids, dependent: :delete_all
258
+ has_many :paranoid_sections, dependent: :destroy
250
259
 
251
- has_one :has_one_not_paranoid, :dependent => :destroy
260
+ has_one :has_one_not_paranoid, dependent: :destroy
252
261
 
253
- belongs_to :not_paranoid, :dependent => :destroy
262
+ belongs_to :not_paranoid, dependent: :destroy
254
263
  end
255
264
 
256
265
  class ParanoidBoolean < ActiveRecord::Base
257
- acts_as_paranoid :column_type => "boolean", :column => "is_deleted"
266
+ acts_as_paranoid column_type: "boolean", column: "is_deleted"
258
267
  validates_as_paranoid
259
268
  validates_uniqueness_of_without_deleted :name
260
269
 
261
270
  belongs_to :paranoid_time
262
- has_one :paranoid_has_one_dependant, :dependent => :destroy
263
- has_many :paranoid_with_counter_cache, :dependent => :destroy
264
- has_many :paranoid_with_custom_counter_cache, :dependent => :destroy
271
+ has_one :paranoid_has_one_dependant, dependent: :destroy
272
+ has_many :paranoid_with_counter_cache, dependent: :destroy
273
+ has_many :paranoid_with_custom_counter_cache, dependent: :destroy
265
274
  end
266
275
 
267
276
  class ParanoidString < ActiveRecord::Base
268
- acts_as_paranoid :column_type => "string", :column => "deleted", :deleted_value => "dead"
277
+ acts_as_paranoid column_type: "string", column: "deleted", deleted_value: "dead"
269
278
  end
270
279
 
271
280
  class NotParanoid < ActiveRecord::Base
272
281
  end
273
282
 
274
283
  class ParanoidNoDoubleTapDestroysFully < ActiveRecord::Base
275
- acts_as_paranoid :double_tap_destroys_fully => false
284
+ acts_as_paranoid double_tap_destroys_fully: false
276
285
  end
277
286
 
278
287
  class HasOneNotParanoid < ActiveRecord::Base
279
- belongs_to :paranoid_time, :with_deleted => true
288
+ belongs_to :paranoid_time, with_deleted: true
280
289
  end
281
290
 
282
291
  class DoubleHasOneNotParanoid < HasOneNotParanoid
283
- belongs_to :paranoid_time, :with_deleted => true
284
- belongs_to :paranoid_time, :with_deleted => true
292
+ belongs_to :paranoid_time, with_deleted: true
293
+ begin
294
+ verbose = $VERBOSE
295
+ $VERBOSE = false
296
+ belongs_to :paranoid_time, with_deleted: true
297
+ ensure
298
+ $VERBOSE = verbose
299
+ end
285
300
  end
286
301
 
287
302
  class ParanoidWithCounterCache < ActiveRecord::Base
288
303
  acts_as_paranoid
289
- belongs_to :paranoid_boolean, :counter_cache => true
304
+ belongs_to :paranoid_boolean, counter_cache: true
290
305
  end
291
306
 
292
307
  class ParanoidWithCustomCounterCache < ActiveRecord::Base
@@ -310,12 +325,19 @@ end
310
325
  class ParanoidHasManyDependant < ActiveRecord::Base
311
326
  acts_as_paranoid
312
327
  belongs_to :paranoid_time
313
- belongs_to :paranoid_time_with_scope, -> { where(name: 'hello').includes(:not_paranoid) }, :class_name => 'ParanoidTime', :foreign_key => :paranoid_time_id
314
- belongs_to :paranoid_time_with_deleted, :class_name => 'ParanoidTime', :foreign_key => :paranoid_time_id, :with_deleted => true
315
- 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
316
- belongs_to :paranoid_time_polymorphic_with_deleted, :class_name => 'ParanoidTime', :foreign_key => :paranoid_time_id, :polymorphic => true, :with_deleted => true
328
+ belongs_to :paranoid_time_with_scope,
329
+ -> { where(name: "hello").includes(:not_paranoid) },
330
+ class_name: "ParanoidTime", foreign_key: :paranoid_time_id
331
+ belongs_to :paranoid_time_with_deleted, class_name: "ParanoidTime",
332
+ foreign_key: :paranoid_time_id, with_deleted: true
333
+ belongs_to :paranoid_time_with_scope_with_deleted,
334
+ -> { where(name: "hello").includes(:not_paranoid) },
335
+ class_name: "ParanoidTime", foreign_key: :paranoid_time_id, with_deleted: true
336
+ belongs_to :paranoid_time_polymorphic_with_deleted, class_name: "ParanoidTime",
337
+ foreign_key: :paranoid_time_id,
338
+ polymorphic: true, with_deleted: true
317
339
 
318
- belongs_to :paranoid_belongs_dependant, :dependent => :destroy
340
+ belongs_to :paranoid_belongs_dependant, dependent: :destroy
319
341
  end
320
342
 
321
343
  class ParanoidBelongsDependant < ActiveRecord::Base
@@ -333,13 +355,14 @@ end
333
355
  class ParanoidWithCallback < ActiveRecord::Base
334
356
  acts_as_paranoid
335
357
 
336
- attr_accessor :called_before_destroy, :called_after_destroy, :called_after_commit_on_destroy
337
- attr_accessor :called_before_recover, :called_after_recover
358
+ attr_accessor :called_before_destroy, :called_after_destroy,
359
+ :called_after_commit_on_destroy, :called_before_recover,
360
+ :called_after_recover
338
361
 
339
362
  before_destroy :call_me_before_destroy
340
363
  after_destroy :call_me_after_destroy
341
364
 
342
- after_commit :call_me_after_commit_on_destroy, :on => :destroy
365
+ after_commit :call_me_after_commit_on_destroy, on: :destroy
343
366
 
344
367
  before_recover :call_me_before_recover
345
368
  after_recover :call_me_after_recover
@@ -372,14 +395,14 @@ end
372
395
 
373
396
  class ParanoidDestroyCompany < ActiveRecord::Base
374
397
  acts_as_paranoid
375
- validates :name, :presence => true
376
- has_many :paranoid_products, :dependent => :destroy
398
+ validates :name, presence: true
399
+ has_many :paranoid_products, dependent: :destroy
377
400
  end
378
401
 
379
402
  class ParanoidDeleteCompany < ActiveRecord::Base
380
403
  acts_as_paranoid
381
- validates :name, :presence => true
382
- has_many :paranoid_products, :dependent => :delete_all
404
+ validates :name, presence: true
405
+ has_many :paranoid_products, dependent: :delete_all
383
406
  end
384
407
 
385
408
  class ParanoidProduct < ActiveRecord::Base
@@ -395,22 +418,21 @@ class SuperParanoid < ActiveRecord::Base
395
418
  end
396
419
 
397
420
  class HasManyInheritedSuperParanoidz < ActiveRecord::Base
398
- has_many :super_paranoidz, :class_name => "InheritedParanoid", :dependent => :destroy
421
+ has_many :super_paranoidz, class_name: "InheritedParanoid", dependent: :destroy
399
422
  end
400
423
 
401
424
  class InheritedParanoid < SuperParanoid
402
425
  acts_as_paranoid
403
426
  end
404
427
 
405
-
406
428
  class ParanoidManyManyParentLeft < ActiveRecord::Base
407
429
  has_many :paranoid_many_many_children
408
- has_many :paranoid_many_many_parent_rights, :through => :paranoid_many_many_children
430
+ has_many :paranoid_many_many_parent_rights, through: :paranoid_many_many_children
409
431
  end
410
432
 
411
433
  class ParanoidManyManyParentRight < ActiveRecord::Base
412
434
  has_many :paranoid_many_many_children
413
- has_many :paranoid_many_many_parent_lefts, :through => :paranoid_many_many_children
435
+ has_many :paranoid_many_many_parent_lefts, through: :paranoid_many_many_children
414
436
  end
415
437
 
416
438
  class ParanoidManyManyChild < ActiveRecord::Base
@@ -421,21 +443,21 @@ end
421
443
 
422
444
  class ParanoidWithScopedValidation < ActiveRecord::Base
423
445
  acts_as_paranoid
424
- validates_uniqueness_of :name, :scope => :category
446
+ validates_uniqueness_of :name, scope: :category
425
447
  end
426
448
 
427
449
  class ParanoidBelongsToPolymorphic < ActiveRecord::Base
428
450
  acts_as_paranoid
429
- belongs_to :parent, :polymorphic => true, :with_deleted => true
451
+ belongs_to :parent, polymorphic: true, with_deleted: true
430
452
  end
431
453
 
432
454
  class NotParanoidHasManyAsParent < ActiveRecord::Base
433
- has_many :paranoid_belongs_to_polymorphics, :as => :parent, :dependent => :destroy
455
+ has_many :paranoid_belongs_to_polymorphics, as: :parent, dependent: :destroy
434
456
  end
435
457
 
436
458
  class ParanoidHasManyAsParent < ActiveRecord::Base
437
459
  acts_as_paranoid
438
- has_many :paranoid_belongs_to_polymorphics, :as => :parent, :dependent => :destroy
460
+ has_many :paranoid_belongs_to_polymorphics, as: :parent, dependent: :destroy
439
461
  end
440
462
 
441
463
  class ParanoidBaseTest < ActiveSupport::TestCase
@@ -443,13 +465,13 @@ class ParanoidBaseTest < ActiveSupport::TestCase
443
465
  setup_db
444
466
 
445
467
  ["paranoid", "really paranoid", "extremely paranoid"].each do |name|
446
- ParanoidTime.create! :name => name
447
- ParanoidBoolean.create! :name => name
468
+ ParanoidTime.create! name: name
469
+ ParanoidBoolean.create! name: name
448
470
  end
449
471
 
450
- ParanoidString.create! :name => "strings can be paranoid"
451
- NotParanoid.create! :name => "no paranoid goals"
452
- ParanoidWithCallback.create! :name => "paranoid with callbacks"
472
+ ParanoidString.create! name: "strings can be paranoid"
473
+ NotParanoid.create! name: "no paranoid goals"
474
+ ParanoidWithCallback.create! name: "paranoid with callbacks"
453
475
  end
454
476
 
455
477
  def teardown
@@ -483,9 +505,9 @@ class ParanoidForest < ActiveRecord::Base
483
505
 
484
506
  ActiveRecord::Base.logger = Logger.new(StringIO.new)
485
507
 
486
- scope :rainforest, lambda{ where(:rainforest => true) }
508
+ scope :rainforest, -> { where(rainforest: true) }
487
509
 
488
- has_many :paranoid_trees, :dependent => :destroy
510
+ has_many :paranoid_trees, dependent: :destroy
489
511
  end
490
512
 
491
513
  class ParanoidTree < ActiveRecord::Base
@@ -496,7 +518,7 @@ end
496
518
 
497
519
  class ParanoidPolygon < ActiveRecord::Base
498
520
  acts_as_paranoid
499
- default_scope { where('sides = ?', 3) }
521
+ default_scope { where("sides = ?", 3) }
500
522
  end
501
523
 
502
524
  class ParanoidAndroid < ActiveRecord::Base
@@ -506,11 +528,11 @@ end
506
528
  class ParanoidSection < ActiveRecord::Base
507
529
  acts_as_paranoid
508
530
  belongs_to :paranoid_time
509
- belongs_to :paranoid_thing, :polymorphic => true, :dependent => :destroy
531
+ belongs_to :paranoid_thing, polymorphic: true, dependent: :destroy
510
532
  end
511
533
 
512
534
  class ParanoidBooleanNotNullable < ActiveRecord::Base
513
- acts_as_paranoid column: 'deleted', column_type: 'boolean', allow_nulls: false
535
+ acts_as_paranoid column: "deleted", column_type: "boolean", allow_nulls: false
514
536
  end
515
537
 
516
538
  class ParanoidWithExplicitTableNameAfterMacro < ActiveRecord::Base
@@ -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
@@ -1,8 +1,10 @@
1
- require 'test_helper'
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
2
4
 
3
5
  class ValidatesUniquenessTest < ParanoidBaseTest
4
6
  def test_should_include_deleted_by_default
5
- ParanoidTime.new(:name => 'paranoid').tap do |record|
7
+ ParanoidTime.new(name: "paranoid").tap do |record|
6
8
  assert !record.valid?
7
9
  ParanoidTime.first.destroy
8
10
  assert !record.valid?
@@ -12,7 +14,7 @@ class ValidatesUniquenessTest < ParanoidBaseTest
12
14
  end
13
15
 
14
16
  def test_should_validate_without_deleted
15
- ParanoidBoolean.new(:name => 'paranoid').tap do |record|
17
+ ParanoidBoolean.new(name: "paranoid").tap do |record|
16
18
  refute record.valid?
17
19
  ParanoidBoolean.first.destroy
18
20
  assert record.valid?
@@ -22,8 +24,8 @@ class ValidatesUniquenessTest < ParanoidBaseTest
22
24
  end
23
25
 
24
26
  def test_models_with_scoped_validations_can_be_multiply_deleted
25
- model_a = ParanoidWithScopedValidation.create(:name => "Model A", :category => "Category A")
26
- model_b = ParanoidWithScopedValidation.create(:name => "Model B", :category => "Category B")
27
+ model_a = ParanoidWithScopedValidation.create(name: "Model A", category: "Category A")
28
+ model_b = ParanoidWithScopedValidation.create(name: "Model B", category: "Category B")
27
29
 
28
30
  ParanoidWithScopedValidation.delete([model_a.id, model_b.id])
29
31
 
@@ -32,8 +34,8 @@ class ValidatesUniquenessTest < ParanoidBaseTest
32
34
  end
33
35
 
34
36
  def test_models_with_scoped_validations_can_be_multiply_destroyed
35
- model_a = ParanoidWithScopedValidation.create(:name => "Model A", :category => "Category A")
36
- model_b = ParanoidWithScopedValidation.create(:name => "Model B", :category => "Category B")
37
+ model_a = ParanoidWithScopedValidation.create(name: "Model A", category: "Category A")
38
+ model_b = ParanoidWithScopedValidation.create(name: "Model B", category: "Category B")
37
39
 
38
40
  ParanoidWithScopedValidation.destroy([model_a.id, model_b.id])
39
41