acts_as_paranoid 0.6.2 → 0.7.3

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,18 +1,31 @@
1
- require 'test_helper'
1
+ # frozen_string_literal: true
2
2
 
3
- class MultipleDefaultScopesTest < ParanoidBaseTest
3
+ require "test_helper"
4
+
5
+ class MultipleDefaultScopesTest < ActiveSupport::TestCase
4
6
  def setup
5
- setup_db
7
+ ActiveRecord::Schema.define(version: 1) do
8
+ create_table :paranoid_polygons do |t|
9
+ t.integer :sides
10
+ t.datetime :deleted_at
11
+
12
+ timestamps t
13
+ end
14
+ end
6
15
 
7
- ParanoidPolygon.create! :sides => 3
8
- ParanoidPolygon.create! :sides => 3
9
- ParanoidPolygon.create! :sides => 3
10
- ParanoidPolygon.create! :sides => 8
16
+ ParanoidPolygon.create! sides: 3
17
+ ParanoidPolygon.create! sides: 3
18
+ ParanoidPolygon.create! sides: 3
19
+ ParanoidPolygon.create! sides: 8
11
20
 
12
21
  assert_equal 3, ParanoidPolygon.count
13
22
  assert_equal 4, ParanoidPolygon.unscoped.count
14
23
  end
15
24
 
25
+ def teardown
26
+ teardown_db
27
+ end
28
+
16
29
  def test_fake_removal_with_multiple_default_scope
17
30
  ParanoidPolygon.first.destroy
18
31
  assert_equal 2, ParanoidPolygon.count
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ class DependentRecoveryTest < ActiveSupport::TestCase
6
+ class ParanoidForest < ActiveRecord::Base
7
+ acts_as_paranoid
8
+ has_many :paranoid_trees, dependent: :destroy
9
+ end
10
+
11
+ class ParanoidTree < ActiveRecord::Base
12
+ acts_as_paranoid
13
+ belongs_to :paranoid_forest, optional: false
14
+ end
15
+
16
+ def setup
17
+ ActiveRecord::Schema.define(version: 1) do
18
+ create_table :paranoid_forests do |t|
19
+ t.string :name
20
+ t.boolean :rainforest
21
+ t.datetime :deleted_at
22
+
23
+ timestamps t
24
+ end
25
+
26
+ create_table :paranoid_trees do |t|
27
+ t.integer :paranoid_forest_id
28
+ t.string :name
29
+ t.datetime :deleted_at
30
+
31
+ timestamps t
32
+ end
33
+ end
34
+ end
35
+
36
+ def teardown
37
+ teardown_db
38
+ end
39
+
40
+ def test_recover_dependent_records_with_required_belongs_to
41
+ forest = ParanoidForest.create! name: "forest"
42
+
43
+ tree = ParanoidTree.new name: "tree"
44
+ refute tree.valid?
45
+ tree.paranoid_forest = forest
46
+ assert tree.valid?
47
+ tree.save!
48
+
49
+ forest.destroy
50
+ forest.recover
51
+
52
+ assert_equal 1, ParanoidTree.count
53
+ end
54
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "test_helper"
4
+
5
+ class DeprecatedBehaviorTest < ActiveSupport::TestCase
6
+ class StringlyParanoid < ActiveRecord::Base
7
+ acts_as_paranoid column_type: "string", column: "foo", recovery_value: "alive"
8
+ end
9
+
10
+ def setup
11
+ ActiveRecord::Schema.define(version: 1) do
12
+ create_table :stringly_paranoids do |t|
13
+ t.string :foo
14
+
15
+ timestamps t
16
+ end
17
+ end
18
+ end
19
+
20
+ def teardown
21
+ teardown_db
22
+ end
23
+
24
+ def test_recovery_value
25
+ record = StringlyParanoid.create!
26
+ record.destroy
27
+ record.recover
28
+ assert_equal "alive", record.paranoid_value
29
+ end
30
+ end
data/test/test_helper.rb CHANGED
@@ -1,23 +1,45 @@
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
+ log_dir = File.expand_path("../log/", __dir__)
36
+ FileUtils.mkdir_p log_dir
37
+ file_path = File.join(log_dir, "test.log")
38
+ ActiveRecord::Base.logger = Logger.new(file_path)
39
+
40
+ # rubocop:disable Metrics/AbcSize
19
41
  def setup_db
