Module: ActsAsParanoid

Defined in:
lib/rails3_acts_as_paranoid.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_paranoid(options = {}) ⇒ Object

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rails3_acts_as_paranoid.rb', line 5

def acts_as_paranoid(options = {})
  raise ArgumentError, "Hash expected, got #{options.class.name}" if not options.is_a?(Hash) and not options.empty?

  configuration = { :column => "deleted_at", :column_type => "time" }
  configuration.update(options) unless options.nil?

  type = case configuration[:column_type]
    when "time" then "Time.now"
    when "boolean" then "true"
    else
      raise ArgumentError, "'time' or 'boolean' expected for :column_type option, got #{configuration[:column_type]}"
  end

  class_eval "    default_scope where(\"\#{self.table_name}.\#{configuration[:column]} IS ?\", nil)\n\n    class << self\n      def with_deleted\n        self.unscoped.where(\"\") #self.unscoped.reload\n      end\n\n      def only_deleted\n        self.unscoped.where(\"\#{self.table_name}.\#{configuration[:column]} IS NOT ?\", nil)\n      end\n\n      def delete_all!(conditions = nil)\n        self.unscoped.delete_all!(conditions)\n      end\n\n      def delete_all(conditions = nil)\n        update_all [\"\#{configuration[:column]} = ?\", \#{type}], conditions\n      end\n    end\n\n    def destroy!\n      before_destroy() if respond_to?(:before_destroy)\n\n      \#{self.name}.delete_all!(:id => self)\n\n      after_destroy() if respond_to?(:after_destroy)\n    end\n\n    def destroy\n      run_callbacks :destroy do\n        if self.\#{configuration[:column]} == nil\n          \#{self.name}.delete_all(:id => self.id)\n        else\n          \#{self.name}.delete_all!(:id => self.id)\n        end\n      end\n    end\n\n    def recover\n      self.update_attribute(:\#{configuration[:column]}, nil)\n    end\n    \n    ActiveRecord::Relation.class_eval do\n      alias_method :delete_all!, :delete_all\n      alias_method :destroy!, :destroy\n    end\n  EOV\nend\n"