Skip to content
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

Add RuboCop cop to enforce no strict loading model or association config #69

Merged

Conversation

dkubb
Copy link
Contributor

@dkubb dkubb commented Jan 18, 2025

dkubb added 3 commits January 17, 2025 14:50
* Add ByDefaultForModels to ensure models do not set
  self.strict_loading_by_default.
* Add ForAssociations to ensure model associations do not set
  :strict_loading option.

The purpose of this cop is to make sure to highlight any places where
we explicitly set strict loading, as opposed to relyong on global
settings. The idea will be to capture all the offenses in a
.rubocop_todo.yml so we can burn down and fix all the cases over time.
@dkubb dkubb force-pushed the dkubb/add/activerecord-strict-loading-cops branch 5 times, most recently from 4e4c855 to 2c2ad8a Compare January 19, 2025 18:48
@@ -8,7 +8,7 @@ jobs:
strategy:
fail-fast: false
matrix:
ruby: [3.0, 3.1, 3.2]
ruby: ['3.0', 3.1, 3.2]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

comment: This fixes a known issue with GitHub Actions.

@dkubb dkubb force-pushed the dkubb/add/activerecord-strict-loading-cops branch 2 times, most recently from 0f03e68 to 766f0db Compare January 19, 2025 19:05
@dkubb dkubb marked this pull request as ready for review January 19, 2025 19:12
@dkubb dkubb force-pushed the dkubb/add/activerecord-strict-loading-cops branch from 766f0db to 2f6406c Compare January 19, 2025 20:06
@dkubb dkubb force-pushed the dkubb/add/activerecord-strict-loading-cops branch 2 times, most recently from b52b739 to 85bf96c Compare January 21, 2025 19:13
expect_offense(<<~RUBY)
class MyModel < ApplicationRecord
has_many :posts, # preserve this comment
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not set `:strict_loading` in ActiveRecord associations.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

comment: This was a bit annoying that I couldn't figure out how to make it also highlight the actual option on the next line. However, I feel it's probably "close enough" that anyone could figure out that the problem is associated with this specific has_many call.

For this cop to work the auto-correct is not strictly required; and I should note the autocorrect does work in this case, the problem is mostly wrt what parts are highlighted, which is a secondary concern.

@rzane
Copy link
Contributor

rzane commented Jan 21, 2025

Should we register these cops in config/default.yml and specify SafeAutoCorrect: false? Performing these autocorrections are very likely to introduce new errors that need to be manually resolved.

@dkubb dkubb force-pushed the dkubb/add/activerecord-strict-loading-cops branch from 85bf96c to 0e4c372 Compare January 21, 2025 19:34
@dkubb
Copy link
Contributor Author

dkubb commented Jan 21, 2025

@rzane

Should we register these cops in config/default.yml and specify SafeAutoCorrect: false?

comment: I saw somewhere that you can define def safe_autocorrect? = false in the cop. I wonder if that would be even better? It does appear as if we have a convention of using SafeAutoCorrect: false so perhaps that is something I should do also/instead?

@dkubb dkubb force-pushed the dkubb/add/activerecord-strict-loading-cops branch from 0e4c372 to 5b8549f Compare January 21, 2025 19:43
@dkubb
Copy link
Contributor Author

dkubb commented Jan 21, 2025

comment: For now I've defined SafeAutoCorrect: false in default/config.yml and also specified it in the cop source, since the default for #safe_autocorrect? is true, which seems like an unsafe default TBH.

@dkubb dkubb requested a review from rzane January 21, 2025 19:46
@dkubb dkubb force-pushed the dkubb/add/activerecord-strict-loading-cops branch from 5b8549f to 83f65df Compare January 24, 2025 21:50
Copy link
Contributor

@rzane rzane left a comment

Choose a reason for hiding this comment

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

domain lgtm

@dkubb dkubb force-pushed the dkubb/add/activerecord-strict-loading-cops branch 2 times, most recently from ba41a48 to ce87479 Compare January 24, 2025 23:54
Copy link
Member

@smudge smudge left a comment

Choose a reason for hiding this comment

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

domain LGTM && platform LGTM - thanks for the README update!

Copy link
Member

@smudge smudge left a comment

Choose a reason for hiding this comment

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

domain LGTM!

@smudge smudge merged commit 99537f8 into Betterment:main Jan 25, 2025
4 checks passed
@dkubb dkubb deleted the dkubb/add/activerecord-strict-loading-cops branch January 25, 2025 00:28
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