20
- ActiveRecord::Schema.define(:version => 1) do
42
+ ActiveRecord::Schema.define(version: 1) do # rubocop:disable Metrics/BlockLength
21
43
  create_table :paranoid_times do |t|
22
44
  t.string :name
23
45
  t.datetime :deleted_at
@@ -32,6 +54,7 @@ def setup_db
32
54
  t.boolean :is_deleted
33
55
  t.integer :paranoid_time_id
34
56
  t.integer :paranoid_with_counter_caches_count
57
+ t.integer :paranoid_with_touch_and_counter_caches_count
35
58
  t.integer :custom_counter_cache
36
59
  timestamps t
37
60
  end
@@ -112,14 +135,15 @@ def setup_db
112
135
 
113
136
  create_table :super_paranoids do |t|
114
137
  t.string :type
115
- t.references :has_many_inherited_super_paranoidz, index: { name: 'index__sp_id_on_has_many_isp' }
138
+ t.references :has_many_inherited_super_paranoidz,
139
+ index: { name: "index__sp_id_on_has_many_isp" }
116
140
  t.datetime :deleted_at
117
141
 
118
142
  timestamps t
119
143
  end
120
144
 
121
145
  create_table :has_many_inherited_super_paranoidzs do |t|
122
- t.references :super_paranoidz, index: { name: 'index_has_many_isp_on_sp_id' }
146
+ t.references :super_paranoidz, index: { name: "index_has_many_isp_on_sp_id" }
123
147
  t.datetime :deleted_at
124
148
 
125
149
  timestamps t
@@ -149,24 +173,8 @@ def setup_db
149
173
  timestamps t
150
174
  end
151
175
 
152
- create_table :paranoid_forests do |t|
153
- t.string :name
154
- t.boolean :rainforest
155
- t.datetime :deleted_at
156
-
157
- timestamps t
158
- end
159
-
160
- create_table :paranoid_trees do |t|
161
- t.integer :paranoid_forest_id
162
- t.string :name
163
- t.datetime :deleted_at
164
-
165
- timestamps t
166
- end
167
-
168
176
  create_table :paranoid_polygons do |t|
169
- t.integer :sides
177
+ t.integer :sides
170
178
  t.datetime :deleted_at
171
179
 
172
180
  timestamps t
@@ -185,7 +193,7 @@ def setup_db
185
193
 
186
194
  create_table :paranoid_boolean_not_nullables do |t|
187
195
  t.string :name
188
- t.boolean :deleted, :boolean, :null => false, :default => false
196
+ t.boolean :deleted, :boolean, null: false, default: false
189
197
  end
190
198
 
191
199
  create_table :paranoid_belongs_to_polymorphics do |t|
@@ -221,21 +229,27 @@ def setup_db
221
229
 
222
230
  timestamps t
223
231
  end
232
+
233
+ create_table :paranoid_with_serialized_columns do |t|
234
+ t.string :name
235
+ t.datetime :deleted_at
236
+ t.string :colors
237
+
238
+ timestamps t
239
+ end
224
240
  end
225
241
  end
226
242
 
243
+ # rubocop:enable Metrics/AbcSize
227
244
  def timestamps(table)
228
- table.column :created_at , :timestamp, :null => false
229
- table.column :updated_at , :timestamp, :null => false
245
+ table.column :created_at, :timestamp, null: false
246
+ table.column :updated_at, :timestamp, null: false
230
247
  end
231
248
 
232
249
  def teardown_db
233
- tables = if ActiveRecord::VERSION::MAJOR < 5
234
- ActiveRecord::Base.connection.tables
235
- else
236
- ActiveRecord::Base.connection.data_sources
250
+ ActiveRecord::Base.connection.data_sources.each do |table|
251
+ ActiveRecord::Base.connection.drop_table(table)
237
252
  end
238
- tables.each { |table| ActiveRecord::Base.connection.drop_table(table) }
239
253
  end
240
254
 
241
255
  class ParanoidTime < ActiveRecord::Base
@@ -243,50 +257,59 @@ class ParanoidTime < ActiveRecord::Base
243
257
 
244
258
  validates_uniqueness_of :name
