simple_params 1.6.9 → 2.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -92,154 +92,56 @@ describe SimpleParams::Errors do
92
92
  end
93
93
  end
94
94
 
95
- describe "setting and getting errors", setters_getters: true do
96
- it "get returns the errors for the provided key" do
95
+ describe '[]', getter: true do
96
+ it "gets the errors for the provided key" do
97
97
  errors = SimpleParams::Errors.new(self)
98
- errors[:foo] = "omg"
99
- errors.get(:foo).should eq(["omg"])
100
- end
101
-
102
- it "sets the error with the provided key" do
103
- errors = SimpleParams::Errors.new(self)
104
- errors.set(:foo, "omg")
105
- errors.messages.should eq({ foo: "omg" })
106
- end
107
-
108
- it "values returns an array of messages" do
109
- errors = SimpleParams::Errors.new(self)
110
- errors.set(:foo, "omg")
111
- errors.set(:baz, "zomg")
112
- errors.values.should eq(["omg", "zomg"])
113
- end
114
-
115
- it "keys returns the error keys" do
116
- errors = SimpleParams::Errors.new(self)
117
- errors.set(:foo, "omg")
118
- errors.set(:baz, "zomg")
119
- errors.keys.should eq([:foo, :baz])
120
- end
121
-
122
- describe "setting on model" do
123
- it "assign error" do
124
- person = Person.new
125
- person.errors[:name] = 'should not be nil'
126
- person.errors[:name].should eq(["should not be nil"])
127
- end
128
-
129
- it "add an error message on a specific attribute" do
130
- person = Person.new
131
- person.errors.add(:name, "can not be blank")
132
- person.errors[:name].should eq(["can not be blank"])
133
- end
134
-
135
- it "add an error with a symbol" do
136
- person = Person.new
137
- person.errors.add(:name, :blank)
138
- message = person.errors.generate_message(:name, :blank)
139
- person.errors[:name].should eq([message])
140
- end
141
-
142
- it "add an error with a proc" do
143
- person = Person.new
144
- message = Proc.new { "can not be blank" }
145
- person.errors.add(:name, message)
146
- person.errors[:name].should eq(["can not be blank"])
147
- end
148
- end
149
- end
150
-
151
- describe "setting and getting nested error model", nested_model: true do
152
- it "can access error model" do
153
- person = Person.new
154
- dog = person.dog
155
- dog_errors = dog.errors
156
- person.errors[:dog].should eq(dog_errors)
157
- end
158
-
159
- it "can add to nested errors through []" do
160
- person = Person.new
161
- person.errors[:dog] = 'should not be nil'
162
- person.dog.errors[:base].should eq(['should not be nil'])
163
- end
164
-
165
- it "can add to nested errors through add" do
166
- person = Person.new
167
- person.errors.add(:dog, 'should not be nil')
168
- person.dog.errors[:base].should eq(['should not be nil'])
98
+ errors.add(:foo, "omg")
99
+ errors[:foo].should eq(["omg"])
169
100
  end
170
101
 
171
- it "can add multiple errors to nested errors through []" do
102
+ it "gets model with nested attributes" do
172
103
  person = Person.new
173
- person.errors[:dog] = 'should not be nil'
174
- person.errors[:dog] = 'must be cute'
175
- person.dog.errors[:base].should eq(['should not be nil', 'must be cute'])
176
- end
177
-
178
- it "can add multiple errors to nested errors through add" do
179
- person = Person.new
180
- person.errors.add(:dog, 'should not be nil')
181
- person.errors.add(:dog, 'must be cute')
182
- person.dog.errors[:base].should eq(['should not be nil', 'must be cute'])
104
+ person.errors.add(:name, "can not be blank")
105
+ person.errors.add(:name, "can not be nil")
106
+ person.errors[:name].should eq(["can not be blank", "can not be nil"])
183
107
  end
184
108
 
185
- it "can add individual errors to nested attributes through []" do
109
+ it "gets nested model errors" do
186
110
  person = Person.new
187
- person.errors[:dog][:breed] = 'should not be nil'
188
- person.dog.errors[:breed].should eq(['should not be nil'])
111
+ person.dog.errors.add(:breed, "can not be blank")
112
+ person.errors[:dog][:breed].should eq(["can not be blank"])
189
113
  end
190
114
 
191
- it "can add individual errors to nested attributes through add" do
115
+ it "gets nested model errors" do
192
116
  person = Person.new
