Module: Discard::Model

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

Overview

Handles soft deletes of records.

Options:

  • :discard_column - The columns used to track soft delete, defaults to ‘:discarded_at`.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#discardBoolean

Discard the record in the database

Returns:

  • (Boolean)

    true if successful, otherwise false



118
119
120
121
122
123
# File 'lib/discard/model.rb', line 118

def discard
  return false if discarded?
  run_callbacks(:discard) do
    update_attribute(self.class.discard_column, Time.current)
  end
end

#discard!Boolean

Discard the record in the database

There’s a series of callbacks associated with #discard!. If the before_discard callback throws :abort the action is cancelled and #discard! raises RecordNotDiscarded.

Returns:

  • (Boolean)

    true if successful

Raises:



133
134
135
# File 'lib/discard/model.rb', line 133

def discard!
  discard || _raise_record_not_discarded
end

#discarded?Boolean

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

Returns:

  • (Boolean)

    true if this record has been discarded, otherwise false



105
106
107
# File 'lib/discard/model.rb', line 105

def discarded?
  self[self.class.discard_column].present?
end

#undiscardBoolean

Undiscard the record in the database

Returns:

  • (Boolean)

    true if successful, otherwise false



140
141
142
143
144
145
# File 'lib/discard/model.rb', line 140

def undiscard
  return false unless discarded?
  run_callbacks(:undiscard) do
    update_attribute(self.class.discard_column, nil)
  end
end

#undiscard!Boolean

Undiscard the record in the database

There’s a series of callbacks associated with #undiscard!. If the before_undiscard callback throws :abort the action is cancelled and #undiscard! raises RecordNotUndiscarded.

Returns:

  • (Boolean)

    true if successful

Raises:



155
156
157
# File 'lib/discard/model.rb', line 155

def undiscard!
  undiscard || _raise_record_not_undiscarded
end

#undiscarded?Boolean Also known as: kept?

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

Returns:

  • (Boolean)

    false if this record has been discarded, otherwise true



110
111
112
# File 'lib/discard/model.rb', line 110

def undiscarded?
  !discarded?
end