Sha256: 0a9761f032ee1a92ebb4e174d48e1ec2944ac54670a80d008c7e9e3c03c1a46a

Contents?: true

Size: 831 Bytes

Versions: 2

Compression:

Stored size: 831 Bytes

Contents

# Supply validation macros for Mongoid classes that embed StrictMoney::Amount.
class FinancialityValidator < ActiveModel::EachValidator
  def check_validity!
    invalid_keys = options.keys - [:allow_nil, :positive, :not_negative]
    raise "Invalid keys #{invalid_keys.inspect} for financiality validator" if invalid_keys.present?
  end

  def validate_each(*args)
    validate_positive(*args) if options[:positive]
    validate_not_negative(*args) if options[:not_negative]
  end

  private

  def validate_not_negative(record, attribute, value)
    if value.present? && value.negative?
      record.errors.add(attribute, "can't be negative")
    end
  end

  def validate_positive(record, attribute, value)
    if value.present? && !value.positive?
      record.errors.add(attribute, "must be greater than 0")
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
strict_money-0.4.0 lib/strict_money/orms/mongoid/financiality_validator.rb
strict_money-0.3.0 lib/strict_money/orms/mongoid/financiality_validator.rb