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

Add missing deprecated errors methods #742

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/fix_deprecated_errors_missing_methods.md
@@ -0,0 +1 @@
* [#742](https://github.com/rubocop/rubocop-rails/pull/742): Rails/DeprecatedActiveModelErrorsMethods was missing the deprecated `values`, `to_h`, and `to_xml` methods. ([@BrianHawley][])
Expand Up @@ -38,6 +38,7 @@ class DeprecatedActiveModelErrorsMethods < Base

MSG = 'Avoid manipulating ActiveModel errors as hash directly.'
AUTOCORRECTABLE_METHODS = %i[<< clear keys].freeze
INCOMPATIBLE_METHODS = %i[keys values to_h to_xml].freeze

MANIPULATIVE_METHODS = Set[
*%i[
Expand All @@ -55,7 +56,7 @@ class DeprecatedActiveModelErrorsMethods < Base
{
#root_manipulation?
#root_assignment?
#errors_keys?
#errors_deprecated?
#messages_details_manipulation?
#messages_details_assignment?
}
Expand All @@ -77,10 +78,10 @@ class DeprecatedActiveModelErrorsMethods < Base
...)
PATTERN

def_node_matcher :errors_keys?, <<~PATTERN
def_node_matcher :errors_deprecated?, <<~PATTERN
(send
(send #receiver_matcher :errors)
:keys)
{:keys :values :to_h :to_xml})
PATTERN

def_node_matcher :messages_details_manipulation?, <<~PATTERN
Expand All @@ -106,7 +107,7 @@ class DeprecatedActiveModelErrorsMethods < Base

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

add_offense(node) do |corrector|
next if skip_autocorrect?(node)
Expand Down
114 changes: 114 additions & 0 deletions spec/rubocop/cop/rails/deprecated_active_model_errors_methods_spec.rb
Expand Up @@ -64,6 +64,39 @@
RUBY
end
end

context 'when using `values` method' do
it 'registers an offense' do
expect_offense(<<~RUBY, file_path)
user.errors.values
^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.
RUBY

expect_no_corrections
end
end

context 'when using `to_h` method' do
it 'registers an offense' do
expect_offense(<<~RUBY, file_path)
user.errors.to_h
^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.
RUBY

expect_no_corrections
end
end

context 'when using `to_xml` method' do
it 'registers an offense' do
expect_offense(<<~RUBY, file_path)
user.errors.to_xml
^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.
RUBY

expect_no_corrections
end
end
end

context 'Rails <= 6.0', :rails60 do
Expand All @@ -81,6 +114,30 @@
RUBY
end
end

context 'when using `values` method' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY, file_path)
user.errors.values
RUBY
end
end

context 'when using `to_h` method' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY, file_path)
user.errors.to_h
RUBY
end
end

context 'when using `to_xml` method' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY, file_path)
user.errors.to_xml
RUBY
end
end
end
end

Expand Down Expand Up @@ -240,6 +297,39 @@ def expect_no_corrections_if_model_file(file_path)
RUBY
end
end

context 'when using `values` method' do
it 'registers an offense' do
expect_offense_if_model_file(<<~RUBY, file_path)
errors.values
^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.
RUBY

expect_no_corrections_if_model_file(file_path)
end
end

context 'when using `to_h` method' do
it 'registers an offense' do
expect_offense_if_model_file(<<~RUBY, file_path)
errors.to_h
^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.
RUBY

expect_no_corrections_if_model_file(file_path)
end
end

context 'when using `to_xml` method' do
it 'registers an offense' do
expect_offense_if_model_file(<<~RUBY, file_path)
errors.to_xml
^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.
RUBY

expect_no_corrections_if_model_file(file_path)
end
end
end

context 'Rails <= 6.0', :rails60 do
Expand All @@ -250,6 +340,30 @@ def expect_no_corrections_if_model_file(file_path)
RUBY
end
end

context 'when using `values` method' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY, file_path)
user.errors.values
RUBY
end
end

context 'when using `to_h` method' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY, file_path)
user.errors.to_h
RUBY
end
end

context 'when using `to_xml` method' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY, file_path)
user.errors.to_xml
RUBY
end
end
end

context 'when calling non-manipulative methods' do
Expand Down