Module: Caboose::Acts::Paranoid

Defined in:
lib/caboose/acts/paranoid.rb

Overview

Overrides some basic methods for the current model so that calling #destroy sets a ‘deleted_at’ field to the current timestamp. This assumes the table has a deleted_at date/time field. Most normal model operations will work, but there will be some oddities.

class Widget < ActiveRecord::Base
  acts_as_paranoid
end

Widget.find(:all)
# SELECT * FROM widgets WHERE widgets.deleted_at IS NULL

Widget.find(:first, :conditions => ['title = ?', 'test'], :order => 'title')
# SELECT * FROM widgets WHERE widgets.deleted_at IS NULL AND title = 'test' ORDER BY title LIMIT 1

Widget.find_with_deleted(:all)
# SELECT * FROM widgets

Widget.find(:all, :with_deleted => true)
# SELECT * FROM widgets

Widget.find_with_deleted(1).deleted?
# Returns true if the record was previously destroyed, false if not 

Widget.count
# SELECT COUNT(*) FROM widgets WHERE widgets.deleted_at IS NULL

Widget.count ['title = ?', 'test']
# SELECT COUNT(*) FROM widgets WHERE widgets.deleted_at IS NULL AND title = 'test'

Widget.count_with_deleted
# SELECT COUNT(*) FROM widgets

Widget.delete_all
# UPDATE widgets SET deleted_at = '2005-09-17 17:46:36'

Widget.delete_all!
# DELETE FROM widgets

@widget.destroy
# UPDATE widgets SET deleted_at = '2005-09-17 17:46:36' WHERE id = 1

@widget.destroy!
# DELETE FROM widgets WHERE id = 1

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



47
48
49
# File 'lib/caboose/acts/paranoid.rb', line 47

def self.included(base) # :nodoc:
  base.extend ClassMethods
end