acts_as_paranoid 0.5.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,52 +1,53 @@
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
- ParanoidHuman.create! :gender => 'male'
9
- ParanoidHuman.create! :gender => 'male'
10
- ParanoidHuman.create! :gender => 'male'
11
- ParanoidHuman.create! :gender => 'female'
9
+ ParanoidPolygon.create! sides: 3
10
+ ParanoidPolygon.create! sides: 3
11
+ ParanoidPolygon.create! sides: 3
12
+ ParanoidPolygon.create! sides: 8
12
13
 
13
- assert_equal 3, ParanoidHuman.count
14
- assert_equal 4, ParanoidHuman.unscoped.count
14
+ assert_equal 3, ParanoidPolygon.count
15
+ assert_equal 4, ParanoidPolygon.unscoped.count
15
16
  end
16
17
 
17
18
  def test_fake_removal_with_multiple_default_scope
18
- ParanoidHuman.first.destroy
19
- assert_equal 2, ParanoidHuman.count
20
- assert_equal 3, ParanoidHuman.with_deleted.count
21
- assert_equal 1, ParanoidHuman.only_deleted.count
22
- assert_equal 4, ParanoidHuman.unscoped.count
23
-
24
- ParanoidHuman.destroy_all
25
- assert_equal 0, ParanoidHuman.count
26
- assert_equal 3, ParanoidHuman.with_deleted.count
27
- assert_equal 3, ParanoidHuman.with_deleted.count
28
- assert_equal 4, ParanoidHuman.unscoped.count
19
+ ParanoidPolygon.first.destroy
20
+ assert_equal 2, ParanoidPolygon.count
21
+ assert_equal 3, ParanoidPolygon.with_deleted.count
22
+ assert_equal 1, ParanoidPolygon.only_deleted.count
23
+ assert_equal 4, ParanoidPolygon.unscoped.count
24
+
25
+ ParanoidPolygon.destroy_all
26
+ assert_equal 0, ParanoidPolygon.count
27
+ assert_equal 3, ParanoidPolygon.with_deleted.count
28
+ assert_equal 3, ParanoidPolygon.with_deleted.count
29
+ assert_equal 4, ParanoidPolygon.unscoped.count
29
30
  end
30
31
 
31
32
  def test_real_removal_with_multiple_default_scope
32
33
  # two-step
33
- ParanoidHuman.first.destroy
34
- ParanoidHuman.only_deleted.first.destroy
35
- assert_equal 2, ParanoidHuman.count
36
- assert_equal 2, ParanoidHuman.with_deleted.count
37
- assert_equal 0, ParanoidHuman.only_deleted.count
38
- assert_equal 3, ParanoidHuman.unscoped.count
39
-
40
- ParanoidHuman.first.destroy_fully!
41
- assert_equal 1, ParanoidHuman.count
42
- assert_equal 1, ParanoidHuman.with_deleted.count
43
- assert_equal 0, ParanoidHuman.only_deleted.count
44
- assert_equal 2, ParanoidHuman.unscoped.count
45
-
46
- ParanoidHuman.delete_all!
47
- assert_equal 0, ParanoidHuman.count
48
- assert_equal 0, ParanoidHuman.with_deleted.count
49
- assert_equal 0, ParanoidHuman.only_deleted.count
50
- assert_equal 1, ParanoidHuman.unscoped.count
34
+ ParanoidPolygon.first.destroy
35
+ ParanoidPolygon.only_deleted.first.destroy
36
+ assert_equal 2, ParanoidPolygon.count
37
+ assert_equal 2, ParanoidPolygon.with_deleted.count
38
+ assert_equal 0, ParanoidPolygon.only_deleted.count
39
+ assert_equal 3, ParanoidPolygon.unscoped.count
40
+
41
+ ParanoidPolygon.first.destroy_fully!
42
+ assert_equal 1, ParanoidPolygon.count
43
+ assert_equal 1, ParanoidPolygon.with_deleted.count
44
+ assert_equal 0, ParanoidPolygon.only_deleted.count
45
+ assert_equal 2, ParanoidPolygon.unscoped.count
46
+
47
+ ParanoidPolygon.delete_all!
48
+ assert_equal 0, ParanoidPolygon.count
49
+ assert_equal 0, ParanoidPolygon.with_deleted.count
50
+ assert_equal 0, ParanoidPolygon.only_deleted.count
51
+ assert_equal 1, ParanoidPolygon.unscoped.count
51
52
  end
52
53
  end
@@ -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
@@ -31,7 +40,8 @@ def setup_db
31
40
  t.string :name
32
41
  t.boolean :is_deleted
