Skip to content

Commit

Permalink
[Fix #332] Fix false negative for Rails/ReflectionClassName
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bubaflub committed Aug 21, 2020
1 parent ef26793 commit 20cc151
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
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
^^^^^^^^^^^^^^^^^^^ 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

0 comments on commit 20cc151

Please sign in to comment.