Refactor delete_records method #15099
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
delete_records
had a lot of conditionals and was generally confusing code.This pulls out anything that deals with just
delete_all
into it's own methoddelete_or_nullify_all_records
for any association that has delete_all run on it.This uses a new
delete_count
method that takes a method and a scope and is shared betweendelete_records
anddelete_or_nullify_all_records
. This handles whetherdelete_all
orupdate_all
is run on the scope — count being the numberupdate_counter
is getting passed.The
delete_or_nullify_all_records
method had to be created in hm:t association as well because of how the methods are chained/coupled. I plan to refactor hm:tdelete_records
eventually as well.This has allowed for the removal of using :all symbol to check if we are deleting all records further optimizing the code. I was then able to remove the unoptimized
records = load_target if records == :all
indelete_records
.The results of delete/delete_all/destroy/destroy_all are the same, this change decouples and improves this part of the code.
cc/ @tenderlove