Created
March 20, 2019 15:43
-
-
Save atesgoral/6b8cd83e706357d347991dacedaed1f3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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