-
-
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
Fix a NoMethodError for errors.keys
in a model
#740
Conversation
Fixes for Rails/DeprecatedActiveModelErrorsMethods: - Fixed a NoMethodError on nil for `errors.keys` in a model. - Made the range calculation more exact so the replacement is easier. - Added missing correction assertions to most test cases. - Added missing test cases. - Improved test structure for future changes.
b8afb46
to
aa9f790
Compare
range = offense_range(node, receiver) | ||
replacement = replacement(node, receiver) | ||
|
||
corrector.replace(range, replacement) | ||
end | ||
|
||
def offense_range(node, receiver) | ||
range_between(receiver.receiver.source_range.end_pos, node.source_range.end_pos) | ||
receiver = receiver.receiver while receiver.send_type? && !receiver.method?(:errors) && receiver.receiver |
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.
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 comment
The 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.
@koic as requested. |
Thanks! |
Fixes for Rails/DeprecatedActiveModelErrorsMethods:
errors.keys
in a model.Before submitting the PR make sure the following are checked:
[Fix #issue-number]
(if the related issue exists).master
(if not - rebase it).bundle exec rake default
. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.md
if the new code introduces user-observable changes. See changelog entry format for details.