17
18
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
|
# File 'lib/amount_field/validations.rb', line 17
def validates_amount_format_of(*attr_names)
configuration = attr_names.
define_special_setter(attr_names)
validates_each(attr_names, configuration) do |record, attr_name, value|
next unless record.instance_variable_defined?("@#{special_method_name(attr_name)}")
original_value = record.instance_variable_get("@#{special_method_name(attr_name)}")
original_value ||= record.send("#{attr_name}_before_type_cast") || value
next if configuration[:allow_nil] and original_value.nil?
next if configuration[:allow_blank] and original_value.blank?
converted_value = convert(original_value, format_configuration(configuration))
if valid_format?(original_value, format_configuration(configuration))
record.send("#{attr_name}=", converted_value)
else
record.send("#{attr_name}=", original_value)
record.errors.add(attr_name,
build_error_message(configuration[:message], {
:value => original_value,
:format_example => valid_format_example(format_configuration(configuration))
})
)
end
end
end
|