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 cop for checking assingments to ignored_columns #771

Merged
merged 1 commit into from
Oct 2, 2022

Conversation

kkitadate
Copy link
Contributor

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:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

@koic
Copy link
Member

koic commented Sep 12, 2022

The issue to be resolved conflicts with #761. We might need some time to figure out which one is better.
Anyway, the next release is a bug fix, so we have time.
cc @sikachu

@sikachu
Copy link

sikachu commented Sep 12, 2022

Oh, interesting. So this approach is to suggest that we always do += instead of = altogether.

I think this approach definitely makes the cop super clean, as you just detect for = instead of +=.

However, I find that it is a bit weird since if you look at just the model itself, you'll have to use += even for the first call. You have to have background knowledge of "Rails sets this variable to [], so that's why we can just use +=.

In our codebase, we never use += but we did have a few accidents of calling = a few times. Hence, I'm in favor of the = version since it's more clear without the need of the background knowledge.

@pirj
Copy link
Member

pirj commented Sep 12, 2022

figure out which one is better

I'd go with +=, keeping in mind inheritance and mixins.

@fsateler
Copy link
Contributor

For reference, #761 also started as a cop checking for =. However, due to the impossibility of checking for mixins and inheritance, this cop might not flag invalid behavior, while it is always correct to use +=.

@sikachu
Copy link

sikachu commented Sep 27, 2022

Yeah, I'm retracting my #761. Let's use += since it works better for inheritance and it makes it much easier to auto-fix it. 👍

@koic
Copy link
Member

koic commented Sep 27, 2022

Thank you for the opinions! Let us move on with this cop.

@@ -899,6 +899,12 @@ Rails/ScopeArgs:
Include:
- app/models/**/*.rb

Rails/SetIgnoredColumns:
Description: 'Looks for assignments of `ignored_columns` that override previous assignments.'
Copy link
Member

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?

Suggested change
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
Copy link
Member

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?

Suggested change
class SetIgnoredColumns < Base
class AppendIgnoredColumns < Base

Copy link
Contributor Author

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.

Comment on lines 7 to 8
self.ignored_columns = [:one]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This assignment to `ignored_columns` may overwrite previous ones.
Copy link
Member

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?

Suggested change
self.ignored_columns = [:one]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This assignment to `ignored_columns` may overwrite previous ones.
self.ignored_columns = [:one]
^ Use `+=` instead of `=`.

@kkitadate
Copy link
Contributor Author

kkitadate commented Sep 29, 2022

CI seems to have failed because rubocop/rubocop#11002 was merged 5 hours ago.
#798 will fix this failure.

@kkitadate kkitadate force-pushed the duplicate-ignored-columns branch 2 times, most recently from 228932c to 4f415c8 Compare September 30, 2022 01:16
@kkitadate kkitadate requested a review from koic September 30, 2022 22:53
Comment on lines 37 to 40
extend AutoCorrector
MSG = 'Use `+=` instead of `=`.'

RESTRICT_ON_SEND = %i[ignored_columns=].freeze
Copy link
Member

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?

Suggested change
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

@@ -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'
Copy link
Contributor Author

@kkitadate kkitadate Oct 1, 2022

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.
@kkitadate kkitadate requested a review from koic October 1, 2022 05:14
@koic koic merged commit 43673c1 into rubocop:master Oct 2, 2022
@koic
Copy link
Member

koic commented Oct 2, 2022

Thanks!

@@ -0,0 +1 @@
* [#771](https://github.com/rubocop/rubocop-rails/pull/771): Add new `Rails/IgnoredColumnsAssignment` cop. ([@kkitadate][])
Copy link
Member

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).

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.

None yet

5 participants