File tree Expand file tree Collapse file tree 2 files changed +19
-6
lines changed Expand file tree Collapse file tree 2 files changed +19
-6
lines changed Original file line number Diff line number Diff line change @@ -70,13 +70,16 @@ def validate_association(document, attribute)
70
70
# Now, treating the target as an array, look at each element
71
71
# and see if it is valid, but only if it has already been
72
72
# persisted, or changed, and hasn't been flagged for destroy.
73
- list . all? do |value |
73
+ #
74
+ # use map.all? instead of just all?, because all? will do short-circuit
75
+ # evaluation and terminate on the first failed validation.
76
+ list . map do |value |
74
77
if value && !value . flagged_for_destroy? && ( !value . persisted? || value . changed? )
75
78
value . validated? ? true : value . valid?
76
79
else
77
80
true
78
81
end
79
- end
82
+ end . all?
80
83
end
81
84
82
85
document . errors . add ( attribute , :invalid ) unless valid
Original file line number Diff line number Diff line change 38
38
User . new ( name : "test" )
39
39
end
40
40
41
- let ( :description ) do
41
+ let ( :description1 ) do
42
+ Description . new
43
+ end
44
+
45
+ let ( :description2 ) do
42
46
Description . new
43
47
end
44
48
45
49
before do
46
- user . descriptions << description
50
+ user . descriptions << description1
51
+ user . descriptions << description2
52
+ user . valid?
47
53
end
48
54
49
55
it "only validates the parent once" do
50
56
expect ( user ) . to_not be_valid
51
57
end
52
58
53
59
it "adds the errors from the relation" do
54
- user . valid?
55
60
expect ( user . errors [ :descriptions ] ) . to_not be_nil
56
61
end
57
62
63
+ it 'reports all failed validations' do
64
+ errors = user . descriptions . flat_map { |d | d . errors [ :details ] }
65
+ expect ( errors . length ) . to be == 2
66
+ end
67
+
58
68
it "only validates the child once" do
59
- expect ( description ) . to_not be_valid
69
+ expect ( description1 ) . to_not be_valid
60
70
end
61
71
end
62
72
You can’t perform that action at this time.
0 commit comments