Module: ActiveRecord::Undo::ModelExtension::InstanceMethods

Defined in:
lib/active_record/undo/model_extension.rb

Instance Method Summary collapse

Instance Method Details

#restore!Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/active_record/undo/model_extension.rb', line 55

def restore!
  ensure_undoable_column_exists!
  return false unless soft_deleted?

  log_item = find_latest_undo_log_item

  if log_item
    log_item.undo_log.restore!
  else
    column_name = self.class.undoable_column
    update_columns(column_name => nil, updated_at: Time.current)
    true
  end
end

#soft_delete!Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/active_record/undo/model_extension.rb', line 39

def soft_delete!
  ensure_undoable_column_exists!
  return false if soft_deleted?

  undo_log = nil
  timestamp = Time.current

  transaction do
    undo_log = ActiveRecord::Undo::UndoLog.create!
    soft_delete_cascade_internal!(timestamp, undo_log)
    undo_log.save!
  end

  undo_log
end

#soft_deleted?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/active_record/undo/model_extension.rb', line 26

def soft_deleted?
  public_send(self.class.undoable_column).present?
end

#undoable?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
# File 'lib/active_record/undo/model_extension.rb', line 30

def undoable?
  return false unless soft_deleted?

  ActiveRecord::Undo::UndoLogItem.joins(:undo_log).exists?(
    item_type: self.class.name,
    item_id: id
  )
end