Module: Paranoid42::Persistence
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/paranoid42/persistence.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #delete(opts = {}) ⇒ Object
- #destroy(opts = {}) ⇒ Object
- #destroy!(opts = {}) ⇒ Object
- #destroy_row ⇒ Object
- #destroyed? ⇒ Boolean (also: #deleted?)
- #persisted? ⇒ Boolean
- #recover(opts = {}) ⇒ Object
- #recover_associations ⇒ Object
- #update_counter_caches(opts = {}) ⇒ Object
Instance Method Details
#delete(opts = {}) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/paranoid42/persistence.rb', line 29 def delete(opts = {}) with_paranoid(opts) do if !deleted? && persisted? touch(:deleted_at) update_counter_caches({direction: :down}) end self.class.unscoped { super() } if paranoid_force end end |
#destroy(opts = {}) ⇒ Object
5 6 7 |
# File 'lib/paranoid42/persistence.rb', line 5 def destroy(opts = {}) with_paranoid(opts) { super() } end |
#destroy!(opts = {}) ⇒ Object
9 10 11 |
# File 'lib/paranoid42/persistence.rb', line 9 def destroy!(opts = {}) with_paranoid(opts) { super() } end |
#destroy_row ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/paranoid42/persistence.rb', line 79 def destroy_row if paranoid_force self.deleted_at = Time.now super else delete 1 end end |
#destroyed? ⇒ Boolean Also known as: deleted?
69 70 71 |
# File 'lib/paranoid42/persistence.rb', line 69 def destroyed? !deleted_at.nil? end |
#persisted? ⇒ Boolean
73 74 75 |
# File 'lib/paranoid42/persistence.rb', line 73 def persisted? !new_record? end |
#recover(opts = {}) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/paranoid42/persistence.rb', line 39 def recover(opts={}) return if !destroyed? attrs = current_time = current_time_from_proper_timezone changes = {} attrs.each do |column| changes[column.to_s] = write_attribute(column.to_s, current_time) end changes['deleted_at'] = write_attribute('deleted_at', nil) changes[self.class.locking_column] = increment_lock if locking_enabled? @changed_attributes.except!(*changes.keys) primary_key = self.class.primary_key self.class.unscoped.where({ primary_key => self[primary_key] }).update_all(changes) if opts.fetch(:associations) { true } recover_associations end update_counter_caches({direction: :up}) end |
#recover_associations ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/paranoid42/persistence.rb', line 58 def recover_associations self.class.reflect_on_all_associations.each do |a| next unless a.klass.paranoid? if a.collection? send(a.name).recover_all else a.klass.unscoped { send(a.name).try(:recover) } end end end |
#update_counter_caches(opts = {}) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/paranoid42/persistence.rb', line 13 def update_counter_caches(opts = {}) return unless opts[:direction].present? && [:up, :down].include?(opts[:direction]) each_counter_cached_associations do |association| foreign_key = association.reflection.foreign_key.to_sym unless destroyed_by_association && destroyed_by_association.foreign_key.to_sym == foreign_key if send(association.reflection.name) if opts[:direction] == :up association.increment_counters elsif opts[:direction] == :down association.decrement_counters end end end end end |