Module: Paranoid::ParanoidMethods

Extended by:
ActiveSupport::Concern
Defined in:
lib/paranoid/paranoid_methods.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#destroyObject

Override the default destroy to allow us to soft delete records. This preserves the before_destroy and after_destroy callbacks. Because this is also called internally by Model.destroy_all and the Model.destroy(id), we don’t need to specify those methods separately.



60
61
62
63
64
65
66
67
# File 'lib/paranoid/paranoid_methods.rb', line 60

def destroy
  _run_destroy_callbacks do
    set_destroyed(field_destroyed.respond_to?(:call) ? field_destroyed.call : field_destroyed)
    @destroyed = true
  end

  self
end

#restoreObject

Restores the record



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/paranoid/paranoid_methods.rb', line 42

def restore
  set_destroyed(field_not_destroyed.respond_to?(:call) ? field_not_destroyed.call : field_not_destroyed)
  
  self.class.reflect_on_all_associations.each do |association|
    if association.options[:dependent] == :destroy && association.klass.paranoid?
      restore_related(association.klass, association.foreign_key, association.options[:primary_key] || 'id', association.options) if association.macro.to_s =~ /^has/
    end
  end
  
  @destroyed = false
  self
end