acts_as_paranoid 0.6.0 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,14 +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
- # Naturally, the default scope for humans is male. Sexism++
8
- ParanoidPolygon.create! :sides => 3
9
- ParanoidPolygon.create! :sides => 3
10
- ParanoidPolygon.create! :sides => 3
11
- ParanoidPolygon.create! :sides => 8
9
+ ParanoidPolygon.create! sides: 3
10
+ ParanoidPolygon.create! sides: 3
11
+ ParanoidPolygon.create! sides: 3
12
+ ParanoidPolygon.create! sides: 8
12
13
 
13
14
  assert_equal 3, ParanoidPolygon.count
14
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
@@ -31,7 +49,9 @@ def setup_db
31
49
  t.string :name
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
@@ -148,7 +169,7 @@ def setup_db
148
169
  timestamps t
149
170
  end
150
171
 
151
- create_table :paranoid_forests do |t|
172
+ create_table :paranoid_forests do |t|
152
173
  t.string :name
153
174
  t.boolean :rainforest
154
175
  t.datetime :deleted_at
@@ -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|
@@ -208,21 +229,32 @@ def setup_db
208
229
 
209
230
  timestamps t
210
231
  end
232
+
233
+ create_table :paranoid_no_double_tap_destroys_fullies do |t|
234
+ t.datetime :deleted_at
235
+ end
236
+
237
+ create_table :paranoid_with_counter_caches do |t|
238
+ t.string :name
239
+ t.datetime :deleted_at
240
+ t.integer :paranoid_boolean_id
241
+
242
+ timestamps t
243
+ end
211
244
  end
212
245
  end
246
+ # rubocop:enable Metrics/AbcSize
247
+ # rubocop:enable Metrics/MethodLength
213
248
 
214
249
  def timestamps(table)
215
- table.column :created_at , :timestamp, :null => false
216
- table.column :updated_at , :timestamp, :null => false
250
+ table.column :created_at, :timestamp, null: false
251
+ table.column :updated_at, :timestamp, null: false
217
252
  end
218
253
 
219
254
  def teardown_db
220
- tables = if ActiveRecord::VERSION::MAJOR < 5
221
- ActiveRecord::Base.connection.tables
222
- else
223
- ActiveRecord::Base.connection.data_sources
255
+ ActiveRecord::Base.connection.data_sources.each do |table|
256
+ ActiveRecord::Base.connection.drop_table(table)
224
257
  end
225
- tables.each { |table| ActiveRecord::Base.connection.drop_table(table) }
226
258
  end
227
259
 
228
260
  class ParanoidTime < ActiveRecord::Base
@@ -230,49 +262,106 @@ class ParanoidTime < ActiveRecord::Base
230
262
 
231
263
  validates_uniqueness_of :name
232
264
 
233
- has_many :paranoid_has_many_dependants, :dependent => :destroy
234
- has_many :paranoid_booleans, :dependent => :destroy
235
- has_many :not_paranoids, :dependent => :delete_all
236
- 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
237
269
 
238
- has_one :has_one_not_paranoid, :dependent => :destroy
270
+ has_one :has_one_not_paranoid, dependent: :destroy
239
271
 
240
- belongs_to :not_paranoid, :dependent => :destroy
272
+ belongs_to :not_paranoid, dependent: :destroy
241
273
  end
242
274
 
243
275
  class ParanoidBoolean < ActiveRecord::Base
244
- acts_as_paranoid :column_type => "boolean", :column => "is_deleted"
276
+ acts_as_paranoid column_type: "boolean", column: "is_deleted"
245
277
  validates_as_paranoid
246
278
  validates_uniqueness_of_without_deleted :name
247
279
 
248
280
  belongs_to :paranoid_time
249
- has_one :paranoid_has_one_dependant, :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
250
286
  end
251
287
 
252
288
  class ParanoidString < ActiveRecord::Base
253
- acts_as_paranoid :column_type => "string", :column => "deleted", :deleted_value => "dead"
289
+ acts_as_paranoid column_type: "string", column: "deleted", deleted_value: "dead"
254
290
  end
