64
64
RUBY
65
65
end
66
66
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
67
100
end
68
101
69
102
context 'Rails <= 6.0' , :rails60 do
81
114
RUBY
82
115
end
83
116
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
84
141
end
85
142
end
86
143
@@ -240,6 +297,39 @@ def expect_no_corrections_if_model_file(file_path)
240
297
RUBY
241
298
end
242
299
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
243
333
end
244
334
245
335
context 'Rails <= 6.0' , :rails60 do
@@ -250,6 +340,30 @@ def expect_no_corrections_if_model_file(file_path)
250
340
RUBY
251
341
end
252
342
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
253
367
end
254
368
255
369
context 'when calling non-manipulative methods' do
0 commit comments