public
Description: ActiveRecord plugin allowing you to hide and restore records without actually deleting them.
Clone URL: git://github.com/technoweenie/acts_as_paranoid.git
Override exists? and apply the delete scope.
This needs to be done because commit 
0ad24df6ed389c1d57970c6e7650c5b1b7ae7676 in rails changed the exists? 
method so that it doesn't use find anymore.
georg (author)
Tue Apr 15 09:12:15 -0700 2008
commit  1e9144e483524b4f246a1768462eadb22f634f19
tree    df9a93728b6c2a14fad1a359448a1931e7e966c9
parent  64e54fea5c8a780f16bc8c3d70b6974b4d915acb
...
25
26
27
 
28
29
30
...
25
26
27
28
29
30
31
0
@@ -25,5 +25,6 @@ class << ActiveRecord::Base
0
   alias_method_chain :belongs_to, :deleted
0
   alias_method :has_many_with_deleted, :has_many
0
   alias_method :has_many, :has_many_without_deleted
0
+ alias_method :exists_with_deleted?, :exists?
0
 end
0
 ActiveRecord::Base.send :include, Caboose::Acts::Paranoid
0
\ No newline at end of file
...
87
88
89
 
 
 
 
90
91
92
...
87
88
89
90
91
92
93
94
95
96
0
@@ -87,6 +87,10 @@ module Caboose #:nodoc:
0
             end
0
           end
0
 
0
+ def exists?(*args)
0
+ with_deleted_scope { exists_with_deleted?(*args) }
0
+ end
0
+
0
           def count_with_deleted(*args)
0
             calculate_with_deleted(:count, *construct_count_options_from_args(*args))
0
           end
...
41
42
43
 
 
 
 
 
44
45
46
...
41
42
43
44
45
46
47
48
49
50
51
0
@@ -41,6 +41,11 @@ end
0
 
0
 class ParanoidTest < Test::Unit::TestCase
0
   fixtures :widgets, :categories, :categories_widgets, :tags, :taggings
0
+
0
+ def test_should_exists_with_deleted
0
+ assert Widget.exists_with_deleted?(2)
0
+ assert !Widget.exists?(2)
0
+ end
0
 
0
   def test_should_count_with_deleted
0
     assert_equal 1, Widget.count

Comments

  • Even with this new version, I still have to manually add the deleted_at field to the scope:

    validates_uniqueness_of :some_field, :scope => [:some_attribute, :deleted_at]

    Otherwise, I get validation errors. I’m thinking that this changeset was meant to fix that, but it doesn’t seem to be the case.

  • I can confirm luigis comment, validation fails for me too.