19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/hanswurst/validator.rb', line 19
def validate_each(record, attribute, values)
return if values.nil?
values = values.values if values.class == Hash
values = [values] unless values.class == Array
valid = true
max = options[:max]
if max && values.size > max
record.errors.add attribute, "must not have more than #{max} entries"
return
end
fkey = options[:fkey] || false
if fkey
values = values.collect do |id|
CouchPotato.database.load_document id
end
end
if klass = options[:class]
klasses = [klass].flatten
if fkey
klasses = klasses.collect{ |k| k.to_s }
values.each do |doc|
valid = false unless doc && doc.hanswurst_roles && doc_is_valid_klass?(doc, klasses)
end
record.errors.add attribute, "must be a doc id of a class #{klasses.join(' or ')}" unless valid
else
values.each do |thing|
valid = false unless is_valid_klass?(klasses, thing)
end
record.errors.add attribute, "must be an instance of #{klasses.join(' or ')}" unless valid
end
elsif role = options[:role]
roles = [role].flatten.collect{ |r| r.to_s }
if fkey
values.each do |doc|
valid = false unless doc && doc.hanswurst_roles && doc_is_valid_role?(doc, roles)
end
record.errors.add attribute, "must be a doc id of a role #{roles.join(' or ')}" unless valid
else
klasses = roles.collect do |role_alias|
klass = Hanswurst.role_class(role_alias.to_sym)
record.errors.add(attribute, "could not find role #{role_alias} with Hanswurst.role_class. Did you register it with Hanswurst.register_role() ?") if klass.nil?
klass
end
values.each do |thing|
valid = false unless is_valid_klass?(klasses, thing)
end
record.errors.add attribute, "must be an instance of #{klasses.join(' or ')}" unless valid
end
end
end
|