193
- person.errors[:dog].add(:breed, 'should not be nil')
194
- person.dog.errors[:breed].should eq(['should not be nil'])
117
+ person.cats.first.errors.add(:name, "can not be blank")
118
+ person.errors[:cats][0][:name].should eq(["can not be blank"])
195
119
  end
196
120
  end
197
121
 
198
- describe "setting and getting nested array error model", nested_array: true do
199
- it "can access error model" do
200
- person = Person.new
201
- cats = person.cats
202
- cat_errors = cats.first.errors
203
- person.errors[:cats][0].should eq(cat_errors)
204
- end
205
-
206
- it "can add to nested errors through []" do
207
- person = Person.new
208
- person.errors[:cats].first[:base] = 'should not be nil'
209
- person.errors[:cats].first[:base].should eq(['should not be nil'])
210
- person.cats.first.errors[:base].should eq(['should not be nil'])
211
- end
212
-
213
- it "can add to nested errors through add" do
214
- person = Person.new
215
- person.errors[:cats].first.add(:age, 'should not be nil')
216
- person.cats.first.errors[:age].should eq(['should not be nil'])
217
- end
218
-
219
- it "can add multiple errors to nested errors through []" do
220
- person = Person.new
221
- person.errors[:cats].first[:name] = 'should not be nil'
222
- person.errors[:cats].first[:name] = 'must be cute'
223
- person.cats.first.errors[:name].should eq(['should not be nil', 'must be cute'])
122
+ describe '[]=', setter: true do
123
+ it "sets the errors for the provided key" do
124
+ errors = SimpleParams::Errors.new(self)
125
+ errors[:foo] = "omg"
126
+ errors[:foo].should eq(["omg"])
224
127
  end
225
128
 
226
- it "can add multiple errors to nested errors through add" do
129
+ it "sets model with nested attributes" do
227
130
  person = Person.new
228
- person.errors[:cats].first.add(:name, 'should not be nil')
229
- person.errors[:cats].first.add(:name, 'must be cute')
230
- person.cats.first.errors[:name].should eq(['should not be nil', 'must be cute'])
131
+ person.errors[:name] = "can not be blank"
132
+ person.errors[:name].should eq(["can not be blank"])
231
133
  end
232
134
 
233
- it "can add individual errors to nested attributes through []" do
135
+ it "sets nested model errors" do
234
136
  person = Person.new
235
- person.errors[:cats][0][:age] = 'should not be nil'
236
- person.cats.first.errors[:age].should eq(['should not be nil'])
137
+ person.dog.errors[:breed] = "can not be blank"
138
+ person.errors[:dog][:breed].should eq(["can not be blank"])
237
139
  end
238
140
 
239
- it "can add individual errors to nested attributes through add" do
141
+ it "sets nested model errors" do
240
142
  person = Person.new
241
- person.errors[:cats].first.add(:age, 'should not be nil')
242
- person.cats.first.errors[:age].should eq(['should not be nil'])
143
+ person.cats.first.errors[:name] = "can not be blank"
144
+ person.errors[:cats][0][:name].should eq(["can not be blank"])
243
145
  end
244
146
  end
245
147
 
@@ -292,51 +194,6 @@ describe SimpleParams::Errors do
292
194
  end
293
195
  end
294
196
 
295
- describe "#added?", added: true do
296
- it "added? detects if a specific error was added to the object" do
297
- person = Person.new
298
- person.errors.add(:name, "can not be blank")
299
- person.errors.added?(:name, "can not be blank").should be_truthy
300
- end
301
-
302
- it "added? handles symbol message" do
303
- person = Person.new
304
- person.errors.add(:name, :blank)
305
- person.errors.added?(:name, :blank).should be_truthy
306
- end
307
-
308
- it "added? handles proc messages" do
309
- person = Person.new
310
- message = Proc.new { "can not be blank" }
311
- person.errors.add(:name, message)
312
- person.errors.added?(:name, message).should be_truthy
313
- end
314
-
315
- it "added? defaults message to :invalid" do
316
- person = Person.new
317
- person.errors.add(:name)
318
- person.errors.added?(:name).should be_truthy
319
- end
320
-
321
- it "added? matches the given message when several errors are present for the same attribute" do
322
- person = Person.new
323
- person.errors.add(:name, "can not be blank")
324
- person.errors.add(:name, "is invalid")
325
- person.errors.added?(:name, "can not be blank").should be_truthy
326
- end
327
-
328
- it "added? returns false when no errors are present" do
329
- person = Person.new
330
- person.errors.added?(:name).should_not be_truthy
331
- end
332
-
333
- it "added? returns false when checking a nonexisting error and other errors are present for the given attribute" do
334
- person = Person.new
335
- person.errors.add(:name, "is invalid")
336
- person.errors.added?(:name, "can not be blank").should_not be_truthy
337
- end
338
- end
339
-
340
197
  describe "#size", size: true do
