glib-web 0.5.73 → 0.5.75
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/app/models/concerns/glib/soft_deletable.rb +68 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 024cbfd3c136b878553f4f7cbeb484cd3b1591bcda6e77ef4b0c9c8fd9217712
|
4
|
+
data.tar.gz: e129fc931a5f85f4870a1e62706589fbd410fef4eb60c46527f335cfe7bc09dd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 68da687986a2806b6ac987beff6fe8aded090ad33b664846b28c40ca061b1f05361ed272483207e04c361af57ff292e1e6a8d473271d1b005f99de77f51616cc
|
7
|
+
data.tar.gz: 2fff915aaf95c1d3c2dddf6ddf02492e4a5f936c6ef8c03c702240108acacfe7e2eb872104e242f3c2e88262f4c75ed9b2770f4219ab2e4d05a96aa784174f97
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module Glib
|
2
|
+
module SoftDeletable
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
default_scope { where(deleted_at: nil) }
|
7
|
+
scope :with_deleted, -> { unscope(where: :deleted_at) }
|
8
|
+
|
9
|
+
# "Soft delete" - set deleted_at if it is nil, actually destroy the record
|
10
|
+
# if forced.
|
11
|
+
#
|
12
|
+
# @param force [Boolean]
|
13
|
+
def destroy(force = nil)
|
14
|
+
return force_destroy_record if force == :force
|
15
|
+
return self if deleted?
|
16
|
+
|
17
|
+
soft_destroy_record
|
18
|
+
end
|
19
|
+
|
20
|
+
# Revive a soft-deleted record and associated records if soft-deleted,
|
21
|
+
# otherwise return self
|
22
|
+
def revive
|
23
|
+
return self unless deleted?
|
24
|
+
|
25
|
+
ActiveRecord::Base.transaction do
|
26
|
+
update(deleted_at: nil)
|
27
|
+
revive_associated_records
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# Whether or not a record is deleted
|
32
|
+
def deleted?
|
33
|
+
deleted_at.present?
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
def force_destroy_record
|
38
|
+
ActiveRecord::Base.transaction do
|
39
|
+
destroy_associated_records(:force)
|
40
|
+
method(:destroy).super_method.call
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def soft_destroy_record
|
45
|
+
ActiveRecord::Base.transaction do
|
46
|
+
destroy_associated_records
|
47
|
+
update(deleted_at: Time.zone.now)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def destroy_associated_records(force = nil)
|
52
|
+
associated_records.each { |r| r.destroy(force) }
|
53
|
+
end
|
54
|
+
|
55
|
+
def revive_associated_records
|
56
|
+
associated_records.each(&:revive)
|
57
|
+
end
|
58
|
+
|
59
|
+
# This should return an array of all associated records of an object that
|
60
|
+
# should be soft deleted with it (i.e. those that have dependent: :destroy
|
61
|
+
# set). In principle we could figure this out automatically but in the
|
62
|
+
# interest of simplicity we'll just define it manually for each class.
|
63
|
+
def associated_records
|
64
|
+
[]
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: glib-web
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.75
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ''
|
@@ -113,6 +113,7 @@ files:
|
|
113
113
|
- app/helpers/glib/json_ui/view_builder/fields.rb
|
114
114
|
- app/helpers/glib/json_ui/view_builder/panels.rb
|
115
115
|
- app/helpers/glib/urls_helper.rb
|
116
|
+
- app/models/concerns/glib/soft_deletable.rb
|
116
117
|
- app/models/glib/active_storage/attachment.rb
|
117
118
|
- app/models/glib/active_storage/blob.rb
|
118
119
|
- app/models/glib/application_record.rb
|