Skip to content

Commit

Permalink
Merge pull request #334 from bubaflub/fix_reflection_class_name_match…
Browse files Browse the repository at this point in the history
…ing_scoped_relations

[Fix #332] Fix false negative for `Rails/ReflectionClassName`
  • Loading branch information
koic committed Aug 21, 2020
2 parents ef26793 + 20cc151 commit 630ccf0
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 630ccf0

Please sign in to comment.