33
42
  t.integer :paranoid_time_id
34
-
43
+ t.integer :paranoid_with_counter_caches_count
44
+ t.integer :custom_counter_cache
35
45
  timestamps t
36
46
  end
37
47
 
@@ -111,14 +121,15 @@ def setup_db
111
121
 
112
122
  create_table :super_paranoids do |t|
113
123
  t.string :type
114
- 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" }
115
126
  t.datetime :deleted_at
116
127
 
117
128
  timestamps t
118
129
  end
119
130
 
120
131
  create_table :has_many_inherited_super_paranoidzs do |t|
121
- 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" }
122
133
  t.datetime :deleted_at
123
134
 
124
135
  timestamps t
@@ -148,7 +159,7 @@ def setup_db
148
159
  timestamps t
149
160
  end
150
161
 
151
- create_table :paranoid_forests do |t|
162
+ create_table :paranoid_forests do |t|
152
163
  t.string :name
153
164
  t.boolean :rainforest
154
165
  t.datetime :deleted_at
@@ -164,8 +175,8 @@ def setup_db
164
175
  timestamps t
165
176
  end
166
177
 
167
- create_table :paranoid_humen do |t|
168
- t.string :gender
178
+ create_table :paranoid_polygons do |t|
179
+ t.integer :sides
169
180
  t.datetime :deleted_at
170
181
 
171
182
  timestamps t
@@ -184,7 +195,7 @@ def setup_db
184
195
 
185
196
  create_table :paranoid_boolean_not_nullables do |t|
186
197
  t.string :name
187
- t.boolean :deleted, :boolean, :null => false, :default => false
198
+ t.boolean :deleted, :boolean, null: false, default: false
188
199
  end
189
200
 
190
201
  create_table :paranoid_belongs_to_polymorphics do |t|
@@ -193,36 +204,47 @@ def setup_db
193
204
  t.integer :parent_id
194
205
  t.datetime :deleted_at
195
206
 
196
- t.timestamps
207
+ timestamps t
197
208
  end
198
209
 
199
210
  create_table :not_paranoid_has_many_as_parents do |t|
200
211
  t.string :name
201
212
 
202
- t.timestamps
213
+ timestamps t
203
214
  end
204
215
 
205
216
  create_table :paranoid_has_many_as_parents do |t|
206
217
  t.string :name
207
218
  t.datetime :deleted_at
208
219
 
209
- t.timestamps
220
+ timestamps t
221
+ end
222
+
223
+ create_table :paranoid_no_double_tap_destroys_fullies do |t|
224
+ t.datetime :deleted_at
225
+ end
226
+
227
+ create_table :paranoid_with_counter_caches do |t|
228
+ t.string :name
229
+ t.datetime :deleted_at
230
+ t.integer :paranoid_boolean_id
231
+
232
+ timestamps t
210
233
  end
211
234
  end
212
235
  end
236
+ # rubocop:enable Metrics/AbcSize
237
+ # rubocop:enable Metrics/MethodLength
213
238
 
214
239
  def timestamps(table)
215
- table.column :created_at , :timestamp, :null => false
216
- table.column :updated_at , :timestamp, :null => false
240
+ table.column :created_at, :timestamp, null: false
241
+ table.column :updated_at, :timestamp, null: false
217
242
  end
218
243
 
219
244
  def teardown_db
220
- tables = if ActiveRecord::VERSION::MAJOR < 5
221
- ActiveRecord::Base.connection.tables
222
- else
223
- ActiveRecord::Base.connection.data_sources
245
+ ActiveRecord::Base.connection.data_sources.each do |table|
246
+ ActiveRecord::Base.connection.drop_table(table)
224
247
  end
225
- tables.each { |table| ActiveRecord::Base.connection.drop_table(table) }
226
248
  end
227
249
 
228
250
  class ParanoidTime < ActiveRecord::Base
@@ -230,49 +252,92 @@ class ParanoidTime < ActiveRecord::Base
230
252
 
231
253
  validates_uniqueness_of :name
232
254
 
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
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
237
259
 
238
- has_one :has_one_not_paranoid, :dependent => :destroy
260
+ has_one :has_one_not_paranoid, dependent: :destroy
239
261
 
240
- belongs_to :not_paranoid, :dependent => :destroy
262
+ belongs_to :not_paranoid, dependent: :destroy
241
263
  end
242
264
 
243
265
  class ParanoidBoolean < ActiveRecord::Base
244
- acts_as_paranoid :column_type => "boolean", :column => "is_deleted"
266
+ acts_as_paranoid column_type: "boolean", column: "is_deleted"
245
267
  validates_as_paranoid
