Module: ConcernsOnRails::Models::SoftDeletable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/concerns_on_rails/models/soft_deletable.rb
Defined Under Namespace
Modules: ClassMethods
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
137 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 137 def after_restore; end |
#after_soft_delete ⇒ Object
135 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 135 def after_soft_delete; end |
#before_restore ⇒ Object
136 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 136 def before_restore; end |
#before_soft_delete ⇒ Object
Soft delete hooks
134 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 134 def before_soft_delete; end |
#deleted? ⇒ Boolean Also known as: is_soft_deleted?, soft_deleted?
179 180 181 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 179 def deleted? self[self.class.soft_delete_field].present? end |
#is_really_deleted? ⇒ Boolean
188 189 190 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 188 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
174 175 176 177 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 174 def really_delete! self.class.unscoped.where(self.class.primary_key => id).delete_all freeze end |
#restore! ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 157 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
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 139 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 |