Skip to content
This repository has been archived by the owner on Jun 30, 2021. It is now read-only.

Commit

Permalink
Considerably simplified logic behind window-based recovery. Removed u…
Browse files Browse the repository at this point in the history
…nnecessary scope.
  • Loading branch information
goncalossilva committed Mar 8, 2012
1 parent 5000cc0 commit 0b565e4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
18 changes: 13 additions & 5 deletions lib/acts_as_paranoid/core.rb
Expand Up @@ -74,7 +74,7 @@ def paranoid_value
def destroy!
with_transaction_returning_status do
run_callbacks :destroy do
act_on_dependent_destroy_associations
destroy_dependent_associations!
self.class.delete_all!(self.class.primary_key.to_sym => self.id)
self.paranoid_value = self.class.delete_now_value
freeze
Expand Down Expand Up @@ -116,28 +116,36 @@ def recover_dependent_associations(window, options)
self.class.dependent_associations.each do |association|
if association.collection? && self.send(association.name).paranoid?
self.send(association.name).unscoped do
self.send(association.name).paranoid_deleted_around_time(paranoid_value, window).each do |object|
only_deleted_inside_window_if_time_paranoid(self.send(association.name), paranoid_value, window).each do |object|
object.recover(options)
end
end
elsif association.klass.paranoid?
if association.macro == :has_one
association.klass.unscoped do
object = association.klass.paranoid_deleted_around_time(paranoid_value, window).send('find_by_'+association.foreign_key, self.id)
object = only_deleted_inside_window_if_time_paranoid(association.klass, paranoid_value, window).send('find_by_'+association.foreign_key, self.id)
object.recover(options) if object
end
else
association.klass.unscoped do
id = self.send(association.foreign_key)
object = association.klass.paranoid_deleted_around_time(paranoid_value, window).find_by_id(id)
object = only_deleted_inside_window_if_time_paranoid(association.klass, paranoid_value, window).find_by_id(id)
object.recover(options) if object
end
end
end
end
end

def act_on_dependent_destroy_associations
def only_deleted_inside_window_if_time_paranoid(klass, value, window)
if klass.paranoid_column_type == 'time'
klass.where("#{klass.paranoid_column} > ? AND #{klass.paranoid_column} < ?", (value - window), (value + window))
else
klass.only_deleted
end
end

def destroy_dependent_associations!
self.class.dependent_associations.each do |association|
if association.collection? && self.send(association.name).paranoid?
association.klass.with_deleted.instance_eval("find_all_by_#{association.foreign_key}(#{self.id.to_json})").each do |object|
Expand Down
16 changes: 3 additions & 13 deletions lib/rails3_acts_as_paranoid.rb
Expand Up @@ -31,22 +31,12 @@ def acts_as_paranoid(options = {})
self.paranoid_column_reference = "#{self.table_name}.#{paranoid_configuration[:column]}"

return if paranoid?

# Magic!
default_scope { where(paranoid_default_scope_sql) }

scope :paranoid_deleted_around_time, lambda {|value, window|
if self.class.respond_to?(:paranoid?) && self.class.paranoid?
if self.class.paranoid_column_type == 'time' && ![true, false].include?(value)
self.where("#{self.class.paranoid_column} > ? AND #{self.class.paranoid_column} < ?", (value - window), (value + window))
else
self.only_deleted
end
end
}

include ActsAsParanoid::Core
include ActsAsParanoid::Associations

# Magic!
default_scope { where(paranoid_default_scope_sql) }
end
end

Expand Down

0 comments on commit 0b565e4

Please sign in to comment.