Created
October 7, 2011 20:33
-
-
Save gfmurphy/1271289 to your computer and use it in GitHub Desktop.
Messy? Can you think of a more clear implementation of soft_destroy_method?
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 SoftDestroy | |
# accepts a method or proc to execute as a soft deletion routine | |
# replaces existing destroy method with new soft destroy | |
def soft_destroy_method(&block) | |
if block_given? | |
self.class_eval do | |
def destroy_with_forgiveness | |
block.call(self) | |
end | |
alias_method_chain :destroy, :forgiveness | |
alias_method :destroy!, :destroy_with_forgiveness | |
end | |
end | |
end | |
end | |
class Example < ActiveRecord::Base | |
extend SoftDestroy | |
soft_destroy_method { |e| e.update_attribute :deleted, true } | |
end | |
Example.find(1).destroy # will set deleted flag on record | |
Example.find(1).destroy! # will destroy record using ActiveRecord method |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment