Skip to content

Refactor delete_records method #15099

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 14, 2014

Conversation

eileencodes
Copy link
Member

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 method delete_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 between delete_records and delete_or_nullify_all_records. This handles whether delete_all or update_all is run on the scope — count being the number update_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:t delete_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 in delete_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

records.each(&:destroy!)
update_counter(-records.length) unless inverse_updates_counter_cache?
update_counter(-count) unless inverse_updates_counter_cache?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any benefit to this change? count isn't used anywhere else. Ditto for L129

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. @eileencodes and I were attempting to pull this call to update_counter as well as the one in the else branch outside the if statement so we pulled out a count variable.

@eileencodes can you change this back to -records.length and I'll merge it in.

Refactor by creating two methods delete_all_records and delete_records
to be called by delete_all and delete (or destroy) respectively.
This reduces the number of conditionals required to handle _how_
records get deleted.

The new delete_count method handles how scope is applied to which
delete action.

A delete_all_records method also has to be called in has_many_through
association because of how the methods are chained. This will be
refactored later on.
Refactor delete_count method to only handle delete_all or nullify/nil cases
and not destroy and switch to if/else rather than case statement. This
refactoring allows removal of :all symbol usage.
Rename delete_all_records because this name better describes
what the method is doing. We can then remove :all from the
hm:t version and pull out the unoptimized call to load_target
in delete_records and pass it directly.
@eileencodes
Copy link
Member Author

@tenderlove @GeekOnCoffee fixed! 😸

this change was unneccsary as nothing was gained from it
@eileencodes
Copy link
Member Author

Missed the comment about L129 and fixed that as well

tenderlove added a commit that referenced this pull request May 14, 2014
@tenderlove tenderlove merged commit dd16a10 into rails:master May 14, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants