Module: ConcernsOnRails::Models::SoftDeletable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/concerns_on_rails/models/soft_deletable.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- SCOPE_BASES =
%i[active without_deleted soft_deleted only_deleted with_deleted deleted_within].freeze
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
170 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 170 def after_restore; end |
#after_soft_delete ⇒ Object
168 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 168 def after_soft_delete; end |
#before_restore ⇒ Object
169 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 169 def before_restore; end |
#before_soft_delete ⇒ Object
Soft delete hooks
167 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 167 def before_soft_delete; end |
#deleted? ⇒ Boolean Also known as: is_soft_deleted?, soft_deleted?
212 213 214 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 212 def deleted? self[self.class.soft_delete_field].present? end |
#is_really_deleted? ⇒ Boolean
221 222 223 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 221 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
207 208 209 210 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 207 def really_delete! self.class.unscoped.where(self.class.primary_key => id).delete_all freeze end |
#restore! ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 190 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
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/concerns_on_rails/models/soft_deletable.rb', line 172 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 |