246
268
  validates_uniqueness_of_without_deleted :name
247
269
 
248
270
  belongs_to :paranoid_time
249
- has_one :paranoid_has_one_dependant, :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
250
274
  end
251
275
 
252
276
  class ParanoidString < ActiveRecord::Base
253
- acts_as_paranoid :column_type => "string", :column => "deleted", :deleted_value => "dead"
277
+ acts_as_paranoid column_type: "string", column: "deleted", deleted_value: "dead"
254
278
  end
255
279
 
256
280
  class NotParanoid < ActiveRecord::Base
257
281
  end
258
282
 
283
+ class ParanoidNoDoubleTapDestroysFully < ActiveRecord::Base
284
+ acts_as_paranoid double_tap_destroys_fully: false
285
+ end
286
+
259
287
  class HasOneNotParanoid < ActiveRecord::Base
260
- belongs_to :paranoid_time, :with_deleted => true
288
+ belongs_to :paranoid_time, with_deleted: true
261
289
  end
262
290
 
263
291
  class DoubleHasOneNotParanoid < HasOneNotParanoid
264
- belongs_to :paranoid_time, :with_deleted => true
265
- 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
300
+ end
301
+
302
+ class ParanoidWithCounterCache < ActiveRecord::Base
303
+ acts_as_paranoid
304
+ belongs_to :paranoid_boolean, counter_cache: true
305
+ end
306
+
307
+ class ParanoidWithCustomCounterCache < ActiveRecord::Base
308
+ self.table_name = "paranoid_with_counter_caches"
309
+
310
+ acts_as_paranoid
311
+ belongs_to :paranoid_boolean, counter_cache: :custom_counter_cache
312
+ end
313
+
314
+ class ParanoidWithCounterCacheOnOptionalBelognsTo < ActiveRecord::Base
315
+ self.table_name = "paranoid_with_counter_caches"
316
+
317
+ acts_as_paranoid
318
+ if ActiveRecord::VERSION::MAJOR < 5
319
+ belongs_to :paranoid_boolean, counter_cache: true, required: false
320
+ else
321
+ belongs_to :paranoid_boolean, counter_cache: true, optional: true
322
+ end
266
323
  end
267
324
 
268
325
  class ParanoidHasManyDependant < ActiveRecord::Base
269
326
  acts_as_paranoid
270
327
  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
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
274
339
 
275
- belongs_to :paranoid_belongs_dependant, :dependent => :destroy
340
+ belongs_to :paranoid_belongs_dependant, dependent: :destroy
276
341
  end
277
342
 
278
343
  class ParanoidBelongsDependant < ActiveRecord::Base
@@ -290,13 +355,14 @@ end
290
355
  class ParanoidWithCallback < ActiveRecord::Base
291
356
  acts_as_paranoid
292
357
 
293
- attr_accessor :called_before_destroy, :called_after_destroy, :called_after_commit_on_destroy
294
- 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
295
361
 
296
362
  before_destroy :call_me_before_destroy
297
363
  after_destroy :call_me_after_destroy
298
364
 
299
- after_commit :call_me_after_commit_on_destroy, :on => :destroy
365
+ after_commit :call_me_after_commit_on_destroy, on: :destroy
300
366
 
301
367
  before_recover :call_me_before_recover
302
368
  after_recover :call_me_after_recover
@@ -329,14 +395,14 @@ end
329
395
 
330
396
  class ParanoidDestroyCompany < ActiveRecord::Base
331
397
  acts_as_paranoid
332
- validates :name, :presence => true
333
- has_many :paranoid_products, :dependent => :destroy
398
+ validates :name, presence: true
399
+ has_many :paranoid_products, dependent: :destroy
334
400
  end
335
401
 
336
402
  class ParanoidDeleteCompany < ActiveRecord::Base
337
403
  acts_as_paranoid
338
- validates :name, :presence => true
339
- has_many :paranoid_products, :dependent => :delete_all
404
+ validates :name, presence: true
405
+ has_many :paranoid_products, dependent: :delete_all
340
406
  end
341
407
 
342
408
  class ParanoidProduct < ActiveRecord::Base
@@ -352,22 +418,21 @@ class SuperParanoid < ActiveRecord::Base
352
418
  end
353
419
 
354
420
  class HasManyInheritedSuperParanoidz < ActiveRecord::Base
355
- has_many :super_paranoidz, :class_name => "InheritedParanoid", :dependent => :destroy
421
+ has_many :super_paranoidz, class_name: "InheritedParanoid", dependent: :destroy
356
422
  end
357
423
 
