Module: ActsAsParanoid

Defined in:
lib/rails3_acts_as_paranoid.rb

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Instance Method Summary collapse

Instance Method Details

#acts_as_paranoid(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rails3_acts_as_paranoid.rb', line 14

def acts_as_paranoid(options = {})
  raise ArgumentError, "Hash expected, got #{options.class.name}" if not options.is_a?(Hash) and not options.empty?
  
  class_attribute :paranoid_configuration, :paranoid_column_reference
  
  self.paranoid_configuration = { :column => "deleted_at", :column_type => "time", :recover_dependent_associations => true, :dependent_recovery_window => 2.minutes }
  self.paranoid_configuration.merge!({ :deleted_value => "deleted" }) if options[:column_type] == "string"
  self.paranoid_configuration.merge!(options) # user options

  raise ArgumentError, "'time', 'boolean' or 'string' expected for :column_type option, got #{paranoid_configuration[:column_type]}" unless ['time', 'boolean', 'string'].include? paranoid_configuration[:column_type]

  self.paranoid_column_reference = "#{self.table_name}.#{paranoid_configuration[:column]}"
  
  return if paranoid?

  ActiveRecord::Relation.class_eval do
    alias_method :delete_all!, :delete_all
    alias_method :destroy!, :destroy
  end
  
  scope :not_deleted, where("#{paranoid_column_reference} IS ?", nil)
  
  scope :paranoid_deleted_around_time, lambda {|value, window|
    if self.class.respond_to?(:paranoid?) && self.class.paranoid?
      if self.class.paranoid_column_type == 'time' && ![true, false].include?(value)
        self.where("#{self.class.paranoid_column} > ? AND #{self.class.paranoid_column} < ?", (value - window), (value + window))
      else
        self.only_deleted
      end
    end if paranoid_configuration[:column_type] == 'time'
  }
  
  include InstanceMethods
  extend ClassMethods
end

#paranoid?Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/rails3_acts_as_paranoid.rb', line 6

def paranoid?
  self.included_modules.include?(InstanceMethods)
end

#validates_as_paranoidObject



10
11
12
# File 'lib/rails3_acts_as_paranoid.rb', line 10

def validates_as_paranoid
  extend ParanoidValidations::ClassMethods
end