Skip to content

Commit

Permalink
[Fix rubocop#1179] accept method calls on local variables in Rails/Re…
Browse files Browse the repository at this point in the history
…flectionClassName
  • Loading branch information
exterm authored and Philip Mueller committed Nov 14, 2023
1 parent dc6cebc commit a9eb6ae
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/change_accept_method_calls_on_local_variables.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#1179](https://github.com/rubocop/rubocop-rails/issues/1179): `Rails/ReflectionClassName`: Accept method calls on local variables. ([@exterm][])
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 @@ -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|
Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/rails/reflection_class_name_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a9eb6ae

Please sign in to comment.