diff --git a/changelog/change_accept_method_calls_on_local_variables.md b/changelog/change_accept_method_calls_on_local_variables.md new file mode 100644 index 0000000000..f6afaecd35 --- /dev/null +++ b/changelog/change_accept_method_calls_on_local_variables.md @@ -0,0 +1 @@ +* [#1179](https://github.com/rubocop/rubocop-rails/issues/1179): `Rails/ReflectionClassName`: Accept method calls on local variables. ([@exterm][]) diff --git a/lib/rubocop/cop/rails/reflection_class_name.rb b/lib/rubocop/cop/rails/reflection_class_name.rb index d0afa1acb6..9f5637f8e7 100644 --- a/lib/rubocop/cop/rails/reflection_class_name.rb +++ b/lib/rubocop/cop/rails/reflection_class_name.rb @@ -40,7 +40,7 @@ class ReflectionClassName < Base def on_send(node) association_with_reflection(node) do |reflection_class_name| - return if reflection_class_name.value.send_type? && reflection_class_name.value.receiver.nil? + return if reflection_class_name.value.send_type? && !reflection_class_name.value.receiver&.const_type? return if reflection_class_name.value.lvar_type? && str_assigned?(reflection_class_name) add_offense(reflection_class_name.source_range) do |corrector| diff --git a/spec/rubocop/cop/rails/reflection_class_name_spec.rb b/spec/rubocop/cop/rails/reflection_class_name_spec.rb index a1e41ed132..a849cf3d69 100644 --- a/spec/rubocop/cop/rails/reflection_class_name_spec.rb +++ b/spec/rubocop/cop/rails/reflection_class_name_spec.rb @@ -124,6 +124,12 @@ RUBY end + it 'does not register an offense when parameter value is a method call on an object in a variable' do + expect_no_offenses(<<~RUBY) + has_many :accounts, class_name: the_class.name + RUBY + end + context 'Ruby >= 3.1', :ruby31 do it 'registers an offense when shorthand syntax value is a local variable assigned a constant' do expect_offense(<<~RUBY)