Skip to content

Commit

Permalink
Merge pull request #423 from jhawthorn/paranoia_aliases
Browse files Browse the repository at this point in the history
Add paranoia_destroy and paranoia_delete aliases
  • Loading branch information
jhawthorn authored Nov 3, 2017
2 parents acd43c1 + c63e0ab commit 7c0d4ac
Showing 1 changed file with 15 additions and 27 deletions.
42 changes: 15 additions & 27 deletions lib/paranoia.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ def self.default_sentinel_value

def self.included(klazz)
klazz.extend Query
klazz.extend Callbacks
end

module Query
Expand Down Expand Up @@ -53,31 +52,11 @@ def restore(id_or_ids, opts = {})
end
end

module Callbacks
def self.extended(klazz)
[:restore, :real_destroy].each do |callback_name|
klazz.define_callbacks callback_name

klazz.define_singleton_method("before_#{callback_name}") do |*args, &block|
set_callback(callback_name, :before, *args, &block)
end

klazz.define_singleton_method("around_#{callback_name}") do |*args, &block|
set_callback(callback_name, :around, *args, &block)
end

klazz.define_singleton_method("after_#{callback_name}") do |*args, &block|
set_callback(callback_name, :after, *args, &block)
end
end
end
end

def destroy
def paranoia_destroy
transaction do
run_callbacks(:destroy) do
@_disable_counter_cache = deleted?
result = delete
result = paranoia_delete
next result unless result && ActiveRecord::VERSION::STRING >= '4.2'
each_counter_cached_associations do |association|
foreign_key = association.reflection.foreign_key.to_sym
Expand All @@ -90,8 +69,14 @@ def destroy
end
end
end
alias_method :destroy, :paranoia_destroy

def delete
def paranoia_destroy!
paranoia_destroy ||
raise(ActiveRecord::RecordNotDestroyed.new("Failed to destroy the record", self))
end

def paranoia_delete
raise ActiveRecord::ReadOnlyRecord, "#{self.class} is marked as readonly" if readonly?
if persisted?
# if a transaction exists, add the record so that after_commit
Expand All @@ -103,6 +88,7 @@ def delete
end
self
end
alias_method :delete, :paranoia_delete

def restore!(opts = {})
self.class.transaction do
Expand All @@ -112,7 +98,7 @@ def restore!(opts = {})
# This only happened on Rails versions earlier than 4.1.
noop_if_frozen = ActiveRecord.version < Gem::Version.new("4.1")
if within_recovery_window?(recovery_window_range) && ((noop_if_frozen && !@attributes.frozen?) || !noop_if_frozen)
@_disable_counter_cache = !deleted?
@_disable_counter_cache = !paranoia_destroyed?
write_attribute paranoia_column, paranoia_sentinel_value
update_columns(paranoia_restore_attributes)
each_counter_cached_associations do |association|
Expand Down Expand Up @@ -149,7 +135,7 @@ def paranoia_destroyed?
def really_destroy!
transaction do
run_callbacks(:real_destroy) do
@_disable_counter_cache = deleted?
@_disable_counter_cache = paranoia_destroyed?
dependent_reflections = self.class.reflections.select do |name, reflection|
reflection.options[:dependent] == :destroy
end
Expand Down Expand Up @@ -241,6 +227,8 @@ def restore_associated_records(recovery_window_range = nil)
ActiveSupport.on_load(:active_record) do
class ActiveRecord::Base
def self.acts_as_paranoid(options={})
define_model_callbacks :restore, :real_destroy

alias_method :really_destroyed?, :destroyed?
alias_method :really_delete, :delete
alias_method :destroy_without_paranoia, :destroy
Expand Down Expand Up @@ -317,7 +305,7 @@ class UniquenessValidator < ActiveModel::EachValidator
class AssociationNotSoftDestroyedValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
# if association is soft destroyed, add an error
if value.present? && value.deleted?
if value.present? && value.paranoia_destroyed?
record.errors[attribute] << 'has been soft-deleted'
end
end
Expand Down

0 comments on commit 7c0d4ac

Please sign in to comment.