-
Notifications
You must be signed in to change notification settings - Fork 7
/
associated_validator.rb
37 lines (36 loc) · 1.24 KB
/
associated_validator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
module Caprese
module Record
# Formats nested errors on associations in a more useful way than Rails alone
#
# @note BEFORE
# POST /posts (with invalid resources) =>
# [
# { "key"=>"invalid", "field"=>"attachment", "message"=>"Attachment is invalid." }
# ]
#
# @note AFTER
# POST /posts (with invalid resources) =>
# [
# { "key"=>"not_found", "field"=>"attachment.file", "message"=>"Could not find a file at ..."}
# ]
class AssociatedValidator < ActiveRecord::Validations::AssociatedValidator
def validate_each(record, attribute, value)
if Current.caprese_style_errors
Array(value).reject { |r| r.marked_for_destruction? || r.valid? }.each do |invalid_record|
invalid_record.errors.to_a.each do |error|
field_name = error.field ? "#{attribute}.#{error.field}" : attribute
record.errors.add(field_name, error.code, { t: error.t }.merge(value: invalid_record))
end
end
else
super
end
end
end
module ClassMethods
def validates_associated(*attr_names)
validates_with Caprese::Record::AssociatedValidator, _merge_attributes(attr_names)
end
end
end
end