paranoia 3.0.0 → 3.0.1

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
  SHA256:
3
- metadata.gz: 86ea97ca665a462c533110c63c1b277d440292839725fbe8b2e3bc8f295d8850
4
- data.tar.gz: 403501a4e47840a726eee8eaeef9e38a4c44c041c6baee5b3cba20df368558d9
3
+ metadata.gz: 8e7ad759d079b1e2bea13d47f092005677c98dee88423a12666fc77db15e8249
4
+ data.tar.gz: f95c4af13b076d6d235d3b6d0b7057595742a8dcd177e71b0ada70cb11674f91
5
5
  SHA512:
6
- metadata.gz: 6690c30c44ed3713090776c3236d4e502cc8369d9556765c689dea4e6ac6c92400cabb95e31afcc37fb7479bdb543125288b5512dca7dc964f33a023b46d1171
7
- data.tar.gz: 5cacc8c926267c5de867b33ea74bde450b353618af43144a73da1aeb8878526510c71587c2720f88e96ad3eb97b90dd9489711b2200fa14faaa628bed7101b77
6
+ metadata.gz: 9b42a61dde0a3e2c417f2d7a0c71c23ad4b1c6980fe4a0a232313625017fe5c5e24065405a28e202bb3751d34e379753ae70a908d552f6704c7b1e78fb18862a
7
+ data.tar.gz: 45fc5b4fe5c4705f29e9bd208dd18d1fcec907c79649167467dc76ae883e002da9b2db93ac41148ab7a47872a39aa3c7545c754e7627dd8b5348e519654806ee
@@ -15,7 +15,7 @@ jobs:
15
15
  strategy:
16
16
  fail-fast: false
17
17
  matrix:
18
- rails: ["edge", "~> 7.2.0", "~> 7.1.0", "~> 7.0.0", "~> 6.1.0"]
18
+ rails: ["~> 7.2.0", "~> 7.1.0", "~> 7.0.0", "~> 6.1.0"]
19
19
  ruby: ["3.3","3.2", "3.1", "3.0", "2.7"]
20
20
  exclude:
21
21
  - rails: "~> 7.2.0"
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # paranoia Changelog
2
2
 
