-
-
Notifications
You must be signed in to change notification settings - Fork 263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix a NoMethodError for errors.keys
in a model
#740
Merged
koic
merged 1 commit into
rubocop:master
from
BrianHawley:fix_rails_deprecated_active_model_errors_methods_20220706_1
Jul 8, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#740](https://github.com/rubocop/rubocop-rails/pull/740): Fix a NoMethodError on nil for `errors.keys` in a model in Rails/DeprecatedActiveModelErrorsMethods. ([@BrianHawley][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
|
||
RSpec.describe RuboCop::Cop::Rails::DeprecatedActiveModelErrorsMethods, :config do | ||
shared_examples 'errors call with explicit receiver' do | ||
context 'when modifying errors' do | ||
context 'when accessing errors' do | ||
it 'registers and corrects an offense' do | ||
expect_offense(<<~RUBY, file_path) | ||
user.errors[:name] << 'msg' | ||
|
@@ -20,6 +20,8 @@ | |
user.errors[:name] = [] | ||
^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly. | ||
RUBY | ||
|
||
expect_no_corrections | ||
end | ||
end | ||
|
||
|
@@ -36,8 +38,8 @@ | |
end | ||
end | ||
|
||
context 'when using `keys` method' do | ||
context 'Rails >= 6.1', :rails61 do | ||
context 'Rails >= 6.1', :rails61 do | ||
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 | ||
|
@@ -62,8 +64,10 @@ | |
RUBY | ||
end | ||
end | ||
end | ||
|
||
context 'Rails <= 6.0', :rails60 do | ||
context 'Rails <= 6.0', :rails60 do | ||
context 'when using `keys` method' do | ||
it 'does not register an offense when root receiver is a variable' do | ||
expect_no_offenses(<<~RUBY, file_path) | ||
user = create_user | ||
|
@@ -98,6 +102,29 @@ | |
user.errors.messages[:name] = [] | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly. | ||
RUBY | ||
|
||
expect_no_corrections | ||
end | ||
end | ||
|
||
context 'when using `clear` method' do | ||
it 'registers and corrects an offense' do | ||
expect_offense(<<~RUBY, file_path) | ||
user.errors.messages[:name].clear | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
user.errors.delete(:name) | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when calling non-manipulative methods' do | ||
it 'does not register an offense' do | ||
expect_no_offenses(<<~RUBY, file_path) | ||
errors.messages[:name].present? | ||
RUBY | ||
end | ||
end | ||
end | ||
|
@@ -116,6 +143,29 @@ | |
user.errors.details[:name] = [] | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly. | ||
RUBY | ||
|
||
expect_no_corrections | ||
end | ||
end | ||
|
||
context 'when using `clear` method' do | ||
it 'registers and corrects an offense' do | ||
expect_offense(<<~RUBY, file_path) | ||
user.errors.details[:name].clear | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
user.errors.delete(:name) | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when calling non-manipulative methods' do | ||
it 'does not register an offense' do | ||
expect_no_offenses(<<~RUBY, file_path) | ||
errors.details[:name].present? | ||
RUBY | ||
end | ||
end | ||
end | ||
|
@@ -131,12 +181,24 @@ def expect_offense_if_model_file(code, file_path) | |
end | ||
end | ||
|
||
context 'when modifying errors' do | ||
def expect_correction_if_model_file(code, file_path) | ||
expect_correction(code) if file_path.include?('/models/') | ||
end | ||
|
||
def expect_no_corrections_if_model_file(file_path) | ||
expect_no_corrections if file_path.include?('/models/') | ||
end | ||
|
||
context 'when accessing errors' do | ||
it 'registers an offense for model file' do | ||
expect_offense_if_model_file(<<~RUBY, file_path) | ||
errors[:name] << 'msg' | ||
^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly. | ||
RUBY | ||
|
||
expect_correction_if_model_file(<<~RUBY, file_path) | ||
errors.add(:name, 'msg') | ||
RUBY | ||
end | ||
|
||
context 'when assigning' do | ||
|
@@ -145,6 +207,46 @@ def expect_offense_if_model_file(code, file_path) | |
errors[:name] = [] | ||
^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly. | ||
RUBY | ||
|
||
expect_no_corrections_if_model_file(file_path) | ||
end | ||
end | ||
|
||
context 'when using `clear` method' do | ||
it 'registers and corrects an offense' do | ||
expect_offense_if_model_file(<<~RUBY, file_path) | ||
errors[:name].clear | ||
^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly. | ||
RUBY | ||
|
||
expect_correction_if_model_file(<<~RUBY, file_path) | ||
errors.delete(:name) | ||
RUBY | ||
end | ||
end | ||
|
||
context 'Rails >= 6.1', :rails61 do | ||
context 'when using `keys` method' do | ||
it 'registers and corrects an offense when root receiver is a variable' do | ||
expect_offense_if_model_file(<<~RUBY, file_path) | ||
errors.keys | ||
^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the specific case that caused the NoMethodError for me. I added the rest of the test changes to look for any other such errors. |
||
RUBY | ||
|
||
expect_correction_if_model_file(<<~RUBY, file_path) | ||
errors.attribute_names | ||
RUBY | ||
end | ||
end | ||
end | ||
|
||
context 'Rails <= 6.0', :rails60 do | ||
context 'when using `keys` method' do | ||
it 'does not register an offense' do | ||
expect_no_offenses(<<~RUBY, file_path) | ||
errors.keys | ||
RUBY | ||
end | ||
end | ||
end | ||
|
||
|
@@ -163,6 +265,10 @@ def expect_offense_if_model_file(code, file_path) | |
errors.messages[:name] << 'msg' | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly. | ||
RUBY | ||
|
||
expect_correction_if_model_file(<<~RUBY, file_path) | ||
errors.add(:name, 'msg') | ||
RUBY | ||
end | ||
|
||
context 'when assigning' do | ||
|
@@ -171,6 +277,29 @@ def expect_offense_if_model_file(code, file_path) | |
errors.messages[:name] = [] | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly. | ||
RUBY | ||
|
||
expect_no_corrections_if_model_file(file_path) | ||
end | ||
end | ||
|
||
context 'when using `clear` method' do | ||
it 'registers and corrects an offense' do | ||
expect_offense_if_model_file(<<~RUBY, file_path) | ||
errors.messages[:name].clear | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly. | ||
RUBY | ||
|
||
expect_correction_if_model_file(<<~RUBY, file_path) | ||
errors.delete(:name) | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when using `keys` method' do | ||
it 'does not register an offense' do | ||
expect_no_offenses(<<~RUBY, file_path) | ||
errors.messages[:name].keys | ||
RUBY | ||
end | ||
end | ||
|
||
|
@@ -197,6 +326,29 @@ def expect_offense_if_model_file(code, file_path) | |
errors.details[:name] = [] | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly. | ||
RUBY | ||
|
||
expect_no_corrections_if_model_file(file_path) | ||
end | ||
end | ||
|
||
context 'when using `clear` method' do | ||
it 'registers and corrects an offense' do | ||
expect_offense_if_model_file(<<~RUBY, file_path) | ||
errors.details[:name].clear | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly. | ||
RUBY | ||
|
||
expect_correction_if_model_file(<<~RUBY, file_path) | ||
errors.delete(:name) | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when using `keys` method' do | ||
it 'does not register an offense' do | ||
expect_no_offenses(<<~RUBY, file_path) | ||
errors.details[:name].keys | ||
RUBY | ||
end | ||
end | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically, for the current patterns,
!receiver.method?(:errors)
would be enough, but it seemed like a good idea to do the other checks in case new patterns are added. We don't want an infinite loop or bad method calls.