Skip to content
Browse files

Add paranoid delete_all implementation [Marshall Roch]

git-svn-id: http://svn.techno-weenie.net/projects/plugins/acts_as_paranoid@1298 567b1171-46fb-0310-a4c9-b4bef9110e78
  • Loading branch information...
1 parent 2cf39e7 commit 1b073c9700835905a8dbe967cfb96491764bc073 technoweenie committed
Showing with 51 additions and 1 deletion.
  1. +4 −0 CHANGELOG
  2. +11 −0 lib/caboose/acts/paranoid.rb
  3. +36 −1 test/paranoid_test.rb
View
4 CHANGELOG
@@ -1,3 +1,7 @@
+* (2 July 2006)
+
+Add paranoid delete_all implementation [Marshall Roch]
+
* (23 May 2006)
Allow setting of future dates for content expiration.
View
11 lib/caboose/acts/paranoid.rb
@@ -28,6 +28,12 @@ module Acts #:nodoc:
# Widget.count_with_deleted
# # SELECT COUNT(*) FROM widgets
#
+ # Widget.delete_all
+ # # UPDATE widgets SET deleted_at = '2005-09-17 17:46:36'
+ #
+ # Widget.delete_all!
+ # # DELETE FROM widgets
+ #
# @widget.destroy
# # UPDATE widgets SET deleted_at = '2005-09-17 17:46:36' WHERE id = 1
#
@@ -49,6 +55,7 @@ class << self
alias_method :find_every_with_deleted, :find_every
alias_method :count_with_deleted, :count
alias_method :calculate_with_deleted, :calculate
+ alias_method :delete_all!, :delete_all
end
end
include InstanceMethods
@@ -86,6 +93,10 @@ def calculate(*args)
with_deleted_scope { calculate_with_deleted(*args) }
end
+ def delete_all(conditions = nil)
+ self.update_all ["#{self.deleted_attribute} = ?", current_time], conditions
+ end
+
protected
def current_time
default_timezone == :utc ? Time.now.utc : Time.now
View
37 test/paranoid_test.rb
@@ -2,7 +2,7 @@
class Widget < ActiveRecord::Base
acts_as_paranoid
- has_many :categories, :dependent => true
+ has_many :categories, :dependent => :destroy
has_and_belongs_to_many :habtm_categories, :class_name => 'Category'
has_one :category
belongs_to :parent_category, :class_name => 'Category'
@@ -48,6 +48,41 @@ def test_should_destroy
assert_equal 4, Category.calculate_with_deleted(:count, :all)
end
+ def test_should_delete_all
+ assert_equal 1, Widget.count
+ assert_equal 2, Widget.calculate_with_deleted(:count, :all)
+ assert_equal 1, Category.count
+ Widget.delete_all
+ assert_equal 0, Widget.count
+ # delete_all doesn't call #destroy, so the dependent callback never fires
+ assert_equal 1, Category.count
+ assert_equal 2, Widget.calculate_with_deleted(:count, :all)
+ end
+
+ def test_should_delete_all_with_conditions
+ assert_equal 1, Widget.count
+ assert_equal 2, Widget.calculate_with_deleted(:count, :all)
+ Widget.delete_all("id < 3")
+ assert_equal 0, Widget.count
+ assert_equal 2, Widget.calculate_with_deleted(:count, :all)
+ end
+
+ def test_should_delete_all2
+ assert_equal 1, Category.count
+ assert_equal 4, Category.calculate_with_deleted(:count, :all)
+ Category.delete_all!
+ assert_equal 0, Category.count
+ assert_equal 0, Category.calculate_with_deleted(:count, :all)
+ end
+
+ def test_should_delete_all_with_conditions2
+ assert_equal 1, Category.count
+ assert_equal 4, Category.calculate_with_deleted(:count, :all)
+ Category.delete_all!("id < 3")
+ assert_equal 0, Category.count
+ assert_equal 2, Category.calculate_with_deleted(:count, :all)
+ end
+
def test_should_not_count_deleted
assert_equal 1, Widget.count
assert_equal 1, Widget.count(['title=?', 'widget 1'])

0 comments on commit 1b073c9

Please sign in to comment.
Something went wrong with that request. Please try again.