From 20cc15141eddffbeb196727fa0681e9d807f9d4a Mon Sep 17 00:00:00 2001 From: Bob Kuo Date: Fri, 21 Aug 2020 11:47:24 -0500 Subject: [PATCH] [Fix #332] Fix false negative for `Rails/ReflectionClassName` 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. --- CHANGELOG.md | 4 +++- lib/rubocop/cop/rails/reflection_class_name.rb | 2 +- spec/rubocop/cop/rails/reflection_class_name_spec.rb | 9 +++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7429611425..ed4794a4c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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) @@ -271,3 +272,4 @@ [@kunitoo]: https://github.com/kunitoo [@jaredmoody]: https://github.com/jaredmoody [@mobilutz]: https://github.com/mobilutz +[@bubaflub]: https://github.com/bubaflub diff --git a/lib/rubocop/cop/rails/reflection_class_name.rb b/lib/rubocop/cop/rails/reflection_class_name.rb index 28dbfc1dde..336b95fc26 100644 --- a/lib/rubocop/cop/rails/reflection_class_name.rb +++ b/lib/rubocop/cop/rails/reflection_class_name.rb @@ -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 diff --git a/spec/rubocop/cop/rails/reflection_class_name_spec.rb b/spec/rubocop/cop/rails/reflection_class_name_spec.rb index 7db8c3b44c..33898c8178 100644 --- a/spec/rubocop/cop/rails/reflection_class_name_spec.rb +++ b/spec/rubocop/cop/rails/reflection_class_name_spec.rb @@ -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"