3
+ ## 3.0.1 - January 19, 2025
4
+
5
+ - [#566](https://github.com/rubysherpas/paranoia/pull/566) Handle #delete_all
6
+ - [#559](https://github.com/rubysherpas/paranoia/pull/559) Trigger an after_commit callback when restoring a record
7
+ - [#567](https://github.com/rubysherpas/paranoia/pull/567) Fix typo in newly added readme
8
+
3
9
  ## 3.0.0 - August 13, 2024
4
10
 
5
11
  _Tagged as 3.0 as Ruby + Rails version constraints have been modernised._
data/README.md CHANGED
@@ -206,6 +206,19 @@ Client.restore(id, :recursive => true, :recovery_window => 2.minutes)
206
206
  client.restore(:recursive => true, :recovery_window => 2.minutes)
207
207
  ```
208
208
 
209
+ If you want to trigger an after_commit callback when restoring a record:
210
+
211
+ ``` ruby
212
+ class Client < ActiveRecord::Base
213
+ acts_as_paranoid after_restore_commit: true
214
+
215
+ after_commit :commit_called, on: :restore
216
+ # or
217
+ after_restore_commit :commit_called
218
+ ...
219
+ end
220
+ ```
221
+
209
222
  Note that by default paranoia will not prevent that a soft destroyed object can't be associated with another object of a different model.
210
223
  A Rails validator is provided should you require this functionality:
211
224
  ``` ruby
@@ -348,6 +361,21 @@ end
348
361
  # => NoMethodError: undefined method `with_deleted' for #<Class:0x0123456>
349
362
  ```
350
363
 
364
+ #### delete_all:
365
+
366
+ The gem supports `delete_all` method, however it is disabled by default, to enable it add this in your `environment` file
367
+
368
+ ``` ruby
369
+ Paranoia.delete_all_enabled = true
370
+ ```
371
+ alternatively, you can enable/disable it for specific models as follow:
372
+
373
+ ``` ruby
374
+ class User < ActiveRecord::Base
375
+ acts_as_paranoid(delete_all_enabled: true)
376
+ end
377
+ ```
378
+
351
379
  ## Acts As Paranoid Migration
352
380
 
353
381
  You can replace the older `acts_as_paranoid` methods as follows:
@@ -1,3 +1,3 @@
1
1
  module Paranoia
2
- VERSION = '3.0.0'.freeze
2
+ VERSION = '3.0.1'.freeze
3
3
  end
data/lib/paranoia.rb CHANGED
@@ -6,15 +6,11 @@ if [ActiveRecord::VERSION::MAJOR, ActiveRecord::VERSION::MINOR] == [5, 2] ||
6
6
  end
7
7
 
8
8
  module Paranoia
9
- @@default_sentinel_value = nil
10
9
 
11
- # Change default_sentinel_value in a rails initializer
12
- def self.default_sentinel_value=(val)
13
- @@default_sentinel_value = val
14
- end
15
-
16
- def self.default_sentinel_value
17
- @@default_sentinel_value
10
+ class << self
11
+ # Change default values in a rails initializer
12
+ attr_accessor :default_sentinel_value,
13
+ :delete_all_enabled
18
14
  end
19
15
 
20
16
  def self.included(klazz)
@@ -58,6 +54,16 @@ module Paranoia
58
54
  end
59
55
  ids.map { |id| only_deleted.find(id).restore!(opts) }
60
56
  end
57
+
58
+ def paranoia_destroy_attributes
59
+ {
60
+ paranoia_column => current_time_from_proper_timezone
61
+ }.merge(timestamp_attributes_with_current_time)
62
+ end
63
+
64
+ def timestamp_attributes_with_current_time
65
+ timestamp_attributes_for_update_in_model.each_with_object({}) { |attr,hash| hash[attr] = current_time_from_proper_timezone }
66
+ end
61
67
  end
62
68
 
63
69
  def paranoia_destroy
@@ -88,7 +94,16 @@ module Paranoia
88
94
  end
89
95
 
90
96
  def trigger_transactional_callbacks?
91
- super || @_trigger_destroy_callback && paranoia_destroyed?
97
+ super || @_trigger_destroy_callback && paranoia_destroyed? ||
98
+ @_trigger_restore_callback && !paranoia_destroyed?
99
+ end
100
+
101
+ def transaction_include_any_action?(actions)
102
+ super || actions.any? do |action|
103
+ if action == :restore
104
+ paranoia_after_restore_commit && @_trigger_restore_callback
105
+ end
106
+ end
92
107
  end
93
108
 
94
109
  def paranoia_delete
@@ -115,6 +130,10 @@ module Paranoia
115
130
  if within_recovery_window?(recovery_window_range) && ((noop_if_frozen && !@attributes.frozen?) || !noop_if_frozen)
116
131
  @_disable_counter_cache = !paranoia_destroyed?
117
132
  write_attribute paranoia_column, paranoia_sentinel_value
133
+ if paranoia_after_restore_commit
134
+ @_trigger_restore_callback = true
135
+ add_to_transaction
136
+ end
118
137
  update_columns(paranoia_restore_attributes)
119
138
  each_counter_cached_associations do |association|
120
139
  if send(association.reflection.name)
@@ -128,6 +147,10 @@ module Paranoia
128
147
  end
129
148
 
130
149
  self
150
+ ensure
151
+ if paranoia_after_restore_commit
152
+ @_trigger_restore_callback = false
153
+ end
131
154
  end
132
155
  alias :restore :restore!
133
156
 
@@ -200,18 +223,10 @@ module Paranoia
200
223
  def paranoia_restore_attributes
201
224
  {
202
225
  paranoia_column => paranoia_sentinel_value
203
- }.merge(timestamp_attributes_with_current_time)
226
+ }.merge(self.class.timestamp_attributes_with_current_time)
204
227
  end
205
228
 
206
- def paranoia_destroy_attributes
207
- {
208
- paranoia_column => current_time_from_proper_timezone
209
- }.merge(timestamp_attributes_with_current_time)
210
- end
211
-
212
- def timestamp_attributes_with_current_time
213
- timestamp_attributes_for_update_in_model.each_with_object({}) { |attr,hash| hash[attr] = current_time_from_proper_timezone }
214
- end
229
+ delegate :paranoia_destroy_attributes, to: 'self.class'
215
230
 
216
231
  def paranoia_find_has_one_target(association)
217
232
  association_foreign_key = association.options[:through].present? ? association.klass.primary_key : association.foreign_key
@@ -262,6 +277,31 @@ module Paranoia
262
277
  end
263
278
  end
264
279
 
280
+ module ActiveRecord
281
+ module Transactions
282
+ module RestoreSupport
283
+ def self.included(base)
284
+ base::ACTIONS << :restore unless base::ACTIONS.include?(:restore)
285
+ end
286
+ end
287
+
288
+ module ClassMethods
289
+ def after_restore_commit(*args, &block)
290
+ set_options_for_callbacks!(args, on: :restore)
291
+ set_callback(:commit, :after, *args, &block)
292
+ end
293
+ end
294
+ end
295
+ end
296
+
297
+ module Paranoia::Relation
298
+ def paranoia_delete_all
299
+ update_all(klass.paranoia_destroy_attributes)
300
+ end
301
+
302
+ alias_method :delete_all, :paranoia_delete_all
303
+ end
304
+
265
305
  ActiveSupport.on_load(:active_record) do
266
306
  class ActiveRecord::Base
267
307
  def self.acts_as_paranoid(options={})
@@ -276,12 +316,15 @@ ActiveSupport.on_load(:active_record) do
276
316
  alias_method :really_destroyed?, :destroyed?
277
317
  alias_method :really_delete, :delete
278
318
  alias_method :destroy_without_paranoia, :destroy
319
+ class << self; delegate :really_delete_all, to: :all end
279
320
 
280
321
  include Paranoia
281
- class_attribute :paranoia_column, :paranoia_sentinel_value
322
+ class_attribute :paranoia_column, :paranoia_sentinel_value, :paranoia_after_restore_commit,
323
+ :delete_all_enabled
282
324
 
283
325
  self.paranoia_column = (options[:column] || :deleted_at).to_s
284
326
  self.paranoia_sentinel_value = options.fetch(:sentinel_value) { Paranoia.default_sentinel_value }
327
+ self.paranoia_after_restore_commit = options.fetch(:after_restore_commit) { false }
285
328
  def self.paranoia_scope
286
329
  where(paranoia_column => paranoia_sentinel_value)
287
330
  end
@@ -297,6 +340,20 @@ ActiveSupport.on_load(:active_record) do
297
340
  after_restore {
298
341
  self.class.notify_observers(:after_restore, self) if self.class.respond_to?(:notify_observers)
299
342
  }
343
+
344
+ if paranoia_after_restore_commit
345
+ ActiveRecord::Transactions.send(:include, ActiveRecord::Transactions::RestoreSupport)
346
+ end
347
+
348
+ self.delete_all_enabled = options[:delete_all_enabled] || Paranoia.delete_all_enabled
349
+
350
+ if self.delete_all_enabled
351
+ "#{self}::ActiveRecord_Relation".constantize.class_eval do
352
+ alias_method :really_delete_all, :delete_all
353
+
354
+ include Paranoia::Relation
355
+ end
356
+ end
300
357
  end
301
358
 
302
359
  # Please do not use this method in production.
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paranoia
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - radarlistener@gmail.com
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-08-13 00:00:00.000000000 Z
10
+ date: 2025-01-19 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: activerecord
@@ -91,7 +90,6 @@ homepage: https://github.com/rubysherpas/paranoia
91
90
  licenses:
92
91
  - MIT
93
92
  metadata: {}
94
- post_install_message:
95
93
  rdoc_options: []
96
94
  require_paths:
97
95
  - lib
@@ -106,8 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
104
  - !ruby/object:Gem::Version
107
105
  version: 1.3.6
108
106
  requirements: []
109
- rubygems_version: 3.4.10
110
- signing_key:
107
+ rubygems_version: 3.6.2
111
108
  specification_version: 4
112
109
  summary: Paranoia is a re-implementation of acts_as_paranoid for Rails 3, 4, and 5,
113
110
  using much, much, much less code.