358
424
  class InheritedParanoid < SuperParanoid
359
425
  acts_as_paranoid
360
426
  end
361
427
 
362
-
363
428
  class ParanoidManyManyParentLeft < ActiveRecord::Base
364
429
  has_many :paranoid_many_many_children
365
- 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
366
431
  end
367
432
 
368
433
  class ParanoidManyManyParentRight < ActiveRecord::Base
369
434
  has_many :paranoid_many_many_children
370
- 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
371
436
  end
372
437
 
373
438
  class ParanoidManyManyChild < ActiveRecord::Base
@@ -378,21 +443,21 @@ end
378
443
 
379
444
  class ParanoidWithScopedValidation < ActiveRecord::Base
380
445
  acts_as_paranoid
381
- validates_uniqueness_of :name, :scope => :category
446
+ validates_uniqueness_of :name, scope: :category
382
447
  end
383
448
 
384
449
  class ParanoidBelongsToPolymorphic < ActiveRecord::Base
385
450
  acts_as_paranoid
386
- belongs_to :parent, :polymorphic => true, :with_deleted => true
451
+ belongs_to :parent, polymorphic: true, with_deleted: true
387
452
  end
388
453
 
389
454
  class NotParanoidHasManyAsParent < ActiveRecord::Base
390
- has_many :paranoid_belongs_to_polymorphics, :as => :parent, :dependent => :destroy
455
+ has_many :paranoid_belongs_to_polymorphics, as: :parent, dependent: :destroy
391
456
  end
392
457
 
393
458
  class ParanoidHasManyAsParent < ActiveRecord::Base
394
459
  acts_as_paranoid
395
- has_many :paranoid_belongs_to_polymorphics, :as => :parent, :dependent => :destroy
460
+ has_many :paranoid_belongs_to_polymorphics, as: :parent, dependent: :destroy
396
461
  end
397
462
 
398
463
  class ParanoidBaseTest < ActiveSupport::TestCase
@@ -400,13 +465,13 @@ class ParanoidBaseTest < ActiveSupport::TestCase
400
465
  setup_db
401
466
 
402
467
  ["paranoid", "really paranoid", "extremely paranoid"].each do |name|
403
- ParanoidTime.create! :name => name
404
- ParanoidBoolean.create! :name => name
468
+ ParanoidTime.create! name: name
469
+ ParanoidBoolean.create! name: name
405
470
  end
406
471
 
407
- ParanoidString.create! :name => "strings can be paranoid"
408
- NotParanoid.create! :name => "no paranoid goals"
409
- 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"
410
475
  end
411
476
 
412
477
  def teardown
@@ -440,9 +505,9 @@ class ParanoidForest < ActiveRecord::Base
440
505
 
441
506
  ActiveRecord::Base.logger = Logger.new(StringIO.new)
442
507
 
443
- scope :rainforest, lambda{ where(:rainforest => true) }
508
+ scope :rainforest, -> { where(rainforest: true) }
444
509
 
445
- has_many :paranoid_trees, :dependent => :destroy
510
+ has_many :paranoid_trees, dependent: :destroy
446
511
  end
447
512
 
448
513
  class ParanoidTree < ActiveRecord::Base
@@ -451,9 +516,9 @@ class ParanoidTree < ActiveRecord::Base
451
516
  validates_presence_of :name
452
517
  end
453
518
 
454
- class ParanoidHuman < ActiveRecord::Base
519
+ class ParanoidPolygon < ActiveRecord::Base
455
520
  acts_as_paranoid
456
- default_scope { where('gender = ?', 'male') }
521
+ default_scope { where("sides = ?", 3) }
457
522
  end
458
523
 
459
524
  class ParanoidAndroid < ActiveRecord::Base
@@ -463,10 +528,14 @@ end
463
528
  class ParanoidSection < ActiveRecord::Base
464
529
  acts_as_paranoid
465
530
  belongs_to :paranoid_time
466
- belongs_to :paranoid_thing, :polymorphic => true, :dependent => :destroy
531
+ belongs_to :paranoid_thing, polymorphic: true, dependent: :destroy
467
532
  end
468
533
 
469
534
  class ParanoidBooleanNotNullable < ActiveRecord::Base
470
- acts_as_paranoid column: 'deleted', column_type: 'boolean', allow_nulls: false
535
+ acts_as_paranoid column: "deleted", column_type: "boolean", allow_nulls: false
471
536
  end
472
537
 
538
+ class ParanoidWithExplicitTableNameAfterMacro < ActiveRecord::Base
539
+ acts_as_paranoid
540
+ self.table_name = "explicit_table"
541
+ end