module Paranoid42::Persistence

Public Instance Methods

delete(opts = {}) click to toggle source
Calls superclass method
# 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
deleted?()
Alias for: destroyed?
destroy(opts = {}) click to toggle source
Calls superclass method
# File lib/paranoid42/persistence.rb, line 5
def destroy(opts = {})
  with_paranoid(opts) { super() }
end
destroy!(opts = {}) click to toggle source
Calls superclass method
# File lib/paranoid42/persistence.rb, line 9
def destroy!(opts = {})
  with_paranoid(opts) { super() }
end
destroy_row() click to toggle source
Calls superclass method
# 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?() click to toggle source
# File lib/paranoid42/persistence.rb, line 69
def destroyed?
  !deleted_at.nil?
end
Also aliased as: deleted?
persisted?() click to toggle source
# File lib/paranoid42/persistence.rb, line 73
def persisted?
  !new_record?
end
recover(opts={}) click to toggle source
# File lib/paranoid42/persistence.rb, line 39
def recover(opts={})
  return if !destroyed?
  attrs = timestamp_attributes_for_update_in_model
  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() click to toggle source
# 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 = {}) click to toggle source
# 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