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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 60185c6924bee924839cffc700ca82fea6244b3c
4
- data.tar.gz: 3b533e035064b8d54f20cef9f8a8b1dadc81538a
3
+ metadata.gz: 1d0ebc9e989d471df9de539fa9f0a7189ec4ab04
4
+ data.tar.gz: 9229273513eee94f67040e1c792513b17debaecc
5
5
  SHA512:
6
- metadata.gz: 282ea0ada88bbb6d94e26100b9f2d1701fa8f532a0a396b8979648a3126dd4d2573c582f2c962f3cd46151bd2a899d69aec9164b08ef405856f994fd467fbe15
7
- data.tar.gz: 77573041c37ac6dae54b8d12e3ccf9bbc3420112657e8feca96b0ec570366310ae680c84ad7751891c418f435f9efb1524a303e65b0446c76b2063275c997eb2
6
+ metadata.gz: 81203ac1d02c7d50a8880d701c2967c56ba4ac264c36e8848bcf2b23c318b6e15c426764444b23966008e177e60810635ae156461e78b3575dde6d074d80ac2b
7
+ data.tar.gz: fbdd2b2f21ed9e83e9a359cf0a513e28b37b2a53a4eb62f84f2f18eb667eecc87685c5004126a4fd9cee174f3432c860f5de500495b9da7ea36d5fcfd2741de8
@@ -5,11 +5,7 @@ module ActiveArchive
5
5
  base.extend Methods
6
6
  base.extend Scopes
7
7
 
8
- base.instance_eval do
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
- destroy_with_permanent_records(force)
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(validate=nil)
36
+ def unarchive(options=nil)
41
37
  with_transaction_returning_status do
42
- run_callbacks(:unarchive) { set_archived_at(nil, validate) }
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 destroy_with_permanent_records(force=nil)
68
- run_callbacks(:destroy) { archived? || (new_record? ? save : set_archived_at(Time.now, force)) }
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 unarchive_destroyed_dependent_records
112
- self.class.reflections
113
- .lazy
114
- .select { |name, reflection| (reflection.options[:dependent].to_s == 'destroy'.freeze) && reflection.klass.archivable? }
115
- .each do |name, reflection|
116
- cardinality = reflection.macro.to_s.gsub('has_'.freeze, ''.freeze)
117
-
118
- if cardinality == 'many'.freeze
119
- records = archived_at.nil? ? send(name).unscoped : send(name).unscoped.where(
120
- [
121
- "#{reflection.quoted_table_name}.archived_at > ? AND #{reflection.quoted_table_name}.archived_at < ?",
122
- archived_at - ActiveArchive.configuration.dependent_record_window,
123
- archived_at + ActiveArchive.configuration.dependent_record_window
124
- ]
125
- )
126
- elsif cardinality == 'one'.freeze or cardinality == 'belongs_to'.freeze
127
- self.class.unscoped do
128
- records = [] << send(name)
129
- end
130
- end
131
-
132
- [records].flatten.compact.lazy.each { |d| d.unarchive }
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 = self.class.unscoped.find(id)
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
@@ -1,3 +1,3 @@
1
1
  module ActiveArchive
2
- VERSION = "2.3.0"
2
+ VERSION = "2.4.0"
3
3
  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.3.0
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: 2015-10-17 00:00:00.000000000 Z
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.4.8
174
+ rubygems_version: 2.5.1
175
175
  signing_key:
176
176
  specification_version: 4
177
177
  summary: Gem for soft-deleting ActiveRecord objects.