Created
March 13, 2010 20:50
-
-
Save esdras/331538 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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