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
class Trash | |
include Mongoid::Document | |
field(:type => String) | |
field(:data => Hash) | |
index(:type => 1) | |
def Trash.destroy(document) | |
create( | |
:type => document.class.name, | |
:data => document.as_document | |
) | |
end | |
def Trash.destroy!(document) | |
destroy(document) | |
ensure | |
document.destroy | |
end | |
def undestroy | |
model = eval(type) | |
if model.respond_to?(:undestroy) | |
model.undestroy(data) | |
else | |
doc = model.new | |
data.each do |attr, value| | |
doc.write_attribute(attr, value) | |
end | |
if doc.save | |
destroy | |
doc | |
else | |
false | |
end | |
end | |
end | |
def Trash.undestroy_all(&block) | |
results = [] | |
all.each do |trash| | |
result = trash.undestroy | |
block ? block.call(result) : results.push(result) | |
end | |
block ? nil : results | |
end | |
def Trash.empty! | |
destroy_all | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment