-
-
Notifications
You must be signed in to change notification settings - Fork 263
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 cop for checking assingments to ignored_columns
#771
Conversation
Oh, interesting. So this approach is to suggest that we always do I think this approach definitely makes the cop super clean, as you just detect for However, I find that it is a bit weird since if you look at just the model itself, you'll have to use In our codebase, we never use |
I'd go with |
For reference, #761 also started as a cop checking for |
Yeah, I'm retracting my #761. Let's use |
Thank you for the opinions! Let us move on with this cop. |
config/default.yml
Outdated
@@ -899,6 +899,12 @@ Rails/ScopeArgs: | |||
Include: | |||
- app/models/**/*.rb | |||
|
|||
Rails/SetIgnoredColumns: | |||
Description: 'Looks for assignments of `ignored_columns` that override previous assignments.' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add StyleGuide
URL?
Description: 'Looks for assignments of `ignored_columns` that override previous assignments.' | |
Description: 'Looks for assignments of `ignored_columns` that override previous assignments.' | |
StyleGuide: 'https://rails.rubystyle.guide/#append-ignored-columns' |
# self.ignored_columns += [:two] | ||
# end | ||
# | ||
class SetIgnoredColumns < Base |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if a name starting with Set
is better for this case.
How about cop name like Rails/AppendIgnoredColumns
or Rails/IgnoredColumnsAssignment
?
class SetIgnoredColumns < Base | |
class AppendIgnoredColumns < Base |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer Rails/IgnoredColumnsAssignment
because it looks like a noun.
self.ignored_columns = [:one] | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This assignment to `ignored_columns` may overwrite previous ones. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you tweak the offense range and message?
self.ignored_columns = [:one] | |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This assignment to `ignored_columns` may overwrite previous ones. | |
self.ignored_columns = [:one] | |
^ Use `+=` instead of `=`. |
d98e857
to
2723be4
Compare
CI seems to have failed because rubocop/rubocop#11002 was merged 5 hours ago. |
228932c
to
4f415c8
Compare
extend AutoCorrector | ||
MSG = 'Use `+=` instead of `=`.' | ||
|
||
RESTRICT_ON_SEND = %i[ignored_columns=].freeze |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's just my two cents. Can you tweak it?
extend AutoCorrector | |
MSG = 'Use `+=` instead of `=`.' | |
RESTRICT_ON_SEND = %i[ignored_columns=].freeze | |
extend AutoCorrector | |
MSG = 'Use `+=` instead of `=`.' | |
RESTRICT_ON_SEND = %i[ignored_columns=].freeze |
lib/rubocop/cop/rails_cops.rb
Outdated
@@ -107,6 +107,7 @@ | |||
require_relative 'rails/save_bang' | |||
require_relative 'rails/schema_comment' | |||
require_relative 'rails/scope_args' | |||
require_relative 'rails/ignored_columns_assignment' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The line that adds this new Cop is wrong so I will also fix it.
Setting `ignored_columns` may overwrite previous assignments, and that is problematic because it can un-ignore a previously set column list. Since it is not a problem to have a duplicate column in the list, simply recommend always appending.
4f415c8
to
816478b
Compare
Thanks! |
@@ -0,0 +1 @@ | |||
* [#771](https://github.com/rubocop/rubocop-rails/pull/771): Add new `Rails/IgnoredColumnsAssignment` cop. ([@kkitadate][]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JFYI, this changelog entry has been updated in the edge because #514 (comment).
This PR is a rebase of #514 on
rubocop:master
.It is requested by #514 (comment).
Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.