Permalink
Browse files

Added recover_with_associations! (and tests) to recover record + any …

…specified paranoid associations

Signed-off-by: rick <technoweenie@gmail.com>
  • Loading branch information...
1 parent 1e9144e commit af6ce1ef95e867c0fe66ac03ee88a83bc097073d Matt Grayson committed with technoweenie Apr 30, 2008
Showing with 28 additions and 1 deletion.
  1. +9 −0 lib/caboose/acts/paranoid.rb
  2. +18 −0 test/paranoid_test.rb
  3. +1 −1 test/test_helper.rb
View
9 lib/caboose/acts/paranoid.rb
@@ -151,6 +151,15 @@ def recover!
self.deleted_at = nil
save!
end
+
+ def recover_with_associations!(*associations)
+ self.recover!
+ associations.to_a.each do |assoc|
+ self.send(assoc).find_with_deleted(:all).each do |a|
+ a.recover! if a.class.paranoid?
+ end
+ end
+ end
end
end
end
View
18 test/paranoid_test.rb
@@ -237,6 +237,24 @@ def test_custom_finder_methods
assert_equal [], w[2].categories.search('c').ids
assert_equal [3,4], w[2].categories.search_with_deleted('c').ids
end
+
+ def test_should_recover_record
+ Widget.find(1).destroy
+ assert_equal true, Widget.find_with_deleted(1).deleted?
+
+ Widget.find_with_deleted(1).recover!
+ assert_equal false, Widget.find(1).deleted?
+ end
+
+ def test_should_recover_record_and_has_many_associations
+ Widget.find(1).destroy
+ assert_equal true, Widget.find_with_deleted(1).deleted?
+ assert_equal true, Category.find_with_deleted(1).deleted?
+
+ Widget.find_with_deleted(1).recover_with_associations!(:categories)
+ assert_equal false, Widget.find(1).deleted?
+ assert_equal false, Category.find(1).deleted?
+ end
end
class Array
View
2 test/test_helper.rb
@@ -7,7 +7,7 @@
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
-ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite'])
+ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite3'])
load(File.dirname(__FILE__) + "/schema.rb")

0 comments on commit af6ce1e

Please sign in to comment.