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