245
259
 
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
260
+ has_many :paranoid_has_many_dependants, dependent: :destroy
261
+ has_many :paranoid_booleans, dependent: :destroy
262
+ has_many :not_paranoids, dependent: :delete_all
263
+ has_many :paranoid_sections, dependent: :destroy
250
264
 
251
- has_one :has_one_not_paranoid, :dependent => :destroy
265
+ has_one :has_one_not_paranoid, dependent: :destroy
252
266
 
253
- belongs_to :not_paranoid, :dependent => :destroy
267
+ belongs_to :not_paranoid, dependent: :destroy
254
268
  end
255
269
 
256
270
  class ParanoidBoolean < ActiveRecord::Base
257
- acts_as_paranoid :column_type => "boolean", :column => "is_deleted"
271
+ acts_as_paranoid column_type: "boolean", column: "is_deleted"
258
272
  validates_as_paranoid
259
273
  validates_uniqueness_of_without_deleted :name
260
274
 
261
275
  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
276
+ has_one :paranoid_has_one_dependant, dependent: :destroy
277
+ has_many :paranoid_with_counter_cache, dependent: :destroy
278
+ has_many :paranoid_with_custom_counter_cache, dependent: :destroy
279
+ has_many :paranoid_with_touch_and_counter_cache, dependent: :destroy
280
+ has_many :paranoid_with_touch, dependent: :destroy
265
281
  end
266
282
 
267
283
  class ParanoidString < ActiveRecord::Base
268
- acts_as_paranoid :column_type => "string", :column => "deleted", :deleted_value => "dead"
284
+ acts_as_paranoid column_type: "string", column: "deleted", deleted_value: "dead"
269
285
  end
270
286
 
271
287
  class NotParanoid < ActiveRecord::Base
288
+ has_many :paranoid_times
272
289
  end
273
290
 
274
291
  class ParanoidNoDoubleTapDestroysFully < ActiveRecord::Base
275
- acts_as_paranoid :double_tap_destroys_fully => false
292
+ acts_as_paranoid double_tap_destroys_fully: false
276
293
  end
277
294
 
278
295
  class HasOneNotParanoid < ActiveRecord::Base
279
- belongs_to :paranoid_time, :with_deleted => true
296
+ belongs_to :paranoid_time, with_deleted: true
280
297
  end
281
298
 
282
299
  class DoubleHasOneNotParanoid < HasOneNotParanoid
283
- belongs_to :paranoid_time, :with_deleted => true
284
- belongs_to :paranoid_time, :with_deleted => true
300
+ belongs_to :paranoid_time, with_deleted: true
301
+ begin
302
+ verbose = $VERBOSE
303
+ $VERBOSE = false
304
+ belongs_to :paranoid_time, with_deleted: true
305
+ ensure
306
+ $VERBOSE = verbose
307
+ end
285
308
  end
286
309
 
287
310
  class ParanoidWithCounterCache < ActiveRecord::Base
288
311
  acts_as_paranoid
289
- belongs_to :paranoid_boolean, :counter_cache => true
312
+ belongs_to :paranoid_boolean, counter_cache: true
290
313
  end
291
314
 
292
315
  class ParanoidWithCustomCounterCache < ActiveRecord::Base
@@ -307,15 +330,34 @@ class ParanoidWithCounterCacheOnOptionalBelognsTo < ActiveRecord::Base
307
330
  end
308
331
  end
309
332
 
333
+ class ParanoidWithTouch < ActiveRecord::Base
334
+ self.table_name = "paranoid_with_counter_caches"
335
+ acts_as_paranoid
336
+ belongs_to :paranoid_boolean, touch: true
337
+ end
338
+
339
+ class ParanoidWithTouchAndCounterCache < ActiveRecord::Base
340
+ self.table_name = "paranoid_with_counter_caches"
341
+ acts_as_paranoid
342
+ belongs_to :paranoid_boolean, touch: true, counter_cache: true
343
+ end
344
+
310
345
  class ParanoidHasManyDependant < ActiveRecord::Base
311
346
  acts_as_paranoid
312
347
  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
