Created
September 16, 2008 22:51
-
-
Save pjb3/11156 to your computer and use it in GitHub Desktop.
acts_as_paranoid for Rails 2.1
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 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