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
|
# File 'lib/money-rails/active_model/validator.rb', line 4
def validate_each(record, attr, value)
subunit_attr = record.class.monetized_attributes[attr.to_sym]
return unless record.changed_attributes.keys.include? subunit_attr
before_type_cast = "#{attr}_money_before_type_cast"
raw_value = record.send(before_type_cast) if record.respond_to?(before_type_cast.to_sym)
return if raw_value.is_a?(Money) || raw_value.nil?
parsed_value = case raw_value
when /\A0[xX]/
nil
else
begin
Kernel.Float(raw_value)
rescue ArgumentError, TypeError
nil
end
end
unless parsed_value
record.errors.add(attr, :not_a_number)
end
end
|