rubocop-rails icon indicating copy to clipboard operation
rubocop-rails copied to clipboard

Rails/BeforeDestroy cop

Open ecbrodie opened this issue 4 years ago • 2 comments

This PR introduces the Rails/BeforeDestroy cop.

The new cop addresses a warning in the Rails ActiveRecord Callbacks documentation for destroying an object. Specifically:

before_destroy callbacks should be placed before dependent: :destroy associations (or use the prepend: true option), to ensure they execute before the records are deleted by dependent: :destroy.

It is my first contribution to the rubocop-rails project, I hope it was a good one ;). There may be some areas of improvement and I openly welcome feedback. Some thoughts that I have about my PR:

  • I thought it was more performant to check for before_destroy send nodes at the start of the #on_send method instead of ActiveRecord callbacks, since before_destroy is less common. I could conceivably reverse how I searched for nodes.
  • I had trouble finding other cops as examples for comparing the line numbers of two different nodes. I hope the approach I took was acceptable, where on a before_destroy node match, I then traversed up to the class node to then search for the association.
  • I debated limiting this cop to ActiveRecord models, similar to the ActiveRecordOverride cop. However, I leaned away from it because I can see this cop being appropriate to mix-ins as well.
  • I know that I have to squash before this can be merged, but I was hoping to receive feedback on my PR first.
  • It should be simple to make this new cop auto-correct-able. My thought is that the auto-correction would just add the prepend: true option to the before_destroy calls. However, I'd rather implement that in a separate PR.

I hope this PR helps the rubocop-rails project. Thank you! :D


Before submitting the PR make sure the following are checked:

  • [x] Wrote good commit messages.
  • [x] Commit message starts with [Fix #issue-number] (if the related issue exists).
  • [x] Feature branch is up-to-date with master (if not - rebase it).
  • [ ] Squashed related commits together.
  • [x] Added tests.
  • [x] Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • [x] The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • [x] Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.

ecbrodie avatar May 16 '20 02:05 ecbrodie

Hi.

So, I just realized now that another PR had already existed with similar functionality: https://github.com/rubocop-hq/rubocop-rails/pull/220. Oops. I did not mean to step on the other PR author's feet with mine.

However, there is not a pure overlap in functionality between my PR and #220, as different approaches were taken in how we enforce the rule. #220 enforces that prepend: true must always be used. Whereas, this PR allows before_destroy without prepend: true to be placed before dependent: :destroy associations (which is the solution my team at work had chosen when we ran into this issue).

I'm happy to work together with the author of #220 to merge our functionality together. I also understand if one PR is selected over the other.

ecbrodie avatar May 16 '20 14:05 ecbrodie

May I request a review please? Attempting to poke primary maintainers @koic @andyw8. It's been 4 days, so I figured it's appropriate to make a gentle poke :)

Thank you.

ecbrodie avatar May 20 '20 01:05 ecbrodie