348
+ belongs_to :paranoid_time_with_scope,
349
+ -> { where(name: "hello").includes(:not_paranoid) },
350
+ class_name: "ParanoidTime", foreign_key: :paranoid_time_id
351
+ belongs_to :paranoid_time_with_deleted, class_name: "ParanoidTime",
352
+ foreign_key: :paranoid_time_id, with_deleted: true
353
+ belongs_to :paranoid_time_with_scope_with_deleted,
354
+ -> { where(name: "hello").includes(:not_paranoid) },
355
+ class_name: "ParanoidTime", foreign_key: :paranoid_time_id, with_deleted: true
356
+ belongs_to :paranoid_time_polymorphic_with_deleted, class_name: "ParanoidTime",
357
+ foreign_key: :paranoid_time_id,
358
+ polymorphic: true, with_deleted: true
317
359
 
318
- belongs_to :paranoid_belongs_dependant, :dependent => :destroy
360
+ belongs_to :paranoid_belongs_dependant, dependent: :destroy
319
361
  end
320
362
 
321
363
  class ParanoidBelongsDependant < ActiveRecord::Base
@@ -333,13 +375,14 @@ end
333
375
  class ParanoidWithCallback < ActiveRecord::Base
334
376
  acts_as_paranoid
335
377
 
336
- attr_accessor :called_before_destroy, :called_after_destroy, :called_after_commit_on_destroy
337
- attr_accessor :called_before_recover, :called_after_recover
378
+ attr_accessor :called_before_destroy, :called_after_destroy,
379
+ :called_after_commit_on_destroy, :called_before_recover,
380
+ :called_after_recover
338
381
 
339
382
  before_destroy :call_me_before_destroy
340
383
  after_destroy :call_me_after_destroy
341
384
 
342
- after_commit :call_me_after_commit_on_destroy, :on => :destroy
385
+ after_commit :call_me_after_commit_on_destroy, on: :destroy
343
386
 
344
387
  before_recover :call_me_before_recover
345
388
  after_recover :call_me_after_recover
@@ -372,14 +415,14 @@ end
372
415
 
373
416
  class ParanoidDestroyCompany < ActiveRecord::Base
374
417
  acts_as_paranoid
375
- validates :name, :presence => true
376
- has_many :paranoid_products, :dependent => :destroy
418
+ validates :name, presence: true
419
+ has_many :paranoid_products, dependent: :destroy
377
420
  end
378
421
 
379
422
  class ParanoidDeleteCompany < ActiveRecord::Base
380
423
  acts_as_paranoid
381
- validates :name, :presence => true
382
- has_many :paranoid_products, :dependent => :delete_all
424
+ validates :name, presence: true
425
+ has_many :paranoid_products, dependent: :delete_all
383
426
  end
384
427
 
385
428
  class ParanoidProduct < ActiveRecord::Base
@@ -395,22 +438,21 @@ class SuperParanoid < ActiveRecord::Base
395
438
  end
396
439
 
397
440
  class HasManyInheritedSuperParanoidz < ActiveRecord::Base
398
- has_many :super_paranoidz, :class_name => "InheritedParanoid", :dependent => :destroy
441
+ has_many :super_paranoidz, class_name: "InheritedParanoid", dependent: :destroy
399
442
  end
400
443
 
401
444
  class InheritedParanoid < SuperParanoid
402
445
  acts_as_paranoid
403
446
  end
404
447
 
405
-
406
448
  class ParanoidManyManyParentLeft < ActiveRecord::Base
407
449
  has_many :paranoid_many_many_children
408
- has_many :paranoid_many_many_parent_rights, :through => :paranoid_many_many_children
450
+ has_many :paranoid_many_many_parent_rights, through: :paranoid_many_many_children
409
451
  end
410
452
 
411
453
  class ParanoidManyManyParentRight < ActiveRecord::Base
412
454
  has_many :paranoid_many_many_children
413
- has_many :paranoid_many_many_parent_lefts, :through => :paranoid_many_many_children
455
+ has_many :paranoid_many_many_parent_lefts, through: :paranoid_many_many_children
414
456
  end
415
457
 
416
458
  class ParanoidManyManyChild < ActiveRecord::Base
@@ -421,21 +463,21 @@ end
421
463
 
422
464
  class ParanoidWithScopedValidation < ActiveRecord::Base
423
465
  acts_as_paranoid
424
- validates_uniqueness_of :name, :scope => :category
466
+ validates_uniqueness_of :name, scope: :category
425
467
  end
