Skip to content

Commit 4032c62

Browse files
authored
Merge pull request #742 from BrianHawley/fix_rails_deprecated_active_model_errors_methods_20220706_3
Add missing deprecated `errors` methods
2 parents f44d616 + 4d11b85 commit 4032c62

File tree

3 files changed

+120
-4
lines changed

3 files changed

+120
-4
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#742](https://github.com/rubocop/rubocop-rails/pull/742): Rails/DeprecatedActiveModelErrorsMethods was missing the deprecated `values`, `to_h`, and `to_xml` methods. ([@BrianHawley][])

lib/rubocop/cop/rails/deprecated_active_model_errors_methods.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class DeprecatedActiveModelErrorsMethods < Base
3838

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

4243
MANIPULATIVE_METHODS = Set[
4344
*%i[
@@ -55,7 +56,7 @@ class DeprecatedActiveModelErrorsMethods < Base
5556
{
5657
#root_manipulation?
5758
#root_assignment?
58-
#errors_keys?
59+
#errors_deprecated?
5960
#messages_details_manipulation?
6061
#messages_details_assignment?
6162
}
@@ -77,10 +78,10 @@ class DeprecatedActiveModelErrorsMethods < Base
7778
...)
7879
PATTERN
7980

80-
def_node_matcher :errors_keys?, <<~PATTERN
81+
def_node_matcher :errors_deprecated?, <<~PATTERN
8182
(send
8283
(send #receiver_matcher :errors)
83-
:keys)
84+
{:keys :values :to_h :to_xml})
8485
PATTERN
8586

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

107108
def on_send(node)
108109
any_manipulation?(node) do
109-
next if node.method?(:keys) && target_rails_version <= 6.0
110+
next if target_rails_version <= 6.0 && INCOMPATIBLE_METHODS.include?(node.method_name)
110111

111112
add_offense(node) do |corrector|
112113
next if skip_autocorrect?(node)

spec/rubocop/cop/rails/deprecated_active_model_errors_methods_spec.rb

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,39 @@
6464
RUBY
6565
end
6666
end
67+
68+
context 'when using `values` method' do
69+
it 'registers an offense' do
70+
expect_offense(<<~RUBY, file_path)
71+
user.errors.values
72+
^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.
73+
RUBY
74+
75+
expect_no_corrections
76+
end
77+
end
78+
79+
context 'when using `to_h` method' do
80+
it 'registers an offense' do
81+
expect_offense(<<~RUBY, file_path)
82+
user.errors.to_h
83+
^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.
84+
RUBY
85+
86+
expect_no_corrections
87+
end
88+
end
89+
90+
context 'when using `to_xml` method' do
91+
it 'registers an offense' do
92+
expect_offense(<<~RUBY, file_path)
93+
user.errors.to_xml
94+
^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.
95+
RUBY
96+
97+
expect_no_corrections
98+
end
99+
end
67100
end
68101

69102
context 'Rails <= 6.0', :rails60 do
@@ -81,6 +114,30 @@
81114
RUBY
82115
end
83116
end
117+
118+
context 'when using `values` method' do
119+
it 'does not register an offense' do
120+
expect_no_offenses(<<~RUBY, file_path)
121+
user.errors.values
122+
RUBY
123+
end
124+
end
125+
126+
context 'when using `to_h` method' do
127+
it 'does not register an offense' do
128+
expect_no_offenses(<<~RUBY, file_path)
129+
user.errors.to_h
130+
RUBY
131+
end
132+
end
133+
134+
context 'when using `to_xml` method' do
135+
it 'does not register an offense' do
136+
expect_no_offenses(<<~RUBY, file_path)
137+
user.errors.to_xml
138+
RUBY
139+
end
140+
end
84141
end
85142
end
86143

@@ -240,6 +297,39 @@ def expect_no_corrections_if_model_file(file_path)
240297
RUBY
241298
end
242299
end
300+
301+
context 'when using `values` method' do
302+
it 'registers an offense' do
303+
expect_offense_if_model_file(<<~RUBY, file_path)
304+
errors.values
305+
^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.
306+
RUBY
307+
308+
expect_no_corrections_if_model_file(file_path)
309+
end
310+
end
311+
312+
context 'when using `to_h` method' do
313+
it 'registers an offense' do
314+
expect_offense_if_model_file(<<~RUBY, file_path)
315+
errors.to_h
316+
^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.
317+
RUBY
318+
319+
expect_no_corrections_if_model_file(file_path)
320+
end
321+
end
322+
323+
context 'when using `to_xml` method' do
324+
it 'registers an offense' do
325+
expect_offense_if_model_file(<<~RUBY, file_path)
326+
errors.to_xml
327+
^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.
328+
RUBY
329+
330+
expect_no_corrections_if_model_file(file_path)
331+
end
332+
end
243333
end
244334

245335
context 'Rails <= 6.0', :rails60 do
@@ -250,6 +340,30 @@ def expect_no_corrections_if_model_file(file_path)
250340
RUBY
251341
end
252342
end
343+
344+
context 'when using `values` method' do
345+
it 'does not register an offense' do
346+
expect_no_offenses(<<~RUBY, file_path)
347+
user.errors.values
348+
RUBY
349+
end
350+
end
351+
352+
context 'when using `to_h` method' do
353+
it 'does not register an offense' do
354+
expect_no_offenses(<<~RUBY, file_path)
355+
user.errors.to_h
356+
RUBY
357+
end
358+
end
359+
360+
context 'when using `to_xml` method' do
361+
it 'does not register an offense' do
362+
expect_no_offenses(<<~RUBY, file_path)
363+
user.errors.to_xml
364+
RUBY
365+
end
366+
end
253367
end
254368

255369
context 'when calling non-manipulative methods' do

0 commit comments

Comments
 (0)