acts_as_paranoid 0.6.1 → 0.6.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +19 -2
- data/lib/acts_as_paranoid.rb +4 -2
- data/lib/acts_as_paranoid/core.rb +6 -4
- data/lib/acts_as_paranoid/version.rb +1 -1
- data/test/test_core.rb +55 -0
- data/test/test_helper.rb +24 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6bcd9a9f5f44fbc3890442360530b48ed39f94bd8cbe4f49d245a2c27510f7fd
|
4
|
+
data.tar.gz: 47c9aceb7415a3704b69b3bcaf7c8db64d402ecc9d1ef41352a70d0a386a819c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 78c376b394c50800f5f22db6a30a4dd607196a277266af7e24e04b209326e176471f916da360a1eb6e7e2c6573c506b211eccac42961a9a3ed686def7c105987
|
7
|
+
data.tar.gz: b0025f38c28cfc80fb852673f4913edf25273912ca83a768e000a15a745edfa9069d6fda490e31cc8ffe39975651d339d7ce82ac07f1065936ef6832f6b5eef1
|
data/CHANGELOG.md
CHANGED
@@ -2,10 +2,19 @@
|
|
2
2
|
|
3
3
|
Notable changes to this project will be documented in this file.
|
4
4
|
|
5
|
+
## 0.6.2
|
6
|
+
|
7
|
+
* Prevent recovery of non-deleted records
|
8
|
+
([#133], by [Mary Beliveau][marycodes2] and [Valerie Woolard][valeriecodes])
|
9
|
+
* Allow model to set `table_name` after `acts_as_paranoid` macro
|
10
|
+
([#131], by [Alex Wheeler][AlexWheeler]).
|
11
|
+
* Make counter cache work with a custom column name and with optional
|
12
|
+
associations ([#123], by [Ned Campion][nedcampion]).
|
13
|
+
|
5
14
|
## 0.6.1
|
6
15
|
|
7
|
-
* Add support for Rails 6 ([#124], by [Daniel Rice]
|
8
|
-
[Josh Bryant]
|
16
|
+
* Add support for Rails 6 ([#124], by [Daniel Rice][danielricecodes],
|
17
|
+
[Josh Bryant][jbryant92], and [Romain Alexandre][RomainAlexandre])
|
9
18
|
* Add support for incrementing and decrementing counter cache columns on
|
10
19
|
associated objects ([#119], by [Dimitar Lukanov][shadydealer])
|
11
20
|
* Add `:double_tap_destroys_fully` option, with default `true` ([#116],
|
@@ -23,10 +32,18 @@ Notable changes to this project will be documented in this file.
|
|
23
32
|
[shadydealer]: https://github.com/shadydealer
|
24
33
|
[danielricecodes]: https://github.com/danielricecodes
|
25
34
|
[jbryant92]: https://github.com/jbryant92
|
35
|
+
[nedcampion]: https://github.com/nedcampion
|
26
36
|
[RomainAlexandre]: https://github.com/RomainAlexandre
|
37
|
+
[AlexWheeler]: https://github.com/AlexWheeler
|
38
|
+
[marycodes2]: https://github.com/marycodes2
|
39
|
+
[valeriecodes]: https://github.com/valeriecodes
|
27
40
|
|
28
41
|
<!-- issues & pull requests -->
|
29
42
|
|
43
|
+
[#133]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/133
|
44
|
+
[#131]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/131
|
45
|
+
[#124]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/124
|
46
|
+
[#123]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/123
|
30
47
|
[#119]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/119
|
31
48
|
[#116]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/116
|
32
49
|
[#114]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/114
|
data/lib/acts_as_paranoid.rb
CHANGED
@@ -17,7 +17,7 @@ module ActsAsParanoid
|
|
17
17
|
def acts_as_paranoid(options = {})
|
18
18
|
raise ArgumentError, "Hash expected, got #{options.class.name}" if not options.is_a?(Hash) and not options.empty?
|
19
19
|
|
20
|
-
class_attribute :paranoid_configuration
|
20
|
+
class_attribute :paranoid_configuration
|
21
21
|
|
22
22
|
self.paranoid_configuration = { :column => "deleted_at", :column_type => "time", :recover_dependent_associations => true, :dependent_recovery_window => 2.minutes, :recovery_value => nil, double_tap_destroys_fully: true }
|
23
23
|
self.paranoid_configuration.merge!({ :deleted_value => "deleted" }) if options[:column_type] == "string"
|
@@ -26,7 +26,9 @@ module ActsAsParanoid
|
|
26
26
|
|
27
27
|
raise ArgumentError, "'time', 'boolean' or 'string' expected for :column_type option, got #{paranoid_configuration[:column_type]}" unless ['time', 'boolean', 'string'].include? paranoid_configuration[:column_type]
|
28
28
|
|
29
|
-
self.paranoid_column_reference
|
29
|
+
def self.paranoid_column_reference
|
30
|
+
"#{self.table_name}.#{paranoid_configuration[:column]}"
|
31
|
+
end
|
30
32
|
|
31
33
|
return if paranoid?
|
32
34
|
|
@@ -158,6 +158,7 @@ module ActsAsParanoid
|
|
158
158
|
alias_method :destroy, :destroy!
|
159
159
|
|
160
160
|
def recover(options={})
|
161
|
+
return if !self.deleted?
|
161
162
|
options = {
|
162
163
|
:recursive => self.class.paranoid_configuration[:recover_dependent_associations],
|
163
164
|
:recovery_window => self.class.paranoid_configuration[:dependent_recovery_window]
|
@@ -248,15 +249,16 @@ module ActsAsParanoid
|
|
248
249
|
return unless [:decrement_counter, :increment_counter].include? method_sym
|
249
250
|
|
250
251
|
each_counter_cached_association_reflection do |assoc_reflection|
|
251
|
-
associated_object = send(assoc_reflection.name)
|
252
|
-
|
253
|
-
|
252
|
+
if associated_object = send(assoc_reflection.name)
|
253
|
+
counter_cache_column = assoc_reflection.counter_cache_column
|
254
|
+
associated_object.class.send(method_sym, counter_cache_column, associated_object.id)
|
255
|
+
end
|
254
256
|
end
|
255
257
|
end
|
256
258
|
|
257
259
|
def each_counter_cached_association_reflection
|
258
260
|
_reflections.each do |name, reflection|
|
259
|
-
yield reflection if reflection.belongs_to? && reflection.counter_cache_column
|
261
|
+
yield reflection if reflection.belongs_to? && reflection.counter_cache_column
|
260
262
|
end
|
261
263
|
end
|
262
264
|
|
data/test/test_core.rb
CHANGED
@@ -312,6 +312,14 @@ class ParanoidTest < ParanoidBaseTest
|
|
312
312
|
assert @paranoid_with_callback.called_after_recover
|
313
313
|
end
|
314
314
|
|
315
|
+
def test_recovery_callbacks_without_destroy
|
316
|
+
@paranoid_with_callback = ParanoidWithCallback.first
|
317
|
+
@paranoid_with_callback.recover
|
318
|
+
|
319
|
+
assert_nil @paranoid_with_callback.called_before_recover
|
320
|
+
assert_nil @paranoid_with_callback.called_after_recover
|
321
|
+
end
|
322
|
+
|
315
323
|
def test_delete_by_multiple_id_is_paranoid
|
316
324
|
model_a = ParanoidBelongsDependant.create
|
317
325
|
model_b = ParanoidBelongsDependant.create
|
@@ -455,6 +463,23 @@ class ParanoidTest < ParanoidBaseTest
|
|
455
463
|
assert_equal 0, paranoid_boolean.reload.paranoid_with_counter_caches_count
|
456
464
|
end
|
457
465
|
|
466
|
+
def test_decrement_custom_counters
|
467
|
+
paranoid_boolean = ParanoidBoolean.create!()
|
468
|
+
paranoid_with_custom_counter_cache = ParanoidWithCustomCounterCache.create!(paranoid_boolean: paranoid_boolean)
|
469
|
+
|
470
|
+
assert_equal 1, paranoid_boolean.custom_counter_cache
|
471
|
+
|
472
|
+
paranoid_with_custom_counter_cache.destroy
|
473
|
+
|
474
|
+
assert_equal 0, paranoid_boolean.reload.custom_counter_cache
|
475
|
+
end
|
476
|
+
|
477
|
+
def test_destroy_with_optional_belongs_to_and_counter_cache
|
478
|
+
ps = ParanoidWithCounterCacheOnOptionalBelognsTo.create!()
|
479
|
+
ps.destroy
|
480
|
+
assert_equal 1, ParanoidWithCounterCacheOnOptionalBelognsTo.only_deleted.where(:id => ps).count
|
481
|
+
end
|
482
|
+
|
458
483
|
def test_hard_destroy_decrement_counters
|
459
484
|
paranoid_boolean = ParanoidBoolean.create!()
|
460
485
|
paranoid_with_counter_cache = ParanoidWithCounterCache.create!(paranoid_boolean: paranoid_boolean)
|
@@ -466,6 +491,17 @@ class ParanoidTest < ParanoidBaseTest
|
|
466
491
|
assert_equal 0, paranoid_boolean.reload.paranoid_with_counter_caches_count
|
467
492
|
end
|
468
493
|
|
494
|
+
def test_hard_destroy_decrement_custom_counters
|
495
|
+
paranoid_boolean = ParanoidBoolean.create!()
|
496
|
+
paranoid_with_custom_counter_cache = ParanoidWithCustomCounterCache.create!(paranoid_boolean: paranoid_boolean)
|
497
|
+
|
498
|
+
assert_equal 1, paranoid_boolean.custom_counter_cache
|
499
|
+
|
500
|
+
paranoid_with_custom_counter_cache.destroy_fully!
|
501
|
+
|
502
|
+
assert_equal 0, paranoid_boolean.reload.custom_counter_cache
|
503
|
+
end
|
504
|
+
|
469
505
|
def test_increment_counters
|
470
506
|
paranoid_boolean = ParanoidBoolean.create!()
|
471
507
|
paranoid_with_counter_cache = ParanoidWithCounterCache.create!(paranoid_boolean: paranoid_boolean)
|
@@ -480,4 +516,23 @@ class ParanoidTest < ParanoidBaseTest
|
|
480
516
|
|
481
517
|
assert_equal 1, paranoid_boolean.reload.paranoid_with_counter_caches_count
|
482
518
|
end
|
519
|
+
|
520
|
+
def test_increment_custom_counters
|
521
|
+
paranoid_boolean = ParanoidBoolean.create!()
|
522
|
+
paranoid_with_custom_counter_cache = ParanoidWithCustomCounterCache.create!(paranoid_boolean: paranoid_boolean)
|
523
|
+
|
524
|
+
assert_equal 1, paranoid_boolean.custom_counter_cache
|
525
|
+
|
526
|
+
paranoid_with_custom_counter_cache.destroy
|
527
|
+
|
528
|
+
assert_equal 0, paranoid_boolean.reload.custom_counter_cache
|
529
|
+
|
530
|
+
paranoid_with_custom_counter_cache.recover
|
531
|
+
|
532
|
+
assert_equal 1, paranoid_boolean.reload.custom_counter_cache
|
533
|
+
end
|
534
|
+
|
535
|
+
def test_explicitly_setting_table_name_after_acts_as_paranoid_macro
|
536
|
+
assert_equal "explicit_table.deleted_at", ParanoidWithExplicitTableNameAfterMacro.paranoid_column_reference
|
537
|
+
end
|
483
538
|
end
|
data/test/test_helper.rb
CHANGED
@@ -32,6 +32,7 @@ def setup_db
|
|
32
32
|
t.boolean :is_deleted
|
33
33
|
t.integer :paranoid_time_id
|
34
34
|
t.integer :paranoid_with_counter_caches_count
|
35
|
+
t.integer :custom_counter_cache
|
35
36
|
timestamps t
|
36
37
|
end
|
37
38
|
|
@@ -260,6 +261,7 @@ class ParanoidBoolean < ActiveRecord::Base
|
|
260
261
|
belongs_to :paranoid_time
|
261
262
|
has_one :paranoid_has_one_dependant, :dependent => :destroy
|
262
263
|
has_many :paranoid_with_counter_cache, :dependent => :destroy
|
264
|
+
has_many :paranoid_with_custom_counter_cache, :dependent => :destroy
|
263
265
|
end
|
264
266
|
|
265
267
|
class ParanoidString < ActiveRecord::Base
|
@@ -287,6 +289,24 @@ class ParanoidWithCounterCache < ActiveRecord::Base
|
|
287
289
|
belongs_to :paranoid_boolean, :counter_cache => true
|
288
290
|
end
|
289
291
|
|
292
|
+
class ParanoidWithCustomCounterCache < ActiveRecord::Base
|
293
|
+
self.table_name = "paranoid_with_counter_caches"
|
294
|
+
|
295
|
+
acts_as_paranoid
|
296
|
+
belongs_to :paranoid_boolean, counter_cache: :custom_counter_cache
|
297
|
+
end
|
298
|
+
|
299
|
+
class ParanoidWithCounterCacheOnOptionalBelognsTo < ActiveRecord::Base
|
300
|
+
self.table_name = "paranoid_with_counter_caches"
|
301
|
+
|
302
|
+
acts_as_paranoid
|
303
|
+
if ActiveRecord::VERSION::MAJOR < 5
|
304
|
+
belongs_to :paranoid_boolean, counter_cache: true, required: false
|
305
|
+
else
|
306
|
+
belongs_to :paranoid_boolean, counter_cache: true, optional: true
|
307
|
+
end
|
308
|
+
end
|
309
|
+
|
290
310
|
class ParanoidHasManyDependant < ActiveRecord::Base
|
291
311
|
acts_as_paranoid
|
292
312
|
belongs_to :paranoid_time
|
@@ -493,3 +513,7 @@ class ParanoidBooleanNotNullable < ActiveRecord::Base
|
|
493
513
|
acts_as_paranoid column: 'deleted', column_type: 'boolean', allow_nulls: false
|
494
514
|
end
|
495
515
|
|
516
|
+
class ParanoidWithExplicitTableNameAfterMacro < ActiveRecord::Base
|
517
|
+
acts_as_paranoid
|
518
|
+
self.table_name = "explicit_table"
|
519
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_paranoid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zachary Scott
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2019-
|
13
|
+
date: 2019-10-22 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activerecord
|