acts_as_paranoid 0.6.2 → 0.6.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +15 -0
- data/README.md +32 -0
- data/lib/acts_as_paranoid/core.rb +29 -6
- data/lib/acts_as_paranoid/version.rb +1 -1
- data/test/test_core.rb +71 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5e2b91eb0df1ed0d8cfb306f7272860c3f03e1da75259b202d222c1fece04927
|
4
|
+
data.tar.gz: 6dc0038fd41642f40e774e32eebe18ee5da6eae6750077781902aed17975ad1e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1587bb00c56acbb5daf5929ef4588f89622eb83141258c2f686ce5ed55719849fa17064bc011cf6cd3720d05f7900f1121442ba6abb71cdcc977f637800b00dc
|
7
|
+
data.tar.gz: 026be4a7b7eec225d6a1113af33aa610438bc4f895d8539e1bf6159f8bfe07bda3add7733ecdec982ac85a4b28f1e3abc9a4cd85ab56f07bc6f9fcb4bd20211b
|
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,15 @@
|
|
2
2
|
|
3
3
|
Notable changes to this project will be documented in this file.
|
4
4
|
|
5
|
+
## 0.6.3
|
6
|
+
|
7
|
+
* Update Travis CI configuration ([#137], by [Matijs van Zuijlen][mvz]).
|
8
|
+
* Add predicate to check if record was soft deleted or hard deleted ([#136],
|
9
|
+
by [Aymeric Le Dorze][aymeric-ledorze]).
|
10
|
+
* Add support for recover! method ([#75], by [vinoth][avinoth]).
|
11
|
+
* Fix a record being dirty after destroying it ([#135], by
|
12
|
+
[Aymeric Le Dorze][aymeric-ledorze]).
|
13
|
+
|
5
14
|
## 0.6.2
|
6
15
|
|
7
16
|
* Prevent recovery of non-deleted records
|
@@ -37,9 +46,14 @@ Notable changes to this project will be documented in this file.
|
|
37
46
|
[AlexWheeler]: https://github.com/AlexWheeler
|
38
47
|
[marycodes2]: https://github.com/marycodes2
|
39
48
|
[valeriecodes]: https://github.com/valeriecodes
|
49
|
+
[aymeric-ledorze]: https://github.com/aymeric-ledorze
|
50
|
+
[avinoth]: https://github.com/avinoth
|
40
51
|
|
41
52
|
<!-- issues & pull requests -->
|
42
53
|
|
54
|
+
[#137]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/137
|
55
|
+
[#136]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/136
|
56
|
+
[#135]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/135
|
43
57
|
[#133]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/133
|
44
58
|
[#131]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/131
|
45
59
|
[#124]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/124
|
@@ -47,3 +61,4 @@ Notable changes to this project will be documented in this file.
|
|
47
61
|
[#119]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/119
|
48
62
|
[#116]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/116
|
49
63
|
[#114]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/114
|
64
|
+
[#75]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/75
|
data/README.md
CHANGED
@@ -171,6 +171,22 @@ or in the recover statement
|
|
171
171
|
Paranoiac.only_deleted.where("name = ?", "not dead yet").first.recover(:recovery_window => 30.seconds)
|
172
172
|
```
|
173
173
|
|
174
|
+
### recover!
|
175
|
+
|
176
|
+
You can invoke `recover!` if you wish to raise an error if the recovery fails. The error generally stems from ActiveRecord.
|
177
|
+
|
178
|
+
```
|
179
|
+
Paranoiac.only_deleted.where("name = ?", "not dead yet").first.recover!
|
180
|
+
|
181
|
+
### => ActiveRecord::RecordInvalid: Validation failed: Name already exists
|
182
|
+
```
|
183
|
+
|
184
|
+
Optionally, you may also raise the error by passing `:raise_error => true` to the `recover` method. This behaves the same as `recover!`.
|
185
|
+
|
186
|
+
```
|
187
|
+
Paranoiac.only_deleted.where("name = ?", "not dead yet").first.recover(:raise_error => true)
|
188
|
+
```
|
189
|
+
|
174
190
|
### Validation
|
175
191
|
ActiveRecord's built-in uniqueness validation does not account for records deleted by ActsAsParanoid. If you want to check for uniqueness among non-deleted records only, use the macro `validates_as_paranoid` in your model. Then, instead of using `validates_uniqueness_of`, use `validates_uniqueness_of_without_deleted`. This will keep deleted records from counting against the uniqueness check.
|
176
192
|
|
@@ -263,6 +279,22 @@ child.parent #=> nil
|
|
263
279
|
child.parent_including_deleted #=> Parent (it works!)
|
264
280
|
```
|
265
281
|
|
282
|
+
### Callbacks
|
283
|
+
|
284
|
+
There are couple of callbacks that you may use when dealing with deletion and recovery of objects. There is `before_recover` and `after_recover` which will be
|
285
|
+
triggered before and after the recovery of an object respectively.
|
286
|
+
|
287
|
+
Default ActiveRecord callbaks such as `before_destroy` and `after_destroy` will be triggered around `.destroy!` and `.destroy_fully!`.
|
288
|
+
|
289
|
+
```
|
290
|
+
class Paranoiac < ActiveRecord::Base
|
291
|
+
acts_as_paranoid
|
292
|
+
|
293
|
+
before_recover :set_counts
|
294
|
+
after_recover :update_logs
|
295
|
+
end
|
296
|
+
```
|
297
|
+
|
266
298
|
## Caveats
|
267
299
|
|
268
300
|
Watch out for these caveats:
|
@@ -108,7 +108,7 @@ module ActsAsParanoid
|
|
108
108
|
# Straight from ActiveRecord 5.1!
|
109
109
|
def delete
|
110
110
|
self.class.delete(id) if persisted?
|
111
|
-
|
111
|
+
stale_paranoid_value
|
112
112
|
freeze
|
113
113
|
end
|
114
114
|
|
@@ -124,7 +124,8 @@ module ActsAsParanoid
|
|
124
124
|
decrement_counters_on_associations
|
125
125
|
end
|
126
126
|
|
127
|
-
|
127
|
+
stale_paranoid_value
|
128
|
+
@destroyed = true
|
128
129
|
freeze
|
129
130
|
end
|
130
131
|
end
|
@@ -144,7 +145,7 @@ module ActsAsParanoid
|
|
144
145
|
|
145
146
|
@_trigger_destroy_callback = true
|
146
147
|
|
147
|
-
|
148
|
+
stale_paranoid_value
|
148
149
|
self
|
149
150
|
end
|
150
151
|
end
|
@@ -161,7 +162,8 @@ module ActsAsParanoid
|
|
161
162
|
return if !self.deleted?
|
162
163
|
options = {
|
163
164
|
:recursive => self.class.paranoid_configuration[:recover_dependent_associations],
|
164
|
-
:recovery_window => self.class.paranoid_configuration[:dependent_recovery_window]
|
165
|
+
:recovery_window => self.class.paranoid_configuration[:dependent_recovery_window],
|
166
|
+
:raise_error => false
|
165
167
|
}.merge(options)
|
166
168
|
|
167
169
|
self.class.transaction do
|
@@ -169,11 +171,21 @@ module ActsAsParanoid
|
|
169
171
|
recover_dependent_associations(options[:recovery_window], options) if options[:recursive]
|
170
172
|
increment_counters_on_associations
|
171
173
|
self.paranoid_value = self.class.paranoid_configuration[:recovery_value]
|
172
|
-
|
174
|
+
if options[:raise_error]
|
175
|
+
self.save!
|
176
|
+
else
|
177
|
+
self.save
|
178
|
+
end
|
173
179
|
end
|
174
180
|
end
|
175
181
|
end
|
176
182
|
|
183
|
+
def recover!(options={})
|
184
|
+
options[:raise_error] = true
|
185
|
+
|
186
|
+
recover(options)
|
187
|
+
end
|
188
|
+
|
177
189
|
def recover_dependent_associations(window, options)
|
178
190
|
self.class.dependent_associations.each do |reflection|
|
179
191
|
next unless (klass = get_reflection_class(reflection)).paranoid?
|
@@ -219,7 +231,7 @@ module ActsAsParanoid
|
|
219
231
|
end
|
220
232
|
|
221
233
|
def deleted?
|
222
|
-
!if self.class.string_type_with_deleted_value?
|
234
|
+
@destroyed || !if self.class.string_type_with_deleted_value?
|
223
235
|
paranoid_value != self.class.delete_now_value || paranoid_value.nil?
|
224
236
|
elsif self.class.boolean_type_not_nullable?
|
225
237
|
paranoid_value == false
|
@@ -230,6 +242,12 @@ module ActsAsParanoid
|
|
230
242
|
|
231
243
|
alias_method :destroyed?, :deleted?
|
232
244
|
|
245
|
+
def deleted_fully?
|
246
|
+
@destroyed
|
247
|
+
end
|
248
|
+
|
249
|
+
alias_method :destroyed_fully?, :deleted_fully?
|
250
|
+
|
233
251
|
private
|
234
252
|
|
235
253
|
def get_reflection_class(reflection)
|
@@ -269,5 +287,10 @@ module ActsAsParanoid
|
|
269
287
|
def decrement_counters_on_associations
|
270
288
|
update_counters_on_associations :decrement_counter
|
271
289
|
end
|
290
|
+
|
291
|
+
def stale_paranoid_value
|
292
|
+
self.paranoid_value = self.class.delete_now_value
|
293
|
+
clear_attribute_changes([self.class.paranoid_column])
|
294
|
+
end
|
272
295
|
end
|
273
296
|
end
|
data/test/test_core.rb
CHANGED
@@ -69,6 +69,13 @@ class ParanoidTest < ParanoidBaseTest
|
|
69
69
|
assert_not_nil pt.paranoid_value
|
70
70
|
end
|
71
71
|
|
72
|
+
def test_non_persisted_delete
|
73
|
+
pt = ParanoidTime.new
|
74
|
+
assert_nil pt.paranoid_value
|
75
|
+
pt.delete
|
76
|
+
assert_not_nil pt.paranoid_value
|
77
|
+
end
|
78
|
+
|
72
79
|
def test_non_persisted_destroy!
|
73
80
|
pt = ParanoidTime.new
|
74
81
|
assert_nil pt.paranoid_value
|
@@ -94,6 +101,15 @@ class ParanoidTest < ParanoidBaseTest
|
|
94
101
|
assert_equal 1, ParanoidString.count
|
95
102
|
end
|
96
103
|
|
104
|
+
def test_recovery!
|
105
|
+
ParanoidBoolean.first.destroy
|
106
|
+
ParanoidBoolean.create(:name => 'paranoid')
|
107
|
+
|
108
|
+
assert_raise do
|
109
|
+
ParanoidBoolean.only_deleted.first.recover!
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
97
113
|
def setup_recursive_tests
|
98
114
|
@paranoid_time_object = ParanoidTime.first
|
99
115
|
|
@@ -265,6 +281,24 @@ class ParanoidTest < ParanoidBaseTest
|
|
265
281
|
assert_equal 0, HasOneNotParanoid.count
|
266
282
|
end
|
267
283
|
|
284
|
+
def test_dirty
|
285
|
+
pt = ParanoidTime.create
|
286
|
+
pt.destroy
|
287
|
+
assert_not pt.changed?
|
288
|
+
end
|
289
|
+
|
290
|
+
def test_delete_dirty
|
291
|
+
pt = ParanoidTime.create
|
292
|
+
pt.delete
|
293
|
+
assert_not pt.changed?
|
294
|
+
end
|
295
|
+
|
296
|
+
def test_destroy_fully_dirty
|
297
|
+
pt = ParanoidTime.create
|
298
|
+
pt.destroy_fully!
|
299
|
+
assert_not pt.changed?
|
300
|
+
end
|
301
|
+
|
268
302
|
def test_deleted?
|
269
303
|
ParanoidTime.first.destroy
|
270
304
|
assert ParanoidTime.with_deleted.first.deleted?
|
@@ -273,6 +307,43 @@ class ParanoidTest < ParanoidBaseTest
|
|
273
307
|
assert ParanoidString.with_deleted.first.deleted?
|
274
308
|
end
|
275
309
|
|
310
|
+
def test_delete_deleted?
|
311
|
+
ParanoidTime.first.delete
|
312
|
+
assert ParanoidTime.with_deleted.first.deleted?
|
313
|
+
|
314
|
+
ParanoidString.first.delete
|
315
|
+
assert ParanoidString.with_deleted.first.deleted?
|
316
|
+
end
|
317
|
+
|
318
|
+
def test_destroy_fully_deleted?
|
319
|
+
object = ParanoidTime.first
|
320
|
+
object.destroy_fully!
|
321
|
+
assert object.deleted?
|
322
|
+
|
323
|
+
object = ParanoidString.first
|
324
|
+
object.destroy_fully!
|
325
|
+
assert object.deleted?
|
326
|
+
end
|
327
|
+
|
328
|
+
def test_deleted_fully?
|
329
|
+
ParanoidTime.first.destroy
|
330
|
+
assert_not ParanoidTime.with_deleted.first.deleted_fully?
|
331
|
+
|
332
|
+
ParanoidString.first.destroy
|
333
|
+
assert ParanoidString.with_deleted.first.deleted?
|
334
|
+
end
|
335
|
+
|
336
|
+
def test_delete_deleted_fully?
|
337
|
+
ParanoidTime.first.delete
|
338
|
+
assert_not ParanoidTime.with_deleted.first.deleted_fully?
|
339
|
+
end
|
340
|
+
|
341
|
+
def test_destroy_fully_deleted_fully?
|
342
|
+
object = ParanoidTime.first
|
343
|
+
object.destroy_fully!
|
344
|
+
assert object.deleted_fully?
|
345
|
+
end
|
346
|
+
|
276
347
|
def test_paranoid_destroy_callbacks
|
277
348
|
@paranoid_with_callback = ParanoidWithCallback.first
|
278
349
|
ParanoidWithCallback.transaction do
|
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.3
|
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:
|
13
|
+
date: 2020-01-25 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activerecord
|
@@ -178,7 +178,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
178
178
|
- !ruby/object:Gem::Version
|
179
179
|
version: '0'
|
180
180
|
requirements: []
|
181
|
-
rubygems_version: 3.
|
181
|
+
rubygems_version: 3.1.2
|
182
182
|
signing_key:
|
183
183
|
specification_version: 4
|
184
184
|
summary: Active Record plugin which allows you to hide and restore records without
|