255
291
 
256
292
  class NotParanoid < ActiveRecord::Base
257
293
  end
258
294
 
295
+ class ParanoidNoDoubleTapDestroysFully < ActiveRecord::Base
296
+ acts_as_paranoid double_tap_destroys_fully: false
297
+ end
298
+
259
299
  class HasOneNotParanoid < ActiveRecord::Base
260
- belongs_to :paranoid_time, :with_deleted => true
300
+ belongs_to :paranoid_time, with_deleted: true
261
301
  end
262
302
 
263
303
  class DoubleHasOneNotParanoid < HasOneNotParanoid
264
- belongs_to :paranoid_time, :with_deleted => true
265
- 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
312
+ end
313
+
314
+ class ParanoidWithCounterCache < ActiveRecord::Base
315
+ acts_as_paranoid
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
266
347
  end
267
348
 
268
349
  class ParanoidHasManyDependant < ActiveRecord::Base
269
350
  acts_as_paranoid
270
351
  belongs_to :paranoid_time
271
- belongs_to :paranoid_time_with_scope, -> { includes(:not_paranoid) }, :class_name => 'ParanoidTime', :foreign_key => :paranoid_time_id
272
- belongs_to :paranoid_time_with_deleted, :class_name => 'ParanoidTime', :foreign_key => :paranoid_time_id, :with_deleted => true
273
- 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
274
363
 
275
- belongs_to :paranoid_belongs_dependant, :dependent => :destroy
364
+ belongs_to :paranoid_belongs_dependant, dependent: :destroy
276
365
  end
277
366
 
278
367
  class ParanoidBelongsDependant < ActiveRecord::Base
@@ -290,13 +379,14 @@ end
290
379
  class ParanoidWithCallback < ActiveRecord::Base
291
380
  acts_as_paranoid
292
381
 
293
- attr_accessor :called_before_destroy, :called_after_destroy, :called_after_commit_on_destroy
294
- 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
295
385
 
296
386
  before_destroy :call_me_before_destroy
297
387
  after_destroy :call_me_after_destroy
298
388
 
299
- after_commit :call_me_after_commit_on_destroy, :on => :destroy
389
+ after_commit :call_me_after_commit_on_destroy, on: :destroy
300
390
 
301
391
  before_recover :call_me_before_recover
302
392
  after_recover :call_me_after_recover
@@ -329,14 +419,14 @@ end
329
419
 
330
420
  class ParanoidDestroyCompany < ActiveRecord::Base
331
421
  acts_as_paranoid
332
- validates :name, :presence => true
333
- has_many :paranoid_products, :dependent => :destroy
422
+ validates :name, presence: true
423
+ has_many :paranoid_products, dependent: :destroy
334
424
  end
335
425
 
336
426
  class ParanoidDeleteCompany < ActiveRecord::Base
337
427
  acts_as_paranoid
338
- validates :name, :presence => true
339
- has_many :paranoid_products, :dependent => :delete_all
428
+ validates :name, presence: true
429
+ has_many :paranoid_products, dependent: :delete_all
340
430
  end
341
431
 
342
432
  class ParanoidProduct < ActiveRecord::Base
@@ -352,22 +442,21 @@ class SuperParanoid < ActiveRecord::Base
352
442
  end
353
443
 
354
444
  class HasManyInheritedSuperParanoidz < ActiveRecord::Base
355
- has_many :super_paranoidz, :class_name => "InheritedParanoid", :dependent => :destroy
445
+ has_many :super_paranoidz, class_name: "InheritedParanoid", dependent: :destroy
356
446
  end
357
447
 
358
448
  class InheritedParanoid < SuperParanoid
359
449
  acts_as_paranoid
360
450
  end
361
451
 
362
-
363
452
  class ParanoidManyManyParentLeft < ActiveRecord::Base
364
453
  has_many :paranoid_many_many_children
365
- 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
366
455
  end
