Skip to content

Instantly share code, notes, and snippets.

@gfmurphy
Created October 7, 2011 20:33
Show Gist options
  • Save gfmurphy/1271289 to your computer and use it in GitHub Desktop.
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?
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