Commit 1fc916b9 by Sam Figueroa

Add delete_all to paranoid

- don’t let people destroy paranoid models with delete_all
parent d9267b77
...@@ -249,6 +249,10 @@ class ActiveRecord::Base ...@@ -249,6 +249,10 @@ class ActiveRecord::Base
def self.paranoid? ; false ; end def self.paranoid? ; false ; end
def paranoid? ; self.class.paranoid? ; end def paranoid? ; self.class.paranoid? ; end
def self.delete_all(conditions=nil)
super and return unless paranoid?
where(conditions).update_all(self.new.send :paranoia_destroy_attributes)
end
private private
def paranoia_column def paranoia_column
......
...@@ -879,6 +879,36 @@ class ParanoiaTest < test_framework ...@@ -879,6 +879,36 @@ class ParanoiaTest < test_framework
related.valid? related.valid?
end end
def test_delete_all
model = ParanoidModel
count = 3
models = count.times.map { model.new }
assert_equal 0, model.count
models.each(&:save!)
assert_equal count, model.count
model.delete_all
assert_equal 0, model.count
assert_equal count, model.with_deleted.count
assert model.with_deleted.all?(&:deleted?)
end
def test_delete_all_for_non_paranoid_model
model = PlainModel
count = 3
models = count.times.map { model.new }
assert_equal 0, model.count
models.each(&:save!)
assert_equal count, model.count
model.delete_all
assert_equal 0, model.count
assert_equal 0, model.unscoped.count
end
# TODO: find a fix for Rails 4.1 # TODO: find a fix for Rails 4.1
if ActiveRecord::VERSION::STRING !~ /\A4\.1/ if ActiveRecord::VERSION::STRING !~ /\A4\.1/
def test_counter_cache_column_update_on_really_destroy def test_counter_cache_column_update_on_really_destroy
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment