Skip to content
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

Conversation

BrianHawley
Copy link
Contributor

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.

Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

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.
@BrianHawley BrianHawley force-pushed the fix_rails_deprecated_active_model_errors_methods_20220706_1 branch from b8afb46 to aa9f790 Compare July 6, 2022 16:57
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
Copy link
Contributor Author

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.
Copy link
Contributor Author

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.

@BrianHawley
Copy link
Contributor Author

@koic as requested.

@koic koic merged commit 77ab1b1 into rubocop:master Jul 8, 2022
@koic
Copy link
Member

koic commented Jul 8, 2022

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants