Skip to content

Commit

Permalink
Merge pull request #742 from BrianHawley/fix_rails_deprecated_active_…
Browse files Browse the repository at this point in the history
…model_errors_methods_20220706_3

Add missing deprecated `errors` methods
  • Loading branch information
koic committed Jul 13, 2022
2 parents f44d616 + 4d11b85 commit 4032c62
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 4 deletions.
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

0 comments on commit 4032c62

Please sign in to comment.