367
456
 
368
457
  class ParanoidManyManyParentRight < ActiveRecord::Base
369
458
  has_many :paranoid_many_many_children
370
- 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
371
460
  end
372
461
 
373
462
  class ParanoidManyManyChild < ActiveRecord::Base
@@ -378,21 +467,21 @@ end
378
467
 
379
468
  class ParanoidWithScopedValidation < ActiveRecord::Base
380
469
  acts_as_paranoid
381
- validates_uniqueness_of :name, :scope => :category
470
+ validates_uniqueness_of :name, scope: :category
382
471
  end
383
472
 
384
473
  class ParanoidBelongsToPolymorphic < ActiveRecord::Base
385
474
  acts_as_paranoid
386
- belongs_to :parent, :polymorphic => true, :with_deleted => true
475
+ belongs_to :parent, polymorphic: true, with_deleted: true
387
476
  end
388
477
 
389
478
  class NotParanoidHasManyAsParent < ActiveRecord::Base
390
- has_many :paranoid_belongs_to_polymorphics, :as => :parent, :dependent => :destroy
479
+ has_many :paranoid_belongs_to_polymorphics, as: :parent, dependent: :destroy
391
480
  end
392
481
 
393
482
  class ParanoidHasManyAsParent < ActiveRecord::Base
394
483
  acts_as_paranoid
395
- has_many :paranoid_belongs_to_polymorphics, :as => :parent, :dependent => :destroy
484
+ has_many :paranoid_belongs_to_polymorphics, as: :parent, dependent: :destroy
396
485
  end
397
486
 
398
487
  class ParanoidBaseTest < ActiveSupport::TestCase
@@ -400,13 +489,13 @@ class ParanoidBaseTest < ActiveSupport::TestCase
400
489
  setup_db
401
490
 
402
491
  ["paranoid", "really paranoid", "extremely paranoid"].each do |name|
403
- ParanoidTime.create! :name => name
404
- ParanoidBoolean.create! :name => name
492
+ ParanoidTime.create! name: name
493
+ ParanoidBoolean.create! name: name
405
494
  end
406
495
 
407
- ParanoidString.create! :name => "strings can be paranoid"
408
- NotParanoid.create! :name => "no paranoid goals"
409
- 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"
410
499
  end
411
500
 
412
501
  def teardown
@@ -440,9 +529,9 @@ class ParanoidForest < ActiveRecord::Base
440
529
 
441
530
  ActiveRecord::Base.logger = Logger.new(StringIO.new)
442
531
 
443
- scope :rainforest, lambda{ where(:rainforest => true) }
532
+ scope :rainforest, -> { where(rainforest: true) }
444
533
 
445
- has_many :paranoid_trees, :dependent => :destroy
534
+ has_many :paranoid_trees, dependent: :destroy
446
535
  end
447
536
 
448
537
  class ParanoidTree < ActiveRecord::Base
@@ -453,7 +542,7 @@ end
453
542
 
454
543
  class ParanoidPolygon < ActiveRecord::Base
455
544
  acts_as_paranoid
456
- default_scope { where('sides = ?', 3) }
545
+ default_scope { where("sides = ?", 3) }
457
546
  end
458
547
 
459
548
  class ParanoidAndroid < ActiveRecord::Base
@@ -463,10 +552,14 @@ end
463
552
  class ParanoidSection < ActiveRecord::Base
464
553
  acts_as_paranoid
465
554
  belongs_to :paranoid_time
466
- belongs_to :paranoid_thing, :polymorphic => true, :dependent => :destroy
555
+ belongs_to :paranoid_thing, polymorphic: true, dependent: :destroy
467
556
  end
468
557
 
469
558
  class ParanoidBooleanNotNullable < ActiveRecord::Base
470
- acts_as_paranoid column: 'deleted', column_type: 'boolean', allow_nulls: false
559
+ acts_as_paranoid column: "deleted", column_type: "boolean", allow_nulls: false
471
560
  end
472
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