Skip to content

Instantly share code, notes, and snippets.

@pjb3
Created September 16, 2008 22:51
Show Gist options
  • Save pjb3/11156 to your computer and use it in GitHub Desktop.
Save pjb3/11156 to your computer and use it in GitHub Desktop.
acts_as_paranoid for Rails 2.1
module Acts
module Paranoid
def acts_as_paranoid
named_scope :not_deleted, :conditions => ["(deleted_at < ? OR deleted_at is null)", Time.now]
class << self
alias_method :find_with_deleted, :find
end
alias_method :destroy!, :destroy
extend ClassMethods
include InstanceMethods
end
end
module ClassMethods
def find(*args)
not_deleted.find_with_deleted(*args)
end
end
module InstanceMethods
def destroy
update_attribute(:deleted_at, Time.now)
end
end
end
ActiveRecord::Base.extend(Acts::Paranoid)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment