Module: ConcernsOnRails::Models::SoftDeletable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/concerns_on_rails/models/soft_deletable.rb
Instance Method Summary collapse
- #after_restore ⇒ Object
- #after_soft_delete ⇒ Object
- #before_restore ⇒ Object
-
#before_soft_delete ⇒ Object
Soft delete hooks.
- #deleted? ⇒ Boolean (also: #is_soft_deleted?, #soft_deleted?)
- #is_really_deleted? ⇒ Boolean
-
#really_delete! ⇒ Object
bypasses AR callbacks and validations — use when you want a true hard delete.
- #restore! ⇒ Object
- #soft_delete! ⇒ Object
Instance Method Details
#after_restore ⇒ Object
73 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 73 def after_restore; end |
#after_soft_delete ⇒ Object
71 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 71 def after_soft_delete; end |
#before_restore ⇒ Object
72 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 72 def before_restore; end |
#before_soft_delete ⇒ Object
Soft delete hooks
70 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 70 def before_soft_delete; end |
#deleted? ⇒ Boolean Also known as: is_soft_deleted?, soft_deleted?
115 116 117 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 115 def deleted? self[self.class.soft_delete_field].present? end |
#is_really_deleted? ⇒ Boolean
124 125 126 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 124 def is_really_deleted? !self.class.unscoped.exists?(id) end |
#really_delete! ⇒ Object
bypasses AR callbacks and validations — use when you want a true hard delete
110 111 112 113 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 110 def really_delete! self.class.unscoped.where(self.class.primary_key => id).delete_all freeze end |
#restore! ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 93 def restore! return true unless deleted? result = false transaction do before_restore result = if self.class.soft_delete_touch update(self.class.soft_delete_field => nil) else update_column(self.class.soft_delete_field, nil) end after_restore if result end result end |
#soft_delete! ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 75 def soft_delete! return true if deleted? result = false # Wrap the timestamp change and its hooks in a transaction so a raising # before/after hook rolls the change back instead of leaving a half-applied state. transaction do before_soft_delete result = if self.class.soft_delete_touch update(self.class.soft_delete_field => Time.zone.now) else update_column(self.class.soft_delete_field, Time.zone.now) end after_soft_delete if result end result end |