Skip to content

Instantly share code, notes, and snippets.

@atesgoral
Created March 20, 2019 15:43
Show Gist options
  • Save atesgoral/6b8cd83e706357d347991dacedaed1f3 to your computer and use it in GitHub Desktop.
Save atesgoral/6b8cd83e706357d347991dacedaed1f3 to your computer and use it in GitHub Desktop.
module SofterDeleteAllDependents
extend ActiveSupport::Concern
included do
before_destroy :softer_delete_associations, prepend: true
end
private
def softer_delete_associations
associations = self.class.reflect_on_all_associations(:has_many).select { |v| v.options[:dependent] == :delete_all }
associations.each do |association|
if association.klass.included_modules & [SofterDelete::UsingIsDeleted, SofterDelete::UsingIsNotDeleted]
model = association.klass.new
model.send(:prepare_for_soft_delete) do
send(association.name).update_all(model.send(:soft_delete_attributes))
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment