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

[Fix #332] Fix false negative for Rails/ReflectionClassName #334

Conversation

bubaflub
Copy link
Contributor

This PR fixes a false negative for Rails/ReflectionClassName when the
relation had a scope parameter. The cop now considers relations with or
without the second optional scope parameter.

Before:

The following relation would not raise an offense even though the class_name parameter is a constant:

belongs_to :account, -> { distinct }, class_name: Account

After:

The relations with an optional second scope parameter like the example above are now considered by the cop.


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.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.
  • 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.

This PR fixes a false negative for `Rails/ReflectionClassName` when the
relation had a scope parameter.  The cop now considers relations with or
without the second optional scope parameter.
@bubaflub
Copy link
Contributor Author

I tried my best to follow CONTRIBUTING.md, the pull request template, existing coding style, and existing git commit message style. If there's anything missing or incorrect please let me know and I'll fix it.

context 'when a relation has a scope parameter' do
it 'registers an offense' do
expect_offense(<<~RUBY)
belongs_to :account, -> { distinct }, class_name: Account
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would you like to see more test cases? If so, any thing in particular you'd like to see? I didn't think it was necessary to test belongs_to vs has_many as that's just part of the matcher syntax but I could write up those test cases if that'd be beneficial.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I think it can be accepted as the minimum case.

@bubaflub
Copy link
Contributor Author

Should I have updated the docs to note that Rails/ReflectionClassName has been updated in the next version?

@koic koic merged commit 630ccf0 into rubocop:master Aug 21, 2020
@koic
Copy link
Member

koic commented Aug 21, 2020

Nice! I think this PR has won the solution to the issue. And I don't think the documentation needs to be updated because the focus of this cop issue is to class_name. Thank you!

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.

2 participants