Skip to content

Commit 0bb5181

Browse files
handle #delete_all
1 parent f441c37 commit 0bb5181

File tree

2 files changed

+104
-21
lines changed

2 files changed

+104
-21
lines changed

Diff for: lib/paranoia.rb

+36-19
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,11 @@
66
end
77

88
module Paranoia
9-
@@default_sentinel_value = nil
109

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
1814
end
1915

2016
def self.included(klazz)
@@ -58,6 +54,16 @@ def restore(id_or_ids, opts = {})
5854
end
5955
ids.map { |id| only_deleted.find(id).restore!(opts) }
6056
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
6167
end
6268

6369
def paranoia_destroy
@@ -200,18 +206,10 @@ def each_counter_cached_associations
200206
def paranoia_restore_attributes
201207
{
202208
paranoia_column => paranoia_sentinel_value
203-
}.merge(timestamp_attributes_with_current_time)
209+
}.merge(self.class.timestamp_attributes_with_current_time)
204210
end
205211

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
212+
delegate :paranoia_destroy_attributes, to: 'self.class'
215213

216214
def paranoia_find_has_one_target(association)
217215
association_foreign_key = association.options[:through].present? ? association.klass.primary_key : association.foreign_key
@@ -262,6 +260,14 @@ def restore_associated_records(recovery_window_range = nil)
262260
end
263261
end
264262

263+
module Paranoia::Relation
264+
def paranoia_delete_all
265+
update_all(klass.paranoia_destroy_attributes)
266+
end
267+
268+
alias_method :delete_all, :paranoia_delete_all
269+
end
270+
265271
ActiveSupport.on_load(:active_record) do
266272
class ActiveRecord::Base
267273
def self.acts_as_paranoid(options={})
@@ -276,9 +282,10 @@ def self.acts_as_paranoid(options={})
276282
alias_method :really_destroyed?, :destroyed?
277283
alias_method :really_delete, :delete
278284
alias_method :destroy_without_paranoia, :destroy
285+
class << self; delegate :really_delete_all, to: :all end
279286

280287
include Paranoia
281-
class_attribute :paranoia_column, :paranoia_sentinel_value
288+
class_attribute :paranoia_column, :paranoia_sentinel_value, :delete_all_enabled
282289

283290
self.paranoia_column = (options[:column] || :deleted_at).to_s
284291
self.paranoia_sentinel_value = options.fetch(:sentinel_value) { Paranoia.default_sentinel_value }
@@ -297,6 +304,16 @@ class << self; alias_method :without_deleted, :paranoia_scope end
297304
after_restore {
298305
self.class.notify_observers(:after_restore, self) if self.class.respond_to?(:notify_observers)
299306
}
307+
308+
self.delete_all_enabled = options[:delete_all_enabled] || Paranoia.delete_all_enabled
309+
310+
if self.delete_all_enabled
311+
"#{self}::ActiveRecord_Relation".constantize.class_eval do
312+
alias_method :really_delete_all, :delete_all
313+
314+
include Paranoia::Relation
315+
end
316+
end
300317
end
301318

302319
# Please do not use this method in production.

Diff for: test/paranoia_test.rb

+68-2
Original file line numberDiff line numberDiff line change
@@ -1255,6 +1255,71 @@ def test_has_one_with_scope_not_restored
12551255
assert_equal 1, ParanoidHasOneWithScope.count # gamma deleted
12561256
end
12571257

1258+
def test_delete_all_disabled_by_default
1259+
assert_nil ParanoidModel.delete_all_enabled
1260+
1261+
(0...3).each{ ParanoidModel.create }
1262+
assert_equal 3, ParanoidModel.count
1263+
ParanoidModel.delete_all
1264+
assert_equal 0, ParanoidModel.count
1265+
assert_equal 0, ParanoidModel.unscoped.count
1266+
end
1267+
1268+
def test_delete_all_called_on_class
1269+
assert Employee.delete_all_enabled
1270+
1271+
(0...3).each{ Employee.create }
1272+
assert_equal 3, Employee.count
1273+
Employee.delete_all
1274+
assert_equal 0, Employee.count
1275+
assert_equal 3, Employee.unscoped.count
1276+
end
1277+
1278+
def test_delete_all_called_on_relation
1279+
assert Employee.delete_all_enabled
1280+
1281+
(0...3).each{ Employee.create }
1282+
assert_equal 3, Employee.count
1283+
Employee.where(id: 1).delete_all
1284+
assert_equal 2, Employee.count
1285+
assert_equal 3, Employee.unscoped.count
1286+
end
1287+
1288+
def test_really_delete_all_called_on_class
1289+
assert Employee.delete_all_enabled
1290+
1291+
(0...3).each{ Employee.create }
1292+
assert_equal 3, Employee.count
1293+
Employee.really_delete_all
1294+
assert_equal 0, Employee.count
1295+
assert_equal 0, Employee.unscoped.count
1296+
end
1297+
1298+
def test_delete_all_called_on_relation
1299+
assert Employee.delete_all_enabled
1300+
1301+
(0...3).each{ Employee.create }
1302+
assert_equal 3, Employee.count
1303+
Employee.where(id: 1).really_delete_all
1304+
assert_equal 2, Employee.count
1305+
assert_equal 2, Employee.unscoped.count
1306+
end
1307+
1308+
def test_update_has_many_through_relation_delete_associations
1309+
employer = Employer.create
1310+
employee1 = Employee.create
1311+
employee2 = Employee.create
1312+
job = Job.create :employer => employer, :employee => employee1
1313+
1314+
assert_equal 1, employer.jobs.count
1315+
assert_equal 1, employer.jobs.with_deleted.count
1316+
1317+
employer.update(employee_ids: [employee2.id])
1318+
1319+
assert_equal 1, employer.jobs.count
1320+
assert_equal 2, employer.jobs.with_deleted.count
1321+
end
1322+
12581323
private
12591324
def get_featureful_model
12601325
FeaturefulModel.new(:name => "not empty")
@@ -1418,16 +1483,17 @@ class Employer < ActiveRecord::Base
14181483
acts_as_paranoid
14191484
validates_uniqueness_of :name
14201485
has_many :jobs
1421-
has_many :employees, :through => :jobs
1486+
has_many :employees, :through => :jobs, dependent: :destroy
14221487
end
14231488

14241489
class Employee < ActiveRecord::Base
1425-
acts_as_paranoid
1490+
acts_as_paranoid(delete_all_enabled: true)
14261491
has_many :jobs
14271492
has_many :employers, :through => :jobs
14281493
end
14291494

14301495
class Job < ActiveRecord::Base
1496+
acts_as_paranoid(delete_all_enabled: true)
14311497
acts_as_paranoid
14321498
belongs_to :employer
14331499
belongs_to :employee

0 commit comments

Comments
 (0)