Class: ActiveRecord::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/rails4_acts_as_paranoid.rb

Constant Summary collapse

VALUES =
[
  :bind_values,
  :create_with_value,
  :eager_load_values,
  :from_value,
  :group_values,
  :having_values,
  :includes_values,
  :joins_values,
  :limit_value,
  :lock_value,
  :offset_value,
  :order_values,
  :preload_values,
  :readonly_value,
  :reordering_value,
  :reverse_order_value,
  :select_values,
  :uniq_value,
  :where_values
]

Instance Method Summary collapse

Instance Method Details

#arel=(a) ⇒ Object



64
65
66
# File 'lib/rails4_acts_as_paranoid.rb', line 64

def arel=(a)
  @arel = a
end

#delete_all(conditions = nil) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/rails4_acts_as_paranoid.rb', line 56

def delete_all(conditions = nil)
  if paranoid?
    update_all(paranoid_deletion_attributes, conditions)
  else
    delete_all!(conditions)
  end
end

#delete_all!(conditions = nil) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/rails4_acts_as_paranoid.rb', line 47

def delete_all!(conditions = nil)
  if conditions
    # This idea comes out of Rails 3.1 ActiveRecord::Record.delete_all
    where(conditions).delete_all!
  else
    really_delete_all!
  end
end

#destroy(id) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/rails4_acts_as_paranoid.rb', line 37

def destroy(id)
  if paranoid?
    where(id: id).update_all(paranoid_deletion_attributes)
  else
    destroy!(id)
  end
end

#destroy!Object



36
# File 'lib/rails4_acts_as_paranoid.rb', line 36

alias_method :destroy!, :destroy

#paranoid?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/rails4_acts_as_paranoid.rb', line 28

def paranoid?
  klass.try(:paranoid?) ? true : false
end

#paranoid_deletion_attributesObject



32
33
34
# File 'lib/rails4_acts_as_paranoid.rb', line 32

def paranoid_deletion_attributes
  { klass.paranoid_column => klass.delete_now_value }
end

#really_delete_all!Object



45
# File 'lib/rails4_acts_as_paranoid.rb', line 45

alias_method :really_delete_all!, :delete_all

#with_deletedObject



68
69
70
71
72
73
74
75
76
# File 'lib/rails4_acts_as_paranoid.rb', line 68

def with_deleted
  wd = self.clone.unscoped
  VALUES.each do |v|
    if self.respond_to?(v) && wd.respond_to?("#{v}=")
      wd.send("#{v}=", self.send(v))
    end
  end
  wd
end