active_archive 2.3.0 → 2.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/active_archive/base.rb +53 -50
- data/lib/active_archive/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1d0ebc9e989d471df9de539fa9f0a7189ec4ab04
|
4
|
+
data.tar.gz: 9229273513eee94f67040e1c792513b17debaecc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 81203ac1d02c7d50a8880d701c2967c56ba4ac264c36e8848bcf2b23c318b6e15c426764444b23966008e177e60810635ae156461e78b3575dde6d074d80ac2b
|
7
|
+
data.tar.gz: fbdd2b2f21ed9e83e9a359cf0a513e28b37b2a53a4eb62f84f2f18eb667eecc87685c5004126a4fd9cee174f3432c860f5de500495b9da7ea36d5fcfd2741de8
|
data/lib/active_archive/base.rb
CHANGED
@@ -5,11 +5,7 @@ module ActiveArchive
|
|
5
5
|
base.extend Methods
|
6
6
|
base.extend Scopes
|
7
7
|
|
8
|
-
base.instance_eval
|
9
|
-
define_model_callbacks :unarchive
|
10
|
-
|
11
|
-
before_unarchive :unarchive_destroyed_dependent_records
|
12
|
-
end
|
8
|
+
base.instance_eval { define_model_callbacks(:unarchive) }
|
13
9
|
end
|
14
10
|
|
15
11
|
def archived?
|
@@ -25,7 +21,7 @@ module ActiveArchive
|
|
25
21
|
if unarchivable? || should_force_destroy?(force)
|
26
22
|
permanently_delete_records_after { super() }
|
27
23
|
else
|
28
|
-
|
24
|
+
destroy_with_active_archive(force)
|
29
25
|
end
|
30
26
|
end
|
31
27
|
end
|
@@ -37,9 +33,9 @@ module ActiveArchive
|
|
37
33
|
I18n.t("active_archive.archival.#{archived? ? :archived : :unarchived}")
|
38
34
|
end
|
39
35
|
|
40
|
-
def unarchive(
|
36
|
+
def unarchive(options=nil)
|
41
37
|
with_transaction_returning_status do
|
42
|
-
|
38
|
+
(should_unarchive_parent_first?(options) ? unarchival.reverse : unarchival).lazy.each { |r| r.call(options) }
|
43
39
|
self
|
44
40
|
end
|
45
41
|
end
|
@@ -64,11 +60,20 @@ module ActiveArchive
|
|
64
60
|
end
|
65
61
|
end
|
66
62
|
|
67
|
-
def
|
68
|
-
run_callbacks(:destroy)
|
63
|
+
def destroy_with_active_archive(force=nil)
|
64
|
+
run_callbacks(:destroy) do
|
65
|
+
(archived? || new_record?) ? save : set_archived_at(Time.now, force)
|
66
|
+
true
|
67
|
+
end
|
68
|
+
|
69
69
|
archived? ? self : false
|
70
70
|
end
|
71
71
|
|
72
|
+
def get_archived_record
|
73
|
+
record_id = (self.respond_to?(:parent_id) && self.parent_id.present?) ? parent_id : id
|
74
|
+
self.class.unscoped.find(record_id)
|
75
|
+
end
|
76
|
+
|
72
77
|
def get_dependent_records
|
73
78
|
dependent_records = {}
|
74
79
|
self.class.reflections.lazy.each do |key, reflection|
|
@@ -89,7 +94,7 @@ module ActiveArchive
|
|
89
94
|
|
90
95
|
def permanently_delete_records(dependent_records)
|
91
96
|
dependent_records.lazy.each do |klass, ids|
|
92
|
-
ids.each do |id|
|
97
|
+
ids.lazy.each do |id|
|
93
98
|
record = begin
|
94
99
|
klass.unscoped.find(id)
|
95
100
|
rescue ::ActiveRecord::RecordNotFound
|
@@ -108,59 +113,53 @@ module ActiveArchive
|
|
108
113
|
return(dependent_results)
|
109
114
|
end
|
110
115
|
|
111
|
-
def
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
send(name, :reload)
|
134
|
-
end
|
116
|
+
def unarchival
|
117
|
+
[
|
118
|
+
->(_validate) { unarchive_destroyed_dependent_records(_validate) },
|
119
|
+
->(_validate) { run_callbacks(:unarchive) { set_archived_at(nil, _validate) } }
|
120
|
+
]
|
121
|
+
end
|
122
|
+
|
123
|
+
def unarchive_destroyed_dependent_records(force = nil)
|
124
|
+
self.class.reflections.select do |name, reflection|
|
125
|
+
'destroy'.freeze == reflection.options[:dependent].to_s && reflection.klass.archivable?
|
126
|
+
end.each do |name, reflection|
|
127
|
+
cardinality = reflection.macro.to_s.gsub('has_'.freeze, ''.freeze).to_sym
|
128
|
+
case cardinality
|
129
|
+
when :many
|
130
|
+
records = (archived_at ? set_record_window(send(name), name, reflection) : send(name))
|
131
|
+
when :one, :belongs_to
|
132
|
+
self.class.unscoped { records = [] << send(name) }
|
133
|
+
end
|
134
|
+
|
135
|
+
[records].flatten.compact.lazy.each { |d| d.unarchive(force) }
|
136
|
+
send(name, :reload)
|
137
|
+
end
|
135
138
|
end
|
136
139
|
|
137
140
|
def set_archived_at(value, force=nil)
|
138
141
|
return self unless archivable?
|
139
|
-
record =
|
142
|
+
record = get_archived_record
|
140
143
|
record.archived_at = value
|
141
144
|
|
142
145
|
begin
|
143
146
|
should_ignore_validations?(force) ? record.save(validate: false) : record.save!
|
144
|
-
|
145
|
-
if ::Gem::Version.new(::ActiveRecord::VERSION::STRING) < ::Gem::Version.new('4.2.0'.freeze)
|
146
|
-
@attributes = record.attributes
|
147
|
-
@attributes_cache = record.attributes.except(record.class.serialized_attributes.keys)
|
148
|
-
|
149
|
-
if defined?(::ActiveRecord::AttributeMethods::Serialization::Attribute)
|
150
|
-
serialized_attribute_class = ::ActiveRecord::AttributeMethods::Serialization::Attribute
|
151
|
-
self.class.serialized_attributes.lazy.each do |key, coder|
|
152
|
-
@attributes[key] = serialized_attribute_class.new(coder, @attributes[key], :unserialized) if @attributes.key?(key)
|
153
|
-
end
|
154
|
-
end
|
155
|
-
else
|
156
|
-
@attributes = record.instance_variable_get('@attributes'.freeze)
|
157
|
-
end
|
147
|
+
@attributes = record.instance_variable_get('@attributes'.freeze)
|
158
148
|
rescue Exception => e
|
159
149
|
record.destroy
|
160
150
|
raise(e)
|
161
151
|
end
|
162
152
|
end
|
163
153
|
|
154
|
+
def set_record_window(request, name, reflection)
|
155
|
+
send(name).unscope(where: :archived_at)
|
156
|
+
.where([
|
157
|
+
"#{reflection.quoted_table_name}.archived_at > ? AND #{reflection.quoted_table_name}.archived_at < ?",
|
158
|
+
archived_at - ActiveArchive.configuration.dependent_record_window,
|
159
|
+
archived_at + ActiveArchive.configuration.dependent_record_window
|
160
|
+
])
|
161
|
+
end
|
162
|
+
|
164
163
|
def should_force_destroy?(force)
|
165
164
|
(Hash === force) ? force[:force] : (:force == force)
|
166
165
|
end
|
@@ -169,5 +168,9 @@ module ActiveArchive
|
|
169
168
|
(Hash === force) && (false == force[:validate])
|
170
169
|
end
|
171
170
|
|
171
|
+
def should_unarchive_parent_first?(order)
|
172
|
+
(Hash === order) && (true == order[:reverse])
|
173
|
+
end
|
174
|
+
|
172
175
|
end
|
173
176
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_archive
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Juan Gomez
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-01-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -171,7 +171,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
171
171
|
version: '0'
|
172
172
|
requirements: []
|
173
173
|
rubyforge_project:
|
174
|
-
rubygems_version: 2.
|
174
|
+
rubygems_version: 2.5.1
|
175
175
|
signing_key:
|
176
176
|
specification_version: 4
|
177
177
|
summary: Gem for soft-deleting ActiveRecord objects.
|