Skip to content

Commit 3b03fde

Browse files
committed
Retrive the monetized attribute instead the subunit when before_type_cast is nil
1 parent 2c89bf4 commit 3b03fde

File tree

1 file changed

+1
-10
lines changed

1 file changed

+1
-10
lines changed

Diff for: lib/money-rails/active_model/validator.rb

+1-10
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,11 @@ def decimal_pieces
1212
end
1313

1414
def validate_each(record, attr, _value)
15-
subunit_attr = record.class.monetized_attributes[attr.to_s]
1615
currency = record.public_send("currency_for_#{attr}")
1716

1817
# WARNING: Currently this is only defined in ActiveRecord extension!
1918
before_type_cast = :"#{attr}_money_before_type_cast"
20-
raw_value = record.try(before_type_cast)
21-
22-
# If raw value is nil and changed subunit is nil, then
23-
# nil is a assigned value, else we should treat the
24-
# subunit value as the one assigned.
25-
if raw_value.nil? && record.public_send(subunit_attr)
26-
subunit_value = record.public_send(subunit_attr)
27-
raw_value = subunit_value.to_f / currency.subunit_to_unit
28-
end
19+
raw_value = record.try(before_type_cast) || record.public_send(attr)
2920

3021
return if options[:allow_nil] && raw_value.nil?
3122

0 commit comments

Comments
 (0)