acts_as_paranoid 0.6.3 → 0.7.0
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 +69 -16
- data/README.md +130 -68
- data/lib/acts_as_paranoid.rb +31 -30
- data/lib/acts_as_paranoid/associations.rb +21 -17
- data/lib/acts_as_paranoid/core.rb +87 -77
- data/lib/acts_as_paranoid/relation.rb +2 -0
- data/lib/acts_as_paranoid/validations.rb +8 -74
- data/lib/acts_as_paranoid/version.rb +3 -1
- data/test/test_associations.rb +119 -43
- data/test/test_core.rb +90 -66
- data/test/test_default_scopes.rb +7 -5
- data/test/test_helper.rb +87 -65
- data/test/test_inheritance.rb +3 -1
- data/test/test_relations.rb +18 -10
- data/test/test_validations.rb +9 -7
- metadata +57 -26
- data/lib/acts_as_paranoid/preloader_association.rb +0 -16
- data/test/test_preloader_association.rb +0 -27
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a57d617e3e2f52608fefad1c84bba5d6825d1b9ea96e681ade8739461ef35796
|
4
|
+
data.tar.gz: 549ef0573150ba7f30740a6145cb6b52ef2916b3584199fc0bab8eba17271eb0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 13ac9918fc0dc45e93d02ad799f20fe9c13afd4eba4a0b81c52201bc495a690dc80b1ab9ba9e700b7d072a61c5973dcf8d134a1049c581d61c1c0120f4444d59
|
7
|
+
data.tar.gz: 6bba5d24a72d45b5de90670e1c577d6172160d1ee5d3850a3d9dd3845eb2fd73060a6d441d738b99dc9aea516189a6807c0406f377997dddf8ac443f88db6fbd
|
data/CHANGELOG.md
CHANGED
@@ -2,23 +2,54 @@
|
|
2
2
|
|
3
3
|
Notable changes to this project will be documented in this file.
|
4
4
|
|
5
|
+
## 0.7.0
|
6
|
+
|
7
|
+
### Breaking changes
|
8
|
+
|
9
|
+
* Support Rails 5.2+ only ([#126], by [Daniel Rice][danielricecodes])
|
10
|
+
* Update set of supported rubies to 2.4-2.7 ([#144], [#173] by [Matijs van Zuijlen][mvz])
|
11
|
+
|
12
|
+
### Improvements
|
13
|
+
|
14
|
+
* Handle `with_deleted` association option as a scope ([#147], by [Matijs van Zuijlen][mvz])
|
15
|
+
* Simplify validation override ([#158], by [Matijs van Zuijlen][mvz])
|
16
|
+
* Use correct unscope syntax so unscope works on Rails Edge ([#160],
|
17
|
+
by [Matijs van Zuijlen][mvz])
|
18
|
+
* Fix ruby 2.7 keyword argument deprecation warning ([#161], by [Jon Riddle][wtfspm])
|
19
|
+
|
20
|
+
### Documentation
|
21
|
+
|
22
|
+
* Document save after destroy behavior ([#146], by [Matijs van Zuijlen][mvz])
|
23
|
+
* Update version number instructions for installing gem ([#164],
|
24
|
+
by [Kevin McAlear][kevinmcalear])
|
25
|
+
* Add example with `destroyed_fully?` and `deleted_fully?` to the readme ([#170],
|
26
|
+
by [Kiril Mitov][thebravoman])
|
27
|
+
|
28
|
+
### Internal
|
29
|
+
|
30
|
+
* Improve code quality using RuboCop ([#148], [#152], [#159], [#163], [#171] and [#173],
|
31
|
+
by [Matijs van Zuijlen][mvz])
|
32
|
+
* Measure code coverage using SimpleCov ([#150] and [#175] by [Matijs van Zuijlen][mvz])
|
33
|
+
* Silence warnings emitted during tests ([#156], by [Matijs van Zuijlen][mvz])
|
34
|
+
* Make rake tasks more robust and intuitive ([#157], by [Matijs van Zuijlen][mvz])
|
35
|
+
|
5
36
|
## 0.6.3
|
6
37
|
|
7
|
-
* Update Travis CI configuration ([#137], by [Matijs van Zuijlen][mvz])
|
38
|
+
* Update Travis CI configuration ([#137], by [Matijs van Zuijlen][mvz])
|
8
39
|
* 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])
|
40
|
+
by [Aymeric Le Dorze][aymeric-ledorze])
|
41
|
+
* Add support for recover! method ([#75], by [vinoth][avinoth])
|
11
42
|
* Fix a record being dirty after destroying it ([#135], by
|
12
|
-
[Aymeric Le Dorze][aymeric-ledorze])
|
43
|
+
[Aymeric Le Dorze][aymeric-ledorze])
|
13
44
|
|
14
45
|
## 0.6.2
|
15
46
|
|
16
47
|
* Prevent recovery of non-deleted records
|
17
48
|
([#133], by [Mary Beliveau][marycodes2] and [Valerie Woolard][valeriecodes])
|
18
49
|
* Allow model to set `table_name` after `acts_as_paranoid` macro
|
19
|
-
([#131], by [Alex Wheeler][AlexWheeler])
|
50
|
+
([#131], by [Alex Wheeler][AlexWheeler])
|
20
51
|
* Make counter cache work with a custom column name and with optional
|
21
|
-
associations ([#123], by [Ned Campion][nedcampion])
|
52
|
+
associations ([#123], by [Ned Campion][nedcampion])
|
22
53
|
|
23
54
|
## 0.6.1
|
24
55
|
|
@@ -27,8 +58,8 @@ Notable changes to this project will be documented in this file.
|
|
27
58
|
* Add support for incrementing and decrementing counter cache columns on
|
28
59
|
associated objects ([#119], by [Dimitar Lukanov][shadydealer])
|
29
60
|
* Add `:double_tap_destroys_fully` option, with default `true` ([#116],
|
30
|
-
by [Michael Riviera][ri4a])
|
31
|
-
* Officially support Ruby 2.6 ([#114], by [Matijs van Zuijlen][mvz])
|
61
|
+
by [Michael Riviera][ri4a])
|
62
|
+
* Officially support Ruby 2.6 ([#114], by [Matijs van Zuijlen][mvz])
|
32
63
|
|
33
64
|
## 0.6.0 and earlier
|
34
65
|
|
@@ -36,26 +67,48 @@ Notable changes to this project will be documented in this file.
|
|
36
67
|
|
37
68
|
<!-- Contributors -->
|
38
69
|
|
39
|
-
[
|
40
|
-
[
|
41
|
-
[
|
70
|
+
[AlexWheeler]: https://github.com/AlexWheeler
|
71
|
+
[RomainAlexandre]: https://github.com/RomainAlexandre
|
72
|
+
[avinoth]: https://github.com/avinoth
|
73
|
+
[aymeric-ledorze]: https://github.com/aymeric-ledorze
|
42
74
|
[danielricecodes]: https://github.com/danielricecodes
|
43
75
|
[jbryant92]: https://github.com/jbryant92
|
44
|
-
[
|
45
|
-
[RomainAlexandre]: https://github.com/RomainAlexandre
|
46
|
-
[AlexWheeler]: https://github.com/AlexWheeler
|
76
|
+
[kevinmcalear]: https://github.com/kevinmcalear
|
47
77
|
[marycodes2]: https://github.com/marycodes2
|
78
|
+
[mvz]: https://github.com/mvz
|
79
|
+
[nedcampion]: https://github.com/nedcampion
|
80
|
+
[ri4a]: https://github.com/ri4a
|
81
|
+
[shadydealer]: https://github.com/shadydealer
|
82
|
+
[thebravoman]: https://github.com/thebravoman
|
48
83
|
[valeriecodes]: https://github.com/valeriecodes
|
49
|
-
[
|
50
|
-
[avinoth]: https://github.com/avinoth
|
84
|
+
[wtfspm]: https://github.com/wtfspm
|
51
85
|
|
52
86
|
<!-- issues & pull requests -->
|
53
87
|
|
88
|
+
[#175]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/175
|
89
|
+
[#173]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/173
|
90
|
+
[#171]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/171
|
91
|
+
[#170]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/170
|
92
|
+
[#164]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/164
|
93
|
+
[#163]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/163
|
94
|
+
[#161]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/161
|
95
|
+
[#160]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/160
|
96
|
+
[#159]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/159
|
97
|
+
[#158]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/158
|
98
|
+
[#157]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/157
|
99
|
+
[#156]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/156
|
100
|
+
[#152]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/152
|
101
|
+
[#150]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/150
|
102
|
+
[#148]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/148
|
103
|
+
[#147]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/147
|
104
|
+
[#146]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/146
|
105
|
+
[#144]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/144
|
54
106
|
[#137]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/137
|
55
107
|
[#136]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/136
|
56
108
|
[#135]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/135
|
57
109
|
[#133]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/133
|
58
110
|
[#131]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/131
|
111
|
+
[#126]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/126
|
59
112
|
[#124]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/124
|
60
113
|
[#123]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/123
|
61
114
|
[#119]: https://github.com/ActsAsParanoid/acts_as_paranoid/pull/119
|
data/README.md
CHANGED
@@ -9,35 +9,38 @@ recoverable later.
|
|
9
9
|
|
10
10
|
## Support
|
11
11
|
|
12
|
-
**This branch targets Rails
|
12
|
+
**This branch targets Rails 5.2+ and Ruby 2.4+ only**
|
13
13
|
|
14
|
-
If you're working with Rails
|
15
|
-
require an older version of the
|
14
|
+
If you're working with Rails 5.1 and earlier, or with Ruby 2.3 or earlier,
|
15
|
+
please switch to the corresponding branch or require an older version of the
|
16
|
+
`acts_as_paranoid` gem.
|
16
17
|
|
17
|
-
### Known issues
|
18
|
+
### Known issues
|
18
19
|
|
19
|
-
* Using acts_as_paranoid and ActiveStorage on the same model
|
20
|
+
* Using `acts_as_paranoid` and ActiveStorage on the same model
|
20
21
|
[leads to a SystemStackError](https://github.com/ActsAsParanoid/acts_as_paranoid/issues/103).
|
21
|
-
* You cannot directly create a model in a deleted state
|
22
|
+
* You cannot directly create a model in a deleted state, or update a model
|
23
|
+
after it's been deleted.
|
22
24
|
|
23
25
|
## Usage
|
24
26
|
|
25
27
|
#### Install gem:
|
26
28
|
|
27
|
-
```
|
28
|
-
gem 'acts_as_paranoid', '~> 0.
|
29
|
+
```ruby
|
30
|
+
gem 'acts_as_paranoid', '~> 0.7.0'
|
29
31
|
```
|
30
|
-
|
32
|
+
|
33
|
+
```shell
|
31
34
|
bundle install
|
32
35
|
```
|
33
36
|
|
34
37
|
#### Create migration
|
35
38
|
|
36
|
-
```
|
39
|
+
```shell
|
37
40
|
bin/rails generate migration AddDeletedAtToParanoiac deleted_at:datetime:index
|
38
41
|
```
|
39
42
|
|
40
|
-
#### Enable ActsAsParanoid
|
43
|
+
#### Enable ActsAsParanoid
|
41
44
|
|
42
45
|
```ruby
|
43
46
|
class Paranoiac < ActiveRecord::Base
|
@@ -45,31 +48,42 @@ class Paranoiac < ActiveRecord::Base
|
|
45
48
|
end
|
46
49
|
```
|
47
50
|
|
48
|
-
|
49
|
-
|
50
|
-
- `:column => 'deleted_at'`
|
51
|
-
- `:column_type => 'time'`
|
51
|
+
By default, ActsAsParanoid assumes a record's *deletion* is stored in a
|
52
|
+
`datetime` column called `deleted_at`.
|
52
53
|
|
53
54
|
### Options
|
54
55
|
|
55
|
-
If you are using a different column name and type to store a record's
|
56
|
+
If you are using a different column name and type to store a record's
|
57
|
+
*deletion*, you can specify them as follows:
|
56
58
|
|
57
|
-
-
|
58
|
-
-
|
59
|
+
- `column: 'deleted'`
|
60
|
+
- `column_type: 'boolean'`
|
59
61
|
|
60
|
-
While *column* can be anything (as long as it exists in your database), *type*
|
62
|
+
While *column* can be anything (as long as it exists in your database), *type*
|
63
|
+
is restricted to:
|
61
64
|
|
62
65
|
- `boolean`
|
63
66
|
- `time` or
|
64
67
|
- `string`
|
65
68
|
|
66
|
-
|
69
|
+
Note that the `time` type corresponds to the database column type `datetime`
|
70
|
+
in your Rails migrations and schema.
|
67
71
|
|
68
|
-
If your column type is a `
|
72
|
+
If your column type is a `string`, you can also specify which value to use when
|
73
|
+
marking an object as deleted by passing `:deleted_value` (default is
|
74
|
+
"deleted"). Any records with a non-matching value in this column will be
|
75
|
+
treated normally, i.e., as not deleted.
|
76
|
+
|
77
|
+
If your column type is a `boolean`, it is possible to specify `allow_nulls`
|
78
|
+
option which is `true` by default. When set to `false`, entities that have
|
79
|
+
`false` value in this column will be considered not deleted, and those which
|
80
|
+
have `true` will be considered deleted. When `true` everything that has a
|
81
|
+
not-null value will be considered deleted.
|
69
82
|
|
70
83
|
### Filtering
|
71
84
|
|
72
|
-
If a record is deleted by ActsAsParanoid, it won't be retrieved when accessing
|
85
|
+
If a record is deleted by ActsAsParanoid, it won't be retrieved when accessing
|
86
|
+
the database.
|
73
87
|
|
74
88
|
So, `Paranoiac.all` will **not** include the **deleted records**.
|
75
89
|
|
@@ -80,7 +94,8 @@ Paranoiac.only_deleted # retrieves only the deleted records
|
|
80
94
|
Paranoiac.with_deleted # retrieves all records, deleted or not
|
81
95
|
```
|
82
96
|
|
83
|
-
When using the default `column_type` of `'time'`, the following extra scopes
|
97
|
+
When using the default `column_type` of `'time'`, the following extra scopes
|
98
|
+
are provided:
|
84
99
|
|
85
100
|
```ruby
|
86
101
|
time = Time.now
|
@@ -100,23 +115,32 @@ In order to really delete a record, just use:
|
|
100
115
|
paranoiac.destroy_fully!
|
101
116
|
Paranoiac.delete_all!(conditions)
|
102
117
|
```
|
103
|
-
**NOTE:** The `.destroy!` method is still usable, but equivalent to `.destroy`. It just hides the object.
|
104
118
|
|
105
|
-
|
119
|
+
**NOTE:** The `.destroy!` method is still usable, but equivalent to `.destroy`.
|
120
|
+
It just hides the object.
|
121
|
+
|
122
|
+
Alternatively you can permanently delete a record by calling `destroy` or
|
123
|
+
`delete_all` on the object **twice**.
|
106
124
|
|
107
|
-
If a record was already deleted (hidden by `ActsAsParanoid`) and you delete it
|
125
|
+
If a record was already deleted (hidden by `ActsAsParanoid`) and you delete it
|
126
|
+
again, it will be removed from the database.
|
108
127
|
|
109
128
|
Take this example:
|
110
129
|
|
111
130
|
```ruby
|
112
131
|
p = Paranoiac.first
|
113
|
-
|
114
|
-
|
132
|
+
|
133
|
+
# does NOT delete the first record, just hides it
|
134
|
+
p.destroy
|
135
|
+
|
136
|
+
# deletes the first record from the database
|
137
|
+
Paranoiac.only_deleted.where(id: p.id).first.destroy
|
115
138
|
```
|
116
139
|
|
117
|
-
This behaviour can be disabled by setting the configuration option. In a future
|
140
|
+
This behaviour can be disabled by setting the configuration option. In a future
|
141
|
+
version, `false` will be the default setting.
|
118
142
|
|
119
|
-
-
|
143
|
+
- `double_tap_destroys_fully: false`
|
120
144
|
|
121
145
|
### Recovery
|
122
146
|
|
@@ -126,34 +150,39 @@ Recovery is easy. Just invoke `recover` on it, like this:
|
|
126
150
|
Paranoiac.only_deleted.where("name = ?", "not dead yet").first.recover
|
127
151
|
```
|
128
152
|
|
129
|
-
All associations marked as
|
153
|
+
All associations marked as `dependent: :destroy` are also recursively recovered.
|
130
154
|
|
131
|
-
If you would like to disable this behavior, you can call `recover` with the
|
155
|
+
If you would like to disable this behavior, you can call `recover` with the
|
156
|
+
`recursive` option:
|
132
157
|
|
133
158
|
```ruby
|
134
|
-
Paranoiac.only_deleted.where("name = ?", "not dead yet").first.recover(:
|
159
|
+
Paranoiac.only_deleted.where("name = ?", "not dead yet").first.recover(recursive: false)
|
135
160
|
```
|
136
161
|
|
137
|
-
If you would like to change this default behavior for one model, you can use
|
162
|
+
If you would like to change this default behavior for one model, you can use
|
163
|
+
the `recover_dependent_associations` option
|
138
164
|
|
139
165
|
```ruby
|
140
166
|
class Paranoiac < ActiveRecord::Base
|
141
|
-
acts_as_paranoid :
|
167
|
+
acts_as_paranoid recover_dependent_associations: false
|
142
168
|
end
|
143
169
|
```
|
144
170
|
|
145
|
-
By default, dependent records will be recovered if they were deleted within 2
|
171
|
+
By default, dependent records will be recovered if they were deleted within 2
|
172
|
+
minutes of the object upon which they depend.
|
146
173
|
|
147
|
-
This restores the objects to the state before the recursive deletion without
|
174
|
+
This restores the objects to the state before the recursive deletion without
|
175
|
+
restoring other objects that were deleted earlier.
|
148
176
|
|
149
|
-
The behavior is only available when both parent and dependant are using
|
177
|
+
The behavior is only available when both parent and dependant are using
|
178
|
+
timestamp fields to mark deletion, which is the default behavior.
|
150
179
|
|
151
180
|
This window can be changed with the `dependent_recovery_window` option:
|
152
181
|
|
153
182
|
```ruby
|
154
183
|
class Paranoiac < ActiveRecord::Base
|
155
184
|
acts_as_paranoid
|
156
|
-
has_many :paranoids, :
|
185
|
+
has_many :paranoids, dependent: :destroy
|
157
186
|
end
|
158
187
|
|
159
188
|
class Paranoid < ActiveRecord::Base
|
@@ -161,34 +190,42 @@ class Paranoid < ActiveRecord::Base
|
|
161
190
|
|
162
191
|
# Paranoid objects will be recovered alongside Paranoic objects
|
163
192
|
# if they were deleted within 10 minutes of the Paranoic object
|
164
|
-
acts_as_paranoid :
|
193
|
+
acts_as_paranoid dependent_recovery_window: 10.minutes
|
165
194
|
end
|
166
195
|
```
|
167
196
|
|
168
197
|
or in the recover statement
|
169
198
|
|
170
199
|
```ruby
|
171
|
-
Paranoiac.only_deleted.where("name = ?", "not dead yet").first
|
200
|
+
Paranoiac.only_deleted.where("name = ?", "not dead yet").first
|
201
|
+
.recover(recovery_window: 30.seconds)
|
172
202
|
```
|
173
203
|
|
174
204
|
### recover!
|
175
205
|
|
176
|
-
You can invoke `recover!` if you wish to raise an error if the recovery fails.
|
206
|
+
You can invoke `recover!` if you wish to raise an error if the recovery fails.
|
207
|
+
The error generally stems from ActiveRecord.
|
177
208
|
|
178
|
-
```
|
209
|
+
```ruby
|
179
210
|
Paranoiac.only_deleted.where("name = ?", "not dead yet").first.recover!
|
180
|
-
|
181
|
-
### => ActiveRecord::RecordInvalid: Validation failed: Name already exists
|
211
|
+
# => ActiveRecord::RecordInvalid: Validation failed: Name already exists
|
182
212
|
```
|
183
213
|
|
184
|
-
Optionally, you may also raise the error by passing
|
214
|
+
Optionally, you may also raise the error by passing `raise_error: true` to the
|
215
|
+
`recover` method. This behaves the same as `recover!`.
|
185
216
|
|
186
|
-
```
|
187
|
-
Paranoiac.only_deleted.where("name = ?", "not dead yet").first.recover(:
|
217
|
+
```ruby
|
218
|
+
Paranoiac.only_deleted.where("name = ?", "not dead yet").first.recover(raise_error: true)
|
188
219
|
```
|
189
220
|
|
190
221
|
### Validation
|
191
|
-
|
222
|
+
|
223
|
+
ActiveRecord's built-in uniqueness validation does not account for records
|
224
|
+
deleted by ActsAsParanoid. If you want to check for uniqueness among
|
225
|
+
non-deleted records only, use the macro `validates_as_paranoid` in your model.
|
226
|
+
Then, instead of using `validates_uniqueness_of`, use
|
227
|
+
`validates_uniqueness_of_without_deleted`. This will keep deleted records from
|
228
|
+
counting against the uniqueness check.
|
192
229
|
|
193
230
|
```ruby
|
194
231
|
class Paranoiac < ActiveRecord::Base
|
@@ -197,10 +234,10 @@ class Paranoiac < ActiveRecord::Base
|
|
197
234
|
validates_uniqueness_of_without_deleted :name
|
198
235
|
end
|
199
236
|
|
200
|
-
p1 = Paranoiac.create(:
|
237
|
+
p1 = Paranoiac.create(name: 'foo')
|
201
238
|
p1.destroy
|
202
239
|
|
203
|
-
p2 = Paranoiac.new(:
|
240
|
+
p2 = Paranoiac.new(name: 'foo')
|
204
241
|
p2.valid? #=> true
|
205
242
|
p2.save
|
206
243
|
|
@@ -208,16 +245,34 @@ p1.recover #=> fails validation!
|
|
208
245
|
```
|
209
246
|
|
210
247
|
### Status
|
211
|
-
|
248
|
+
|
249
|
+
A paranoid object could be deleted or destroyed fully.
|
250
|
+
|
251
|
+
You can check if the object is deleted with the `deleted?` helper
|
212
252
|
|
213
253
|
```ruby
|
214
|
-
Paranoiac.create(:
|
254
|
+
Paranoiac.create(name: 'foo').destroy
|
215
255
|
Paranoiac.with_deleted.first.deleted? #=> true
|
216
256
|
```
|
217
257
|
|
258
|
+
After the first call to .destroy the object is deleted?
|
259
|
+
|
260
|
+
You can check if the object is fully destroyed with destroyed_fully? or deleted_fully?.
|
261
|
+
|
262
|
+
```ruby
|
263
|
+
Paranoiac.create(name: 'foo').destroy
|
264
|
+
Paranoiac.with_deleted.first.deleted? #=> true
|
265
|
+
Paranoiac.with_deleted.first.destroyed_fully? #=> false
|
266
|
+
p1 = Paranoiac.with_deleted.first
|
267
|
+
p1.destroy # this fully destroys the object
|
268
|
+
p1.destroyed_fully? #=> true
|
269
|
+
p1.deleted_fully? #=> true
|
270
|
+
```
|
271
|
+
|
218
272
|
### Scopes
|
219
273
|
|
220
|
-
As you've probably guessed, `with_deleted` and `only_deleted` are scopes. You
|
274
|
+
As you've probably guessed, `with_deleted` and `only_deleted` are scopes. You
|
275
|
+
can, however, chain them freely with other scopes you might have.
|
221
276
|
|
222
277
|
For example:
|
223
278
|
|
@@ -236,10 +291,10 @@ You can work freely with scopes and it will just work:
|
|
236
291
|
```ruby
|
237
292
|
class Paranoiac < ActiveRecord::Base
|
238
293
|
acts_as_paranoid
|
239
|
-
scope :pretty, where(:
|
294
|
+
scope :pretty, where(pretty: true)
|
240
295
|
end
|
241
296
|
|
242
|
-
Paranoiac.create(:
|
297
|
+
Paranoiac.create(pretty: true)
|
243
298
|
|
244
299
|
Paranoiac.pretty.count #=> 1
|
245
300
|
Paranoiac.only_deleted.count #=> 0
|
@@ -256,11 +311,13 @@ Paranoiac.pretty.only_deleted.count #=> 1
|
|
256
311
|
|
257
312
|
Associations are also supported.
|
258
313
|
|
259
|
-
From the simplest behaviors you'd expect to more nifty things like the ones
|
314
|
+
From the simplest behaviors you'd expect to more nifty things like the ones
|
315
|
+
mentioned previously or the usage of the `:with_deleted` option with
|
316
|
+
`belongs_to`
|
260
317
|
|
261
318
|
```ruby
|
262
319
|
class Parent < ActiveRecord::Base
|
263
|
-
has_many :children, :
|
320
|
+
has_many :children, class_name: "ParanoiacChild"
|
264
321
|
end
|
265
322
|
|
266
323
|
class ParanoiacChild < ActiveRecord::Base
|
@@ -268,7 +325,8 @@ class ParanoiacChild < ActiveRecord::Base
|
|
268
325
|
belongs_to :parent
|
269
326
|
|
270
327
|
# You may need to provide a foreign_key like this
|
271
|
-
belongs_to :parent_including_deleted, :
|
328
|
+
belongs_to :parent_including_deleted, class_name: "Parent",
|
329
|
+
foreign_key: 'parent_id', with_deleted: true
|
272
330
|
end
|
273
331
|
|
274
332
|
parent = Parent.first
|
@@ -281,12 +339,14 @@ child.parent_including_deleted #=> Parent (it works!)
|
|
281
339
|
|
282
340
|
### Callbacks
|
283
341
|
|
284
|
-
There are couple of callbacks that you may use when dealing with deletion and
|
285
|
-
|
342
|
+
There are couple of callbacks that you may use when dealing with deletion and
|
343
|
+
recovery of objects. There is `before_recover` and `after_recover` which will
|
344
|
+
be triggered before and after the recovery of an object respectively.
|
286
345
|
|
287
|
-
Default ActiveRecord
|
346
|
+
Default ActiveRecord callbacks such as `before_destroy` and `after_destroy` will
|
347
|
+
be triggered around `.destroy!` and `.destroy_fully!`.
|
288
348
|
|
289
|
-
```
|
349
|
+
```ruby
|
290
350
|
class Paranoiac < ActiveRecord::Base
|
291
351
|
acts_as_paranoid
|
292
352
|
|
@@ -299,14 +359,16 @@ end
|
|
299
359
|
|
300
360
|
Watch out for these caveats:
|
301
361
|
|
302
|
-
-
|
303
|
-
-
|
304
|
-
|
305
|
-
|
362
|
+
- You cannot use scopes named `with_deleted` and `only_deleted`
|
363
|
+
- You cannot use scopes named `deleted_inside_time_window`,
|
364
|
+
`deleted_before_time`, `deleted_after_time` **if** your paranoid column's
|
365
|
+
type is `time`
|
366
|
+
- You cannot name association `*_with_deleted`
|
367
|
+
- `unscoped` will return all records, deleted or not
|
306
368
|
|
307
369
|
# Acknowledgements
|
308
370
|
|
309
|
-
* To [Rick Olson](https://github.com/technoweenie) for creating acts_as_paranoid
|
371
|
+
* To [Rick Olson](https://github.com/technoweenie) for creating `acts_as_paranoid`
|
310
372
|
* To [cheerfulstoic](https://github.com/cheerfulstoic) for adding recursive recovery
|
311
373
|
* To [Jonathan Vaught](https://github.com/gravelpup) for adding paranoid validations
|
312
374
|
* To [Geoffrey Hichborn](https://github.com/phene) for improving the overral code quality and adding support for after_commit
|