Sha256: bbc726b8c757ce3eaaf294652b2d859116e813d6b894eb125561d8322c4653ac

Contents?: true

Size: 972 Bytes

Versions: 1

Compression:

Stored size: 972 Bytes

Contents

require 'active_support/core_ext/array/extract_options'

module EasyRailsMoney
  # TODO: there are a lot of validations here. customizing the messages
  # for all of them seems cumbersome. if needed write your own. or even patch
  # also while calling the individual validators in validates_money
  # only allow_nil is passed around (as it was needed). test for other
  # like if and unless as well
  class MoneyValidator < ActiveModel::EachValidator

    class << self
      attr_writer :currency_list

      def currency_list
        @currency_list ||= Money::Currency.table.keys.map(&:to_s)
      end
    end
    
    def validate_each(record, attribute, value)
      if options[:allow_nil]
        return if value.nil?
      else
        if value.nil?
          record.errors[attribute] << "cannot be nil" 
          return
        end
      end
      
      if value.fractional < 0
        record.errors[attribute] << "cannot be negative"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
easy_rails_money-0.0.9.pre1 lib/easy_rails_money/money_validator.rb