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
|
# File 'lib/association_validator.rb', line 2
def validate_each(record, attribute, record_id)
return if record.send(attribute).blank?
if not options[:polymorphic_class].blank?
attribute_string = record.send(options[:polymorphic_class])
return if attribute_string.blank?
elsif options[:class_name].blank?
attribute_string = attribute.to_s
attribute_string[-3,3] = ''
elsif options[:class_name].is_a?(String)
attribute_string = options[:class_name]
elsif options[:class_name].is_a?(Proc)
attribute_string = options[:class_name].yield(record)
return if attribute_string.blank?
else
raise "Association Validator does not support a(n) #{options[:class_name].class.to_s.humanize} as a class_name option"
end
c = attribute_string.camelize.constantize
begin
valid = c.find(value, :select => 'id')
rescue
record.errors[attribute] = (options[:message] || "is a non existent record")
end
end
|