341
198
  it "size calculates the number of error messages" do
342
199
  person = Person.new
@@ -394,7 +251,7 @@ describe SimpleParams::Errors do
394
251
  person = Person.new
395
252
  person.errors.add(:name, "can not be blank")
396
253
  person.dog.errors.add(:breed, "can not be nil")
397
- person.errors.to_hash.should eq({
254
+ person.errors.to_hash.should eq({
398
255
  name: ["can not be blank"],
399
256
  dog: {
400
257
  breed: ["can not be nil"]
@@ -408,7 +265,7 @@ describe SimpleParams::Errors do
408
265
  person.errors.add(:name, "can not be blank")
409
266
  person.dog.errors.add(:base, :invalid)
410
267
  person.dog.errors.add(:breed, "can not be nil")
411
- person.errors.to_hash.should eq({
268
+ person.errors.to_hash.should eq({
412
269
  base: ["is invalid"],
413
270
  name: ["can not be blank"],
414
271
  dog: {
@@ -425,7 +282,7 @@ describe SimpleParams::Errors do
425
282
  person.dog.errors.add(:base, :invalid)
426
283
  person.dog.errors.add(:breed, "can not be nil")
427
284
  person.cats.first.errors.add(:name, "can not be blank")
428
- person.errors.to_hash.should eq({
285
+ person.errors.to_hash.should eq({
429
286
  base: ["is invalid"],
430
287
  name: ["can not be blank"],
431
288
  dog: {
@@ -454,13 +311,13 @@ describe SimpleParams::Errors do
454
311
  person.errors[:name] = 'can not be nil'
455
312
  person.errors[:name].should eq(["can not be nil"])
456
313
  person.errors.as_json(full_messages: true).should eq({ name: ["name can not be nil"] })
457
- end
314
+ end
458
315
 
459
316
  it "handles nested attributes without full_messages" do
460
317
  person = Person.new
461
318
  person.errors[:name] = 'can not be nil'
462
319
  person.dog.errors[:breed] = 'is invalid'
463
- person.errors.as_json.should eq({
320
+ person.errors.as_json.should eq({
464
321
  name: ["can not be nil"],
465
322
  dog: {
466
323
  breed: ["is invalid"]
@@ -472,7 +329,7 @@ describe SimpleParams::Errors do
472
329
  person = Person.new
473
330
  person.errors[:name] = 'can not be nil'
474
331
  person.dog.errors[:breed] = 'is invalid'
475
- person.errors.as_json(full_messages: true).should eq({
332
+ person.errors.as_json(full_messages: true).should eq({
476
333
  name: ["name can not be nil"],
477
334
  dog: {
478
335
  breed: ["breed is invalid"]
@@ -520,63 +377,4 @@ describe SimpleParams::Errors do
520
377
  person.errors.full_message(:name_test, "can not be blank").should eq("name_test can not be blank")
521
378
  end
522
379
  end
523
-
524
- describe "#generate_message", generate_message: true do
525
- it "generate_message works without i18n_scope" do
526
- person = Person.new
527
- Person.should_not respond_to(:i18n_scope)
528
- expect {
529
- person.errors.generate_message(:name, :blank)
530
- }.to_not raise_error
531
- end
532
- end
533
-
534
- describe "#adds_on_empty", add_on_empty: true do
535
- it "add_on_empty generates message" do
536
- person = Person.new
537
- person.errors.should_receive(:generate_message).with(:name, :empty, {})
538
- person.errors.add_on_empty :name
539
- end
540
-
541
- it "add_on_empty generates message for multiple attributes" do
542
- person = Person.new
543
- person.errors.should_receive(:generate_message).with(:name, :empty, {})
544
- person.errors.should_receive(:generate_message).with(:age, :empty, {})
545
- person.errors.add_on_empty [:name, :age]
546
- end
547
-
548
- it "add_on_empty generates message with custom default message" do
549
- person = Person.new
550
- person.errors.should_receive(:generate_message).with(:name, :empty, { message: 'custom' })
551
- person.errors.add_on_empty :name, message: 'custom'
552
- end
553
-
554
- it "add_on_empty generates message with empty string value" do
555
- person = Person.new
556
- person.name = ''
557
- person.errors.should_receive(:generate_message).with(:name, :empty, {})
558
- person.errors.add_on_empty :name
559
- end
560
- end
561
-
562
- describe "#adds_on_blank", add_on_blank: true do
563
- it "add_on_blank generates message" do
564
- person = Person.new
565
- person.errors.should_receive(:generate_message).with(:name, :blank, {})
566
- person.errors.add_on_blank :name
567
- end
568
-
569
- it "add_on_blank generates message for multiple attributes" do
570
- person = Person.new
571
- person.errors.should_receive(:generate_message).with(:name, :blank, {})
572
- person.errors.should_receive(:generate_message).with(:age, :blank, {})
573
- person.errors.add_on_blank [:name, :age]
574
- end
575
-
576
- it "add_on_blank generates message with custom default message" do
577
- person = Person.new
578
- person.errors.should_receive(:generate_message).with(:name, :blank, { message: 'custom' })
579
- person.errors.add_on_blank :name, message: 'custom'
580
- end
581
- end
582
380
  end
@@ -53,59 +53,32 @@ describe SimpleParams::NestedErrors do
53
53
  end
54
54
  end
55
55
 
56
- describe "setting and getting errors", setters_getters: true do
57
- it "get returns the errors for the provided key" do
58
- errors = SimpleParams::Errors.new(self)
59
- errors[:foo] = "omg"
60
- errors.get(:foo).should eq(["omg"])
56
+ describe '[]', getter: true do
57
+ it "gets the errors for the provided key" do
58
+ errors = SimpleParams::NestedErrors.new(self)
59
+ errors.add(:foo, "omg")
60
+ errors[:foo].should eq(["omg"])
61
61
  end
62
62
 
63
- it "sets the error with the provided key" do
64
- errors = SimpleParams::Errors.new(self)
65
- errors.set(:foo, "omg")
66
- errors.messages.should eq({ foo: "omg" })
63
+ it "gets model errors" do
64
+ car = Car.new
65
+ car.errors.add(:make, "can not be blank")
66
+ car.errors.add(:make, "can not be nil")
67
+ car.errors[:make].should eq(["can not be blank", "can not be nil"])
67
68
  end
69
+ end
68
70
 
69
- it "values returns an array of messages" do
71
+ describe '[]=', setter: true do
72
+ it "sets the errors for the provided key" do
70
73
  errors = SimpleParams::Errors.new(self)
71
- errors.set(:foo, "omg")
72
- errors.set(:baz, "zomg")
73
- errors.values.should eq(["omg", "zomg"])
74
+ errors[:foo] = "omg"
75
+ errors[:foo].should eq(["omg"])
74
76
  end
75
77
 
76
- it "keys returns the error keys" do
77
- errors = SimpleParams::Errors.new(self)
78
- errors.set(:foo, "omg")
79
- errors.set(:baz, "zomg")
80
- errors.keys.should eq([:foo, :baz])
81
- end
82
-
83
- describe "setting on model" do
84
- it "assign error" do
85
- car = Car.new
86
- car.errors[:make] = 'should not be nil'
87
- car.errors[:make].should eq(["should not be nil"])
88
- end
89
-
90
- it "add an error message on a specific attribute" do
91
- car = Car.new
92
- car.errors.add(:make, "can not be blank")
93
- car.errors[:make].should eq(["can not be blank"])
94
- end
95
-
96
- it "add an error with a symbol" do
97
- car = Car.new
98
- car.errors.add(:make, :blank)
99
- message = car.errors.generate_message(:make, :blank)
100
- car.errors[:make].should eq([message])
101
- end
102
-
103
- it "add an error with a proc" do
104
- car = Car.new
105
- message = Proc.new { "can not be blank" }
106
- car.errors.add(:make, message)
107
- car.errors[:make].should eq(["can not be blank"])
108
- end
78
+ it "sets model errors" do
79
+ car = Car.new
80
+ car.errors[:make] = "can not be blank"
81
+ car.errors[:make].should eq(["can not be blank"])
109
82
  end
110
83
  end
111
84
 
@@ -136,51 +109,6 @@ describe SimpleParams::NestedErrors do
136
109
  end
137
110
  end
138
111
 
139
- describe "#added?", added: true do
140
- it "added? detects if a specific error was added to the object" do
141
- car = Car.new
142
- car.errors.add(:make, "can not be blank")
143
- car.errors.added?(:make, "can not be blank").should be_truthy
144
- end
145
-
146
- it "added? handles symbol message" do
147
- car = Car.new
148
- car.errors.add(:make, :blank)
149
- car.errors.added?(:make, :blank).should be_truthy
150
- end
151
-
152
- it "added? handles proc messages" do
153
- car = Car.new
154
- message = Proc.new { "can not be blank" }
155
- car.errors.add(:make, message)
156
- car.errors.added?(:make, message).should be_truthy
157
- end
158
-
159
- it "added? defaults message to :invalid" do
160
- car = Car.new
161
- car.errors.add(:make)
162
- car.errors.added?(:make).should be_truthy
163
- end
164
-
165
- it "added? matches the given message when several errors are present for the same attribute" do
166
- car = Car.new
167
- car.errors.add(:make, "can not be blank")
168
- car.errors.add(:make, "is invalid")
169
- car.errors.added?(:make, "can not be blank").should be_truthy
170
- end
171
-
172
- it "added? returns false when no errors are present" do
173
- car = Car.new
174
- car.errors.added?(:make).should_not be_truthy
175
- end
176
-
177
- it "added? returns false when checking a nonexisting error and other errors are present for the given attribute" do
178
- car = Car.new
179
- car.errors.add(:make, "is invalid")
180
- car.errors.added?(:make, "can not be blank").should_not be_truthy
181
- end
182
- end
183
-
184
112
  describe "#size", size: true do
185
113
  it "size calculates the number of error messages" do
186
114
  car = Car.new
@@ -205,7 +133,6 @@ describe SimpleParams::NestedErrors do
205
133
  car.errors.add(:make, "can not be nil")
206
134
  car.errors.to_s.should eq("make can not be blank, make can not be nil")
207
135
  end
208
-
209
136
  end
210
137
 
211
138
  describe "#to_hash", to_hash: true do
@@ -281,53 +208,4 @@ describe SimpleParams::NestedErrors do
281
208
  }.to_not raise_error
282
209
  end
283
210
  end
284
-
285
- describe "#adds_on_empty", add_on_empty: true do
286
- it "add_on_empty generates message" do
287
- car = Car.new
288
- car.errors.should_receive(:generate_message).with(:make, :empty, {})
289
- car.errors.add_on_empty :make
290
- end
291
-
292
- it "add_on_empty generates message for multiple attributes" do
293
- car = Car.new
294
- car.errors.should_receive(:generate_message).with(:make, :empty, {})
295
- car.errors.should_receive(:generate_message).with(:model, :empty, {})
296
- car.errors.add_on_empty [:make, :model]
297
- end
298
-
299
- it "add_on_empty generates message with custom default message" do
300
- car = Car.new
301
- car.errors.should_receive(:generate_message).with(:make, :empty, { message: 'custom' })
302
- car.errors.add_on_empty :make, message: 'custom'
303
- end
304
-
305
- it "add_on_empty generates message with empty string value" do
306
- car = Car.new
307
- car.make = ''
308
- car.errors.should_receive(:generate_message).with(:make, :empty, {})
309
- car.errors.add_on_empty :make
310
- end
311
- end
312
-
313
- describe "#adds_on_blank", add_on_blank: true do
314
- it "add_on_blank generates message" do
315
- car = Car.new
316
- car.errors.should_receive(:generate_message).with(:make, :blank, {})
317
- car.errors.add_on_blank :make
318
- end
319
-
320
- it "add_on_blank generates message for multiple attributes" do
321
- car = Car.new
322
- car.errors.should_receive(:generate_message).with(:make, :blank, {})
323
- car.errors.should_receive(:generate_message).with(:model, :blank, {})
324
- car.errors.add_on_blank [:make, :model]
325
- end
326
-
327
- it "add_on_blank generates message with custom default message" do
328
- car = Car.new
329
- car.errors.should_receive(:generate_message).with(:make, :blank, { message: 'custom' })
330
- car.errors.add_on_blank :make, message: 'custom'
331
- end
332
- end
333
211
  end