Skip to content
This repository
Browse code

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

…specified paranoid associations

Signed-off-by: rick <technoweenie@gmail.com>
  • Loading branch information...
commit af6ce1ef95e867c0fe66ac03ee88a83bc097073d 1 parent 1e9144e
Matt Grayson authored committed
9 lib/caboose/acts/paranoid.rb
@@ -151,6 +151,15 @@ def recover!
151 151 self.deleted_at = nil
152 152 save!
153 153 end
  154 +
  155 + def recover_with_associations!(*associations)
  156 + self.recover!
  157 + associations.to_a.each do |assoc|
  158 + self.send(assoc).find_with_deleted(:all).each do |a|
  159 + a.recover! if a.class.paranoid?
  160 + end
  161 + end
  162 + end
154 163 end
155 164 end
156 165 end
18 test/paranoid_test.rb
@@ -237,6 +237,24 @@ def test_custom_finder_methods
237 237 assert_equal [], w[2].categories.search('c').ids
238 238 assert_equal [3,4], w[2].categories.search_with_deleted('c').ids
239 239 end
  240 +
  241 + def test_should_recover_record
  242 + Widget.find(1).destroy
  243 + assert_equal true, Widget.find_with_deleted(1).deleted?
  244 +
  245 + Widget.find_with_deleted(1).recover!
  246 + assert_equal false, Widget.find(1).deleted?
  247 + end
  248 +
  249 + def test_should_recover_record_and_has_many_associations
  250 + Widget.find(1).destroy
  251 + assert_equal true, Widget.find_with_deleted(1).deleted?
  252 + assert_equal true, Category.find_with_deleted(1).deleted?
  253 +
  254 + Widget.find_with_deleted(1).recover_with_associations!(:categories)
  255 + assert_equal false, Widget.find(1).deleted?
  256 + assert_equal false, Category.find(1).deleted?
  257 + end
240 258 end
241 259
242 260 class Array
2  test/test_helper.rb
@@ -7,7 +7,7 @@
7 7
8 8 config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
9 9 ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
10   -ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite'])
  10 +ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite3'])
11 11
12 12 load(File.dirname(__FILE__) + "/schema.rb")
13 13

0 comments on commit af6ce1e

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