Skip to content

Commit 9b454fa

Browse files
authored
Merge pull request #721 from koic/fix_an_error_for_rails_deprecated_active_model_errors_methods
[Fix #717] Fix an error for `Rails/DeprecatedActiveModelErrorsMethods`
2 parents ac65854 + 0640cb3 commit 9b454fa

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#717](https://github.com/rubocop/rubocop-rails/issues/717): Fix an error for `Rails/DeprecatedActiveModelErrorsMethods` when root receiver is a variable. ([@koic][])

lib/rubocop/cop/rails/deprecated_active_model_errors_methods.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def on_send(node)
119119
def autocorrect(corrector, node)
120120
receiver = node.receiver
121121

122-
if receiver.receiver.method?(:messages)
122+
if receiver.receiver.send_type? && receiver.receiver.method?(:messages)
123123
corrector.remove(receiver.receiver.loc.dot)
124124
corrector.remove(receiver.receiver.loc.selector)
125125
end

spec/rubocop/cop/rails/deprecated_active_model_errors_methods_spec.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,20 @@
3737
end
3838

3939
context 'when using `keys` method' do
40-
it 'registers and corrects an offense' do
40+
it 'registers and corrects an offense when root receiver is a variable' do
41+
expect_offense(<<~RUBY, file_path)
42+
user = create_user
43+
user.errors.keys
44+
^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.
45+
RUBY
46+
47+
expect_correction(<<~RUBY)
48+
user = create_user
49+
user.errors.attribute_names
50+
RUBY
51+
end
52+
53+
it 'registers and corrects an offense when root receiver is a method' do
4154
expect_offense(<<~RUBY, file_path)
4255
user.errors.keys.include?(:name)
4356
^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.

0 commit comments

Comments
 (0)