Skip to content

Instantly share code, notes, and snippets.

@esdras
Created March 13, 2010 20:50
Show Gist options
  • Save esdras/331538 to your computer and use it in GitHub Desktop.
Save esdras/331538 to your computer and use it in GitHub Desktop.
module Caboose #:nodoc:
module Acts #:nodoc:
module Paranoid
module InstanceMethods #:nodoc:
# by @esdras_mayrink
# 2010-03-13
# recover this record and its children.
def recover_with_all_associations!(recursively = true)
associations = self.class.reflect_on_all_associations(:has_many).map(&:name) + self.class.reflect_on_all_associations(:has_one).map(&:name)
associations.to_a.each do |assoc|
if self.send(assoc).respond_to?(:find_with_deleted)
self.send(assoc).find_with_deleted(:all).each do |a|
if recursively
a.recover_with_all_associations! if a.class.paranoid?
else
a.recover_with_associations!(associations) if a.class.paranoid?
end
end
end
end
self.recover!
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment