Module: ZombieRecord::Restorable

Extended by:
ActiveSupport::Concern
Defined in:
lib/zombie_record/restorable.rb

Defined Under Namespace

Modules: ClassMethods, WithDeletedAssociationsWrapper Classes: WithDeletedAssociations

Instance Method Summary collapse

Instance Method Details

#deleted?Boolean

Whether the record has been destroyed.

Returns true if the record is deleted, false otherwise.

Returns:

  • (Boolean)

30
31
32
# File 'lib/zombie_record/restorable.rb', line 30

def deleted?
  !deleted_at.nil?
end

#restore!Object

Restores a destroyed record.

Returns nothing.


14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/zombie_record/restorable.rb', line 14

def restore!
  if frozen?
    raise "cannot restore an object that has been destroyed directly; " <<
          "please make sure to load it from the database again."
  end

  run_callbacks :restore do
    update_column(:deleted_at, nil)

    restore_associated_records!
  end
end

#with_deleted_associationsObject

Allows accessing deleted associations from the record.

Example

book = Book.first.with_deleted_associations

# Even deleted chapters are returned!
book.chapters #=> [...]

Returns a wrapped ActiveRecord::Base object.


44
45
46
47
48
49
50
# File 'lib/zombie_record/restorable.rb', line 44

def with_deleted_associations
  if deleted?
    WithDeletedAssociations.new(self)
  else
    self
  end
end