Skip to content

Commit

Permalink
Fixing cascading recover to recover the main model first and then its…
Browse files Browse the repository at this point in the history
… dependents
  • Loading branch information
Chris Chiu committed Aug 6, 2013
1 parent f550885 commit 60c236e
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/acts_as_paranoid/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,17 @@ def recover(options={})

self.class.transaction do
run_callbacks :recover do
recover_dependent_associations(options[:recovery_window], options) if options[:recursive]
paranoid_original_value = self.paranoid_value

self.paranoid_value = nil
self.save
self.save!

recover_dependent_associations(paranoid_original_value, options[:recovery_window], options) if options[:recursive]
end
end
end

def recover_dependent_associations(window, options)
def recover_dependent_associations(paranoid_original_value, window, options)
self.class.dependent_associations.each do |reflection|
next unless (klass = get_reflection_class(reflection)).paranoid?

Expand All @@ -144,7 +146,7 @@ def recover_dependent_associations(window, options)
# We can only recover by window if both parent and dependant have a
# paranoid column type of :time.
if self.class.paranoid_column_type == :time && klass.paranoid_column_type == :time
scope = scope.merge(klass.deleted_inside_time_window(paranoid_value, window))
scope = scope.merge(reflection.klass.deleted_inside_time_window(paranoid_original_value, window))
end

scope.each do |object|
Expand Down

0 comments on commit 60c236e

Please sign in to comment.