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
58 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 58 def after_restore; end |
#after_soft_delete ⇒ Object
56 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 56 def after_soft_delete; end |
#before_restore ⇒ Object
57 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 57 def before_restore; end |
#before_soft_delete ⇒ Object
Soft delete hooks
55 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 55 def before_soft_delete; end |
#deleted? ⇒ Boolean Also known as: is_soft_deleted?, soft_deleted?
92 93 94 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 92 def deleted? self[self.class.soft_delete_field].present? end |
#is_really_deleted? ⇒ Boolean
101 102 103 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 101 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
87 88 89 90 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 87 def really_delete! self.class.unscoped.where(self.class.primary_key => id).delete_all freeze end |
#restore! ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 73 def restore! return true unless deleted? 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 result end |
#soft_delete! ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 60 def soft_delete! return true if deleted? 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 result end |