Class: MoneyRails::ActiveModel::MoneyValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/money-rails/active_model/validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attr, value) ⇒ Object



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)

  # If subunit is not set then no need to validate as it is an
  # indicator that no assignment has been done onto the virtual
  # money field.
  subunit_attr = record.class.monetized_attributes[attr.to_sym]
  return unless record.changed_attributes.keys.include? subunit_attr

  # WARNING: Currently this is only defined in ActiveRecord extension!
  before_type_cast = "#{attr}_money_before_type_cast"
  raw_value = record.send(before_type_cast) if record.respond_to?(before_type_cast.to_sym)

  # Skip it if raw_value is already a Money object
  return if raw_value.is_a?(Money) || raw_value.nil?

  # Extracted from activemodel's protected parse_raw_value_as_a_number
  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