Skip to content

Commit

Permalink
[Fix #737] Fix a false positive for `Rails/DeprecatedActiveModelError…
Browse files Browse the repository at this point in the history
…sMethods`

Fixes #737.

This commit fixes a false positive for `Rails/DeprecatedActiveModelErrorsMethods`
when using `keys` method with Rails 6.0.
  • Loading branch information
koic committed Jul 2, 2022
1 parent 03b4f2f commit a703aaf
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 20 deletions.
@@ -0,0 +1 @@
* [#737](https://github.com/rubocop/rubocop-rails/issues/737): Fix a false positive for `Rails/DeprecatedActiveModelErrorsMethods` when using `keys` method with Rails 6.0. ([@koic][])
Expand Up @@ -106,6 +106,8 @@ class DeprecatedActiveModelErrorsMethods < Base

def on_send(node)
any_manipulation?(node) do
next if node.method?(:keys) && target_rails_version <= 6.0

add_offense(node) do |corrector|
next unless AUTOCORECTABLE_METHODS.include?(node.method_name)

Expand Down
Expand Up @@ -37,28 +37,45 @@
end

context 'when using `keys` method' do
it 'registers and corrects an offense when root receiver is a variable' do
expect_offense(<<~RUBY, file_path)
user = create_user
user.errors.keys
^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.
RUBY

expect_correction(<<~RUBY)
user = create_user
user.errors.attribute_names
RUBY
context 'Rails >= 6.1', :rails61 do
it 'registers and corrects an offense when root receiver is a variable' do
expect_offense(<<~RUBY, file_path)
user = create_user
user.errors.keys
^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.
RUBY

expect_correction(<<~RUBY)
user = create_user
user.errors.attribute_names
RUBY
end

it 'registers and corrects an offense when root receiver is a method' do
expect_offense(<<~RUBY, file_path)
user.errors.keys.include?(:name)
^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.
RUBY

expect_correction(<<~RUBY)
user.errors.attribute_names.include?(:name)
RUBY
end
end

it 'registers and corrects an offense when root receiver is a method' do
expect_offense(<<~RUBY, file_path)
user.errors.keys.include?(:name)
^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.
RUBY

expect_correction(<<~RUBY)
user.errors.attribute_names.include?(:name)
RUBY
context 'Rails <= 6.0', :rails60 do
it 'does not register an offense when root receiver is a variable' do
expect_no_offenses(<<~RUBY, file_path)
user = create_user
user.errors.keys
RUBY
end

it 'does not register an offense when root receiver is a method' do
expect_no_offenses(<<~RUBY, file_path)
user.errors.keys.include?(:name)
RUBY
end
end
end
end
Expand Down

0 comments on commit a703aaf

Please sign in to comment.