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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* [#323](https://github.com/rubocop-hq/rubocop-rails/pull/323): Add new `Rails/OrderById` cop. ([@fatkodima][])
* [#274](https://github.com/rubocop-hq/rubocop-rails/pull/274): Add new `Rails/WhereNot` cop. ([@fatkodima][])
* [#311](https://github.com/rubocop-hq/rubocop-rails/issues/311): Make `Rails/HelperInstanceVariable` aware of memoization. ([@koic][])
* [#332](https://github.com/rubocop-hq/rubocop-rails/issues/332): Fix `Rails/ReflectionClassName` cop false negative when relation had a scope parameter. ([@bubaflub][])

### Bug fixes

Expand All @@ -35,7 +36,7 @@

### Changes

* [#301](https://github.com/rubocop-hq/rubocop-rails/issues/301): Set disalbed by default for `Rails/PluckId`. ([@koic][])
* [#301](https://github.com/rubocop-hq/rubocop-rails/issues/301): Set disabled by default for `Rails/PluckId`. ([@koic][])

## 2.7.0 (2020-07-21)

Expand Down Expand Up @@ -271,3 +272,4 @@
[@kunitoo]: https://github.com/kunitoo
[@jaredmoody]: https://github.com/jaredmoody
[@mobilutz]: https://github.com/mobilutz
[@bubaflub]: https://github.com/bubaflub
2 changes: 1 addition & 1 deletion lib/rubocop/cop/rails/reflection_class_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ReflectionClassName < Cop
MSG = 'Use a string value for `class_name`.'

def_node_matcher :association_with_reflection, <<~PATTERN
(send nil? {:has_many :has_one :belongs_to} _
(send nil? {:has_many :has_one :belongs_to} _ _ ?
(hash <$#reflection_class_name ...>)
)
PATTERN
Expand Down
9 changes: 9 additions & 0 deletions spec/rubocop/cop/rails/reflection_class_name_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@
end
end

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.

^^^^^^^^^^^^^^^^^^^ Use a string value for `class_name`.
RUBY
end
end

it 'does not register an offense when using string with interpolation' do
expect_no_offenses(<<~'RUBY')
has_many :accounts, class_name: "#{prefix}Account"
Expand Down