Skip to content

Commit

Permalink
Added recover_with_associations! (and tests) to recover record + any …
Browse files Browse the repository at this point in the history
…specified paranoid associations

Signed-off-by: rick <technoweenie@gmail.com>
  • Loading branch information
Matt Grayson authored and technoweenie committed Dec 16, 2008
1 parent 1e9144e commit af6ce1e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
9 changes: 9 additions & 0 deletions lib/caboose/acts/paranoid.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@ def recover!
self.deleted_at = nil self.deleted_at = nil
save! save!
end 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 end
end end
Expand Down
18 changes: 18 additions & 0 deletions test/paranoid_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -237,6 +237,24 @@ def test_custom_finder_methods
assert_equal [], w[2].categories.search('c').ids assert_equal [], w[2].categories.search('c').ids
assert_equal [3,4], w[2].categories.search_with_deleted('c').ids assert_equal [3,4], w[2].categories.search_with_deleted('c').ids
end 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 end


class Array class Array
Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml')) config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log") 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") load(File.dirname(__FILE__) + "/schema.rb")


Expand Down

0 comments on commit af6ce1e

Please sign in to comment.