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 <<-EOV
    default_scope where("#{self.table_name}.#{configuration[:column]} IS ?", nil)

    class << self
      def with_deleted
        self.unscoped.where("") #self.unscoped.reload
      end

      def only_deleted
        self.unscoped.where("#{self.table_name}.#{configuration[:column]} IS NOT ?", nil)
      end

      def delete_all!(conditions = nil)
        self.unscoped.delete_all!(conditions)
      end

      def delete_all(conditions = nil)
        update_all ["#{configuration[:column]} = ?", #{type}], conditions
      end
    end

    def destroy!
      before_destroy() if respond_to?(:before_destroy)

      #{self.name}.delete_all!(:id => self)

      after_destroy() if respond_to?(:after_destroy)
    end

    def destroy
      run_callbacks :destroy do
        if self.#{configuration[:column]} == nil
          #{self.name}.delete_all(:id => self.id)
        else
          #{self.name}.delete_all!(:id => self.id)
        end
      end
    end

    def recover
      self.update_attribute(:#{configuration[:column]}, nil)
    end
    
    ActiveRecord::Relation.class_eval do
      alias_method :delete_all!, :delete_all
      alias_method :destroy!, :destroy
    end
  EOV
end