2
2
3
3
RSpec . describe RuboCop ::Cop ::Rails ::DeprecatedActiveModelErrorsMethods , :config do
4
4
shared_examples 'errors call with explicit receiver' do
5
- context 'when modifying errors' do
5
+ context 'when accessing errors' do
6
6
it 'registers and corrects an offense' do
7
7
expect_offense ( <<~RUBY , file_path )
8
8
user.errors[:name] << 'msg'
20
20
user.errors[:name] = []
21
21
^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.
22
22
RUBY
23
+
24
+ expect_no_corrections
23
25
end
24
26
end
25
27
36
38
end
37
39
end
38
40
39
- context 'when using `keys` method' do
40
- context 'Rails >= 6.1' , :rails61 do
41
+ context 'Rails >= 6.1' , :rails61 do
42
+ context 'when using `keys` method' do
41
43
it 'registers and corrects an offense when root receiver is a variable' do
42
44
expect_offense ( <<~RUBY , file_path )
43
45
user = create_user
62
64
RUBY
63
65
end
64
66
end
67
+ end
65
68
66
- context 'Rails <= 6.0' , :rails60 do
69
+ context 'Rails <= 6.0' , :rails60 do
70
+ context 'when using `keys` method' do
67
71
it 'does not register an offense when root receiver is a variable' do
68
72
expect_no_offenses ( <<~RUBY , file_path )
69
73
user = create_user
98
102
user.errors.messages[:name] = []
99
103
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.
100
104
RUBY
105
+
106
+ expect_no_corrections
107
+ end
108
+ end
109
+
110
+ context 'when using `clear` method' do
111
+ it 'registers and corrects an offense' do
112
+ expect_offense ( <<~RUBY , file_path )
113
+ user.errors.messages[:name].clear
114
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.
115
+ RUBY
116
+
117
+ expect_correction ( <<~RUBY )
118
+ user.errors.delete(:name)
119
+ RUBY
120
+ end
121
+ end
122
+
123
+ context 'when calling non-manipulative methods' do
124
+ it 'does not register an offense' do
125
+ expect_no_offenses ( <<~RUBY , file_path )
126
+ errors.messages[:name].present?
127
+ RUBY
101
128
end
102
129
end
103
130
end
116
143
user.errors.details[:name] = []
117
144
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.
118
145
RUBY
146
+
147
+ expect_no_corrections
148
+ end
149
+ end
150
+
151
+ context 'when using `clear` method' do
152
+ it 'registers and corrects an offense' do
153
+ expect_offense ( <<~RUBY , file_path )
154
+ user.errors.details[:name].clear
155
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.
156
+ RUBY
157
+
158
+ expect_correction ( <<~RUBY )
159
+ user.errors.delete(:name)
160
+ RUBY
161
+ end
162
+ end
163
+
164
+ context 'when calling non-manipulative methods' do
165
+ it 'does not register an offense' do
166
+ expect_no_offenses ( <<~RUBY , file_path )
167
+ errors.details[:name].present?
168
+ RUBY
119
169
end
120
170
end
121
171
end
@@ -131,12 +181,24 @@ def expect_offense_if_model_file(code, file_path)
131
181
end
132
182
end
133
183
134
- context 'when modifying errors' do
184
+ def expect_correction_if_model_file ( code , file_path )
185
+ expect_correction ( code ) if file_path . include? ( '/models/' )
186
+ end
187
+
188
+ def expect_no_corrections_if_model_file ( file_path )
189
+ expect_no_corrections if file_path . include? ( '/models/' )
190
+ end
191
+
192
+ context 'when accessing errors' do
135
193
it 'registers an offense for model file' do
136
194
expect_offense_if_model_file ( <<~RUBY , file_path )
137
195
errors[:name] << 'msg'
138
196
^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.
139
197
RUBY
198
+
199
+ expect_correction_if_model_file ( <<~RUBY , file_path )
200
+ errors.add(:name, 'msg')
201
+ RUBY
140
202
end
141
203
142
204
context 'when assigning' do
@@ -145,6 +207,46 @@ def expect_offense_if_model_file(code, file_path)
145
207
errors[:name] = []
146
208
^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.
147
209
RUBY
210
+
211
+ expect_no_corrections_if_model_file ( file_path )
212
+ end
213
+ end
214
+
215
+ context 'when using `clear` method' do
216
+ it 'registers and corrects an offense' do
217
+ expect_offense_if_model_file ( <<~RUBY , file_path )
218
+ errors[:name].clear
219
+ ^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.
220
+ RUBY
221
+
222
+ expect_correction_if_model_file ( <<~RUBY , file_path )
223
+ errors.delete(:name)
224
+ RUBY
225
+ end
226
+ end
227
+
228
+ context 'Rails >= 6.1' , :rails61 do
229
+ context 'when using `keys` method' do
230
+ it 'registers and corrects an offense when root receiver is a variable' do
231
+ expect_offense_if_model_file ( <<~RUBY , file_path )
232
+ errors.keys
233
+ ^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.
234
+ RUBY
235
+
236
+ expect_correction_if_model_file ( <<~RUBY , file_path )
237
+ errors.attribute_names
238
+ RUBY
239
+ end
240
+ end
241
+ end
242
+
243
+ context 'Rails <= 6.0' , :rails60 do
244
+ context 'when using `keys` method' do
245
+ it 'does not register an offense' do
246
+ expect_no_offenses ( <<~RUBY , file_path )
247
+ errors.keys
248
+ RUBY
249
+ end
148
250
end
149
251
end
150
252
@@ -163,6 +265,10 @@ def expect_offense_if_model_file(code, file_path)
163
265
errors.messages[:name] << 'msg'
164
266
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.
165
267
RUBY
268
+
269
+ expect_correction_if_model_file ( <<~RUBY , file_path )
270
+ errors.add(:name, 'msg')
271
+ RUBY
166
272
end
167
273
168
274
context 'when assigning' do
@@ -171,6 +277,29 @@ def expect_offense_if_model_file(code, file_path)
171
277
errors.messages[:name] = []
172
278
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.
173
279
RUBY
280
+
281
+ expect_no_corrections_if_model_file ( file_path )
282
+ end
283
+ end
284
+
285
+ context 'when using `clear` method' do
286
+ it 'registers and corrects an offense' do
287
+ expect_offense_if_model_file ( <<~RUBY , file_path )
288
+ errors.messages[:name].clear
289
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.
290
+ RUBY
291
+
292
+ expect_correction_if_model_file ( <<~RUBY , file_path )
293
+ errors.delete(:name)
294
+ RUBY
295
+ end
296
+ end
297
+
298
+ context 'when using `keys` method' do
299
+ it 'does not register an offense' do
300
+ expect_no_offenses ( <<~RUBY , file_path )
301
+ errors.messages[:name].keys
302
+ RUBY
174
303
end
175
304
end
176
305
@@ -197,6 +326,29 @@ def expect_offense_if_model_file(code, file_path)
197
326
errors.details[:name] = []
198
327
^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.
199
328
RUBY
329
+
330
+ expect_no_corrections_if_model_file ( file_path )
331
+ end
332
+ end
333
+
334
+ context 'when using `clear` method' do
335
+ it 'registers and corrects an offense' do
336
+ expect_offense_if_model_file ( <<~RUBY , file_path )
337
+ errors.details[:name].clear
338
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid manipulating ActiveModel errors as hash directly.
339
+ RUBY
340
+
341
+ expect_correction_if_model_file ( <<~RUBY , file_path )
342
+ errors.delete(:name)
343
+ RUBY
344
+ end
345
+ end
346
+
347
+ context 'when using `keys` method' do
348
+ it 'does not register an offense' do
349
+ expect_no_offenses ( <<~RUBY , file_path )
350
+ errors.details[:name].keys
351
+ RUBY
200
352
end
201
353
end
202
354
0 commit comments