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

Add new Rails/BeforeDestroy cop

Open osdakira opened this issue 4 years ago • 4 comments

When we use before_destroy, we have to use one of the two solutions.

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. https://guides.rubyonrails.org/active_record_callbacks.html#destroying-an-object

Putting before_destroy before dependent: :destroy can be detected with RuboCop::Cop::Layout::ClassStructure. I added a cop to detect the prepend: true option.

Since there are two solutions, the cop default is disabled. It's the same reason it's not Safe.


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).
  • [x] 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.

osdakira avatar Mar 27 '20 10:03 osdakira

(btw there's a typo in the PR title, "Destory" instead of "Destroy")

andyw8 avatar Mar 27 '20 12:03 andyw8

@andyw8

Thank you for pointing this out! I fixed the typo in the title.

And I'm sorry. Commit messages and branch names also have typos. Can I close this PR and re-create it with the correct branch name?

osdakira avatar Mar 27 '20 23:03 osdakira

I don't care about the branch name. On the other hand, please squash your commits into one. Note that this proposal has not been determined to be appropriate for inclusion in RuboCop Rails yet. Either way, the next RuboCop Rails release will be a bug fix release, so a release including a new cops will come later. Thank you.

koic avatar Mar 28 '20 01:03 koic

Thank you for your comment! All the commits are combined into one commit.

osdakira avatar Mar 28 '20 01:03 osdakira