Module: SoftTrash::Model

Extended by:
ActiveSupport::Concern
Defined in:
lib/soft_trash/model.rb

Overview

Handles soft deletes of records.

Options:

  • :soft_trash_column - The columns used to track soft delete, defaults to :deleted_at.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#active?Boolean

Returns false if this record has been discarded, otherwise true.

Returns:

  • (Boolean)

    false if this record has been discarded, otherwise true



58
59
60
# File 'lib/soft_trash/model.rb', line 58

def active?
  !trashed?
end

#restoreBoolean

Undiscard the record in the database

Returns:

  • (Boolean)

    true if successful, otherwise false



79
80
81
82
83
84
# File 'lib/soft_trash/model.rb', line 79

def restore
  return false unless trashed?
  run_callbacks(:restore) do
    update_attribute(self.class.soft_trash_column, nil)
  end
end

#restore!Object



87
88
89
# File 'lib/soft_trash/model.rb', line 87

def restore!
  restore || _raise_record_not_restoreed
end

#trashBoolean

Discard the record in the database

Returns:

  • (Boolean)

    true if successful, otherwise false



65
66
67
68
69
70
# File 'lib/soft_trash/model.rb', line 65

def trash
  return false if trashed?
  run_callbacks(:trash) do
    update_attribute(self.class.soft_trash_column, Time.current)
  end
end

#trash!Object



72
73
74
# File 'lib/soft_trash/model.rb', line 72

def trash!
  trash || _raise_record_not_trashed
end

#trashed?Boolean

Returns true if this record has been discarded, otherwise false.

Returns:

  • (Boolean)

    true if this record has been discarded, otherwise false



53
54
55
# File 'lib/soft_trash/model.rb', line 53

def trashed?
  self[self.class.soft_trash_column].present?
end