426
468
 
427
469
  class ParanoidBelongsToPolymorphic < ActiveRecord::Base
428
470
  acts_as_paranoid
429
- belongs_to :parent, :polymorphic => true, :with_deleted => true
471
+ belongs_to :parent, polymorphic: true, with_deleted: true
430
472
  end
431
473
 
432
474
  class NotParanoidHasManyAsParent < ActiveRecord::Base
433
- has_many :paranoid_belongs_to_polymorphics, :as => :parent, :dependent => :destroy
475
+ has_many :paranoid_belongs_to_polymorphics, as: :parent, dependent: :destroy
434
476
  end
435
477
 
436
478
  class ParanoidHasManyAsParent < ActiveRecord::Base
437
479
  acts_as_paranoid
438
- has_many :paranoid_belongs_to_polymorphics, :as => :parent, :dependent => :destroy
480
+ has_many :paranoid_belongs_to_polymorphics, as: :parent, dependent: :destroy
439
481
  end
440
482
 
441
483
  class ParanoidBaseTest < ActiveSupport::TestCase
@@ -443,13 +485,13 @@ class ParanoidBaseTest < ActiveSupport::TestCase
443
485
  setup_db
444
486
 
445
487
  ["paranoid", "really paranoid", "extremely paranoid"].each do |name|
446
- ParanoidTime.create! :name => name
447
- ParanoidBoolean.create! :name => name
488
+ ParanoidTime.create! name: name
489
+ ParanoidBoolean.create! name: name
448
490
  end
449
491
 
450
- ParanoidString.create! :name => "strings can be paranoid"
451
- NotParanoid.create! :name => "no paranoid goals"
452
- ParanoidWithCallback.create! :name => "paranoid with callbacks"
492
+ ParanoidString.create! name: "strings can be paranoid"
493
+ NotParanoid.create! name: "no paranoid goals"
494
+ ParanoidWithCallback.create! name: "paranoid with callbacks"
453
495
  end
454
496
 
455
497
  def teardown
@@ -478,25 +520,9 @@ class ParanoidBaseTest < ActiveSupport::TestCase
478
520
  end
479
521
  end
480
522
 
481
- class ParanoidForest < ActiveRecord::Base
482
- acts_as_paranoid
483
-
484
- ActiveRecord::Base.logger = Logger.new(StringIO.new)
485
-
486
- scope :rainforest, lambda{ where(:rainforest => true) }
487
-
488
- has_many :paranoid_trees, :dependent => :destroy
489
- end
490
-
491
- class ParanoidTree < ActiveRecord::Base
492
- acts_as_paranoid
493
- belongs_to :paranoid_forest
494
- validates_presence_of :name
495
- end
496
-
497
523
  class ParanoidPolygon < ActiveRecord::Base
498
524
  acts_as_paranoid
499
- default_scope { where('sides = ?', 3) }
525
+ default_scope { where("sides = ?", 3) }
500
526
  end
501
527
 
502
528
  class ParanoidAndroid < ActiveRecord::Base
@@ -506,14 +532,23 @@ end
506
532
  class ParanoidSection < ActiveRecord::Base
507
533
  acts_as_paranoid
508
534
  belongs_to :paranoid_time
509
- belongs_to :paranoid_thing, :polymorphic => true, :dependent => :destroy
535
+ belongs_to :paranoid_thing, polymorphic: true, dependent: :destroy
510
536
  end
511
537
 
512
538
  class ParanoidBooleanNotNullable < ActiveRecord::Base
513
- acts_as_paranoid column: 'deleted', column_type: 'boolean', allow_nulls: false
539
+ acts_as_paranoid column: "deleted", column_type: "boolean", allow_nulls: false
514
540
  end
515
541
 
516
542
  class ParanoidWithExplicitTableNameAfterMacro < ActiveRecord::Base
517
543
  acts_as_paranoid
518
544
  self.table_name = "explicit_table"
519
545
  end
546
+
547
+ class ParanoidWithSerializedColumn < ActiveRecord::Base
548
+ acts_as_paranoid
549
+ validates_as_paranoid
550
+
551
+ serialize :colors, Array
552
+
553
+ validates_uniqueness_of_without_deleted :colors
554
+ end