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 20
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)
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?
include ActsAsParanoid::Core
default_scope { where(paranoid_default_scope_sql) }
if paranoid_configuration[:column_type] == 'time'
scope :deleted_inside_time_window, lambda {|time, window|
deleted_after_time((time - window)).deleted_before_time((time + window))
}
scope :deleted_after_time, lambda { |time| where("#{paranoid_column} > ?", time) }
scope :deleted_before_time, lambda { |time| where("#{paranoid_column} < ?", time) }
end
end
|