Module: IsParanoid

Defined in:
lib/is_paranoid.rb

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Instance Method Summary collapse

Instance Method Details

#is_paranoid(opts = {}) ⇒ Object

Call this in your model to enable all the safety-net goodness

Example:

class Android < ActiveRecord::Base

is_paranoid

end



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/is_paranoid.rb', line 13

def is_paranoid opts = {}
  opts[:field] ||= [:deleted_at, Proc.new{Time.now.utc}, nil]
  class_inheritable_accessor :destroyed_field, :field_destroyed, :field_not_destroyed
  self.destroyed_field, self.field_destroyed, self.field_not_destroyed = opts[:field]

  if self.reflect_on_all_associations.size > 0 && ! opts[:suppress_load_order_warning]
    warn "is_paranoid warning in class #{self}:  You should declare is_paranoid before your associations"
  end

  # This is the real magic. All calls made to this model will append
  # the conditions deleted_at => nil (or whatever your destroyed_field
  # and field_not_destroyed are). All exceptions require using
  # exclusive_scope (see self.delete_all, self.count_with_destroyed,
  # and self.find_with_destroyed defined in the module ClassMethods)
  default_scope :conditions => {destroyed_field => field_not_destroyed}

  extend ClassMethods
  include InstanceMethods
end