soft_deletion 1.11.0 → 1.13.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/Readme.md +31 -0
- data/lib/soft_deletion/core.rb +15 -1
- data/lib/soft_deletion/setup.rb +2 -0
- data/lib/soft_deletion/version.rb +1 -1
- 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: be5a857dd49c9d30a402c6d241e4b3c16c474c797c8bfefb42d97210dc16deaa
|
4
|
+
data.tar.gz: d3c386ce8a0604e556239b6f924b3c623c1e493958364b5eccf99c985f8611d6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bacaea7cdb75414da79d3002ec07406c210ba63a0eb05b305c808aa410ae4235f34c883cc3ce2bcf39d9002253f689a35ca7c9306c5d68d755a6b79bbc011216
|
7
|
+
data.tar.gz: 928bf5bd616e71bea0ca5624ab07332fc3a2c09a3713718e8ce197c32f18cb5864fa222aa1bcdbdfe3556e765a2abf7107a56d6f1839c39458668aaf6d606e9a
|
data/Readme.md
CHANGED
@@ -44,8 +44,39 @@ user.products.count == 10
|
|
44
44
|
|
45
45
|
# soft delete many
|
46
46
|
User.soft_delete_all!(1,2,3,4)
|
47
|
+
|
48
|
+
# get soft deleted records
|
49
|
+
User.soft_deleted
|
50
|
+
```
|
51
|
+
|
52
|
+
To add the `deleted_at` to your model, you can either generate a migration using:
|
53
|
+
|
54
|
+
```
|
55
|
+
rails generate migration add_deleted_at_to_users deleted_at:datetime:index
|
56
|
+
```
|
57
|
+
|
58
|
+
or create a migration file yourself like:
|
59
|
+
|
60
|
+
```Ruby
|
61
|
+
class AddDeletedAtToUsers < ActiveRecord::Migration[6.0]
|
62
|
+
def change
|
63
|
+
add_column :users, :deleted_at, :datetime
|
64
|
+
add_index :users, :deleted_at
|
65
|
+
end
|
66
|
+
end
|
47
67
|
```
|
48
68
|
|
69
|
+
By default `soft_deletion` does not change the `updated_at` value in the record. If you need this feature, you can use the `update_timestamp` option, passing the name of the column holding the timestamp to be updated, like, for example, `updated_at`:
|
70
|
+
|
71
|
+
```Ruby
|
72
|
+
require 'soft_deletion'
|
73
|
+
|
74
|
+
class User < ActiveRecord::Base
|
75
|
+
has_soft_deletion update_timestamp: :updated_at
|
76
|
+
end
|
77
|
+
```
|
78
|
+
|
79
|
+
|
49
80
|
TODO
|
50
81
|
====
|
51
82
|
- has_many :through should delete join associations on soft_delete
|
data/lib/soft_deletion/core.rb
CHANGED
@@ -9,6 +9,7 @@ module SoftDeletion
|
|
9
9
|
base.define_model_callbacks :soft_delete
|
10
10
|
base.define_model_callbacks :soft_undelete
|
11
11
|
base.cattr_accessor :soft_delete_default_scope
|
12
|
+
base.cattr_accessor :soft_deletion_update_timestamp
|
12
13
|
end
|
13
14
|
|
14
15
|
module ClassMethods
|
@@ -26,8 +27,16 @@ module SoftDeletion
|
|
26
27
|
Thread.current[key] = nil
|
27
28
|
end
|
28
29
|
|
30
|
+
def soft_deleted
|
31
|
+
with_deleted { where.not(deleted_at: nil) }
|
32
|
+
end
|
33
|
+
|
29
34
|
def mark_as_soft_deleted_sql
|
30
|
-
|
35
|
+
t = Time.now
|
36
|
+
|
37
|
+
{deleted_at: t}.tap do |h|
|
38
|
+
h[self.soft_deletion_update_timestamp] = t if self.soft_deletion_update_timestamp
|
39
|
+
end
|
31
40
|
end
|
32
41
|
|
33
42
|
def soft_delete_all!(ids_or_models)
|
@@ -58,6 +67,11 @@ module SoftDeletion
|
|
58
67
|
|
59
68
|
def mark_as_deleted
|
60
69
|
self.deleted_at ||= Time.now
|
70
|
+
if self.class.soft_deletion_update_timestamp
|
71
|
+
new_timestamp = [self.deleted_at, send(self.class.soft_deletion_update_timestamp)].compact.max
|
72
|
+
|
73
|
+
send("#{self.class.soft_deletion_update_timestamp}=", new_timestamp)
|
74
|
+
end
|
61
75
|
end
|
62
76
|
|
63
77
|
def mark_as_undeleted
|
data/lib/soft_deletion/setup.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: soft_deletion
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.13.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Grosser
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2025-03-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|