Module: Shamu::Entities::ActiveRecordSoftDestroy
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/shamu/entities/active_record_soft_destroy.rb
Overview
Add the ability to "soft-delete" a record. Marking it as deleted so it is no longer present in the default scope but without actually removing the record from the database.
Note You must add a column
destroyed_at
to the model.
Attributes collapse
-
#destroyed_at ⇒ DateTime
When the record was destroyed.
Scopes collapse
-
.apply_destroyed_list_scope(criteria, scope) ⇒ Object
Apply list scoping that includes targeting
destroyed
state. -
#destroyed ⇒ ActiveRecord::Relation
Limit the records to those that have been soft destroyed.
-
#except_destroyed ⇒ ActiveRecord::Relation
Limit the records to those that have not been destroyed.
-
#including_destroyed ⇒ ActiveRecord::Relation
Include live and soft destroyed records.
Instance Method Summary collapse
-
#destroy ⇒ Boolean
Mark the record as deleted.
-
#destroy! ⇒ Boolean
Really destroy! the record.
-
#obliterate ⇒ Boolean
Really destroy the record.
-
#obliterate! ⇒ Boolean
Really destroy! the record.
-
#soft_destroyed? ⇒ Boolean
True if the record has been soft destroyed.
-
#undestroy ⇒ Boolean
Mark the record as no longer destroyed.
Instance Attribute Details
#destroyed_at ⇒ DateTime
Returns when the record was destroyed.
|
# File 'lib/shamu/entities/active_record_soft_destroy.rb', line 18
|
Class Method Details
.apply_destroyed_list_scope(criteria, scope) ⇒ Object
Apply list scoping that includes targeting destroyed
state.
44 45 46 47 48 49 50 51 52 |
# File 'lib/shamu/entities/active_record_soft_destroy.rb', line 44 def self.apply_destroyed_list_scope( criteria, scope ) return criteria if scope.destroyed.nil? if scope.destroyed criteria.destroyed else criteria.except_destroyed end end |
Instance Method Details
#destroy ⇒ Boolean
Mark the record as deleted.
62 63 64 65 66 67 68 |
# File 'lib/shamu/entities/active_record_soft_destroy.rb', line 62 def destroy( = nil ) if destroyed_at || ( && [:obliterate] ) super() else update_attribute :destroyed_at, Time.now.utc end end |
#destroy! ⇒ Boolean
Really destroy! the record.
79 80 81 82 83 84 85 |
# File 'lib/shamu/entities/active_record_soft_destroy.rb', line 79 def destroy!( = nil ) if destroyed_at || ( && [:obliterate] ) super() else update_attribute :destroyed_at, Time.now.utc end end |
#destroyed ⇒ ActiveRecord::Relation
Limit the records to those that have been soft destroyed.
30 |
# File 'lib/shamu/entities/active_record_soft_destroy.rb', line 30 scope :destroyed, ->() { unscope( where: :destroyed_at ).where( arel_table[ :destroyed_at ].not_eq( nil ) ) } |
#except_destroyed ⇒ ActiveRecord::Relation
Limit the records to those that have not been destroyed.
34 |
# File 'lib/shamu/entities/active_record_soft_destroy.rb', line 34 scope :except_destroyed, ->() { unscope( where: :destroyed_at ).where( arel_table[ :destroyed_at ].eq( nil ) ) } |
#including_destroyed ⇒ ActiveRecord::Relation
Include live and soft destroyed records.
38 |
# File 'lib/shamu/entities/active_record_soft_destroy.rb', line 38 scope :including_destroyed, ->() { unscope( where: :destroyed_at ) } |
#obliterate ⇒ Boolean
Really destroy the record.
72 73 74 |
# File 'lib/shamu/entities/active_record_soft_destroy.rb', line 72 def obliterate destroy( obliterate: true ) end |
#obliterate! ⇒ Boolean
Really destroy! the record.
89 90 91 |
# File 'lib/shamu/entities/active_record_soft_destroy.rb', line 89 def obliterate! destroy!( obliterate: true ) end |
#soft_destroyed? ⇒ Boolean
Returns true if the record has been soft destroyed.
100 101 102 |
# File 'lib/shamu/entities/active_record_soft_destroy.rb', line 100 def soft_destroyed? !!destroyed_at end |
#undestroy ⇒ Boolean
Mark the record as no longer destroyed.
95 96 97 |
# File 'lib/shamu/entities/active_record_soft_destroy.rb', line 95 def undestroy update_attribute :destroyed_at, nil end |