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
-
#active? ⇒ Boolean
False if this record has been discarded, otherwise true.
-
#restore ⇒ Boolean
Undiscard the record in the database.
- #restore! ⇒ Object
-
#trash ⇒ Boolean
Discard the record in the database.
- #trash! ⇒ Object
-
#trashed? ⇒ Boolean
True if this record has been discarded, otherwise false.
Instance Method Details
#active? ⇒ Boolean
Returns false if this record has been discarded, otherwise true.
58 59 60 |
# File 'lib/soft_trash/model.rb', line 58 def active? !trashed? end |
#restore ⇒ Boolean
Undiscard the record in the database
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 |
#trash ⇒ Boolean
Discard the record in the database
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.
53 54 55 |
# File 'lib/soft_trash/model.rb', line 53 def trashed? self[self.class.soft_trash_column].present? end |