rubocop-rails
rubocop-rails copied to clipboard
Add new Rails/BeforeDestroy cop
When we use before_destroy
, we have to use one of the two solutions.
before_destroy
callbacks should be placed beforedependent: :destroy
associations (or use theprepend: true
option), to ensure they execute before the records are deleted bydependent: :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.
- I'm using Semantic Commit Messages. If that's a bad thing, I'd like to fix it.
- [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.
(btw there's a typo in the PR title, "Destory" instead of "Destroy")
@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?
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.
Thank you for your comment! All the commits are combined into one commit.