Skip to content

Instantly share code, notes, and snippets.

@taelor
Created March 7, 2013 20:55
Show Gist options
  • Select an option

  • Save taelor/5111717 to your computer and use it in GitHub Desktop.

Select an option

Save taelor/5111717 to your computer and use it in GitHub Desktop.
A fix for polymorpic belongs to dependent => destroy recovery in rails3_acts_as_paranoid
def recover_dependent_associations(window, options)
self.class.dependent_associations.each do |reflection|
the_klass = if reflection.macro == :belongs_to && reflection.options.include?(:polymorphic)
self.send("#{reflection.name}_type").constantize
else
reflection.klass
end
next unless the_klass.paranoid?
scope = the_klass.only_deleted
# Merge in the association's scope
scope = scope.merge(association(reflection.name).association_scope)
# 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 && the_klass.paranoid_column_type == :time
scope = scope.merge(the_klass.deleted_inside_time_window(paranoid_value, window))
end
scope.each do |object|
object.recover(options)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment