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 Rails/UnusedIgnoredColumns cop #494

Merged
merged 2 commits into from
May 31, 2021

Conversation

pocke
Copy link
Contributor

@pocke pocke commented May 23, 2021

This pull request adds a new Rails/UnusedIgnoredColumns cop.
The cop detects columns that ignored_clumns contains but RDBMS doesn't contain.

Background

ignored_columns is necessary to remove a column. For example:

class User < ApplicationRecord
  # Before `remove_column :users, :name`, `ignored_columns` should be specified.
  self.ignored_columns = [:name]
end

The ignored_columns should be removed after the migration, but sometimes we forget to remove the ignored_columns.
You can avoid forgetting to remove it by this cop.


I think this cop doesn't need an entry in the Rails Style Guide because it is just a small bug.


This pull request doesn't contain the autocorrect feature to keep the PR small for the first step. We can implement the autocorrect feature for the next step.


Before submitting the PR make sure the following are checked:

  • 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.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Run bundle exec rake default. It executes all tests and RuboCop for itself, and generates the documentation.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

@pocke pocke force-pushed the Add_Rails_UnusedIgnoredColumns_cop branch from e9c8ae3 to e4ab457 Compare May 23, 2021 05:39
@pocke pocke force-pushed the Add_Rails_UnusedIgnoredColumns_cop branch from e4ab457 to 91da1b4 Compare May 24, 2021 03:22
class UnusedIgnoredColumns < Base
include ActiveRecordHelper

MSG = 'Remove `%<column_name>s` from `ignored_columns` because the column does not exist.'
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 use RESTRICT_ON_SEND = %i[ignored_columns=].freeze?

end
RUBY
end
end
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 the test case?

self.ignored_columns = [:real_name, :account]


MSG = 'Remove `%<column_name>s` from `ignored_columns` because the column does not exist.'

def_node_matcher :ignored_columns?, <<~PATTERN
Copy link
Member

@koic koic May 24, 2021

Choose a reason for hiding this comment

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

? looks unnecessary because a captured value is returned.

Suggested change
def_node_matcher :ignored_columns?, <<~PATTERN
def_node_matcher :ignored_columns, <<~PATTERN

@pocke
Copy link
Contributor Author

pocke commented May 26, 2021

Thanks for your review! I'll check them this weekend

* Use RESTRICT_ON_SEND constant
* Drop `?` for the matcher because it returns non-boolean value
* Add test case
@pocke
Copy link
Contributor Author

pocke commented May 30, 2021

I updated this pull request with your comment. 4cd98d4

Thanks!

@koic koic merged commit 79c83c3 into rubocop:master May 31, 2021
@koic
Copy link
Member

koic commented May 31, 2021

Thanks!

@pocke pocke deleted the Add_Rails_UnusedIgnoredColumns_cop branch May 31, 2021 08:54
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

2 participants