Skip to content

Commit

Permalink
Merge pull request #509 from skryukov/fix/reflection-class-name
Browse files Browse the repository at this point in the history
Fix an error for `Rails/ReflectionClassName` when using `class_name: to_s`
  • Loading branch information
koic authored Jun 24, 2021
2 parents b3ce31a + 6e14b78 commit 41597b0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Bug fixes

* [#509](https://github.com/rubocop/rubocop-rails/pull/509): Fix an error for `Rails/ReflectionClassName` when using `class_name: to_s`. ([@skryukov][])
* [#507](https://github.com/rubocop/rubocop-rails/pull/507): Fix an error for `Rails/FindBy` when calling `take` after block. ([@koic][])
* [#504](https://github.com/rubocop/rubocop-rails/issues/504): Fix a false positive for `Rails/FindBy` when receiver is not an Active Record. ([@nvasilevski][])

Expand Down Expand Up @@ -411,3 +412,4 @@
[@jdelStrother]: https://github.com/jdelStrother
[@aesthetikx]: https://github.com/aesthetikx
[@nvasilevski]: https://github.com/nvasilevski
[@skryukov]: https://github.com/skryukov
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 @@ def on_send(node)

def reflection_class_value?(class_value)
if class_value.send_type?
!class_value.method?(:to_s) || class_value.receiver.const_type?
!class_value.method?(:to_s) || class_value.receiver&.const_type?
else
!ALLOWED_REFLECTION_CLASS_TYPES.include?(class_value.type)
end
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 @@ -59,6 +59,12 @@
RUBY
end

it 'does not register an offense when using `class_name: to_s`' do
expect_no_offenses(<<~'RUBY')
has_many :accounts, class_name: to_s
RUBY
end

it 'does not register an offense when using `foreign_key :account_id`' do
expect_no_offenses(<<~RUBY)
has_many :accounts, class_name: 'Account', foreign_key: :account_id
Expand Down

0 comments on commit 41597b0

Please sign in to comment.