Permalink
Browse files

Fix validation for nothing-but-decimal-mark input

  • Loading branch information...
1 parent 587ca88 commit bb3124cd03ee890ff3d2f4ad53ac57b2c84335ab @soulcutter soulcutter committed Jan 23, 2013
Showing with 13 additions and 6 deletions.
  1. +3 −2 lib/money-rails/active_model/validator.rb
  2. +10 −4 spec/active_record/monetizable_spec.rb
@@ -27,11 +27,12 @@ def validate_each(record, attr, value)
decimal_pieces = raw_value.split(decimal_mark)
- # check for numbers like 12.23.45
- if decimal_pieces.length > 2
+ # check for numbers like '12.23.45' or '....'
+ unless [1, 2].include? decimal_pieces.length
record.errors.add(attr, I18n.t('errors.messages.invalid_currency',
{ :thousands => thousands_separator,
:decimal => decimal_mark }))
+ return
end
pieces = decimal_pieces[0].split(thousands_separator)
@@ -67,7 +67,7 @@ class Sub < Product; end
@product.price = Money.new(320, "USD")
@product.save.should be_true
-
+
@product.sale_price = "12.34"
@product.sale_price_currency_code = 'EUR'
@product.valid?.should be_true
@@ -79,6 +79,12 @@ class Sub < Product; end
@product.errors[:price].first.should match(/Must be a valid/)
end
+ it "fails validation with the proper error message if money value is nothing but periods" do
+ @product.price = "..."
+ @product.save.should be_false
+ @product.errors[:price].first.should match(/Must be a valid/)
+ end
+
it "fails validation with the proper error message if money value has invalid thousands part" do
@product.price = "12,23.24"
@product.save.should be_false
@@ -100,7 +106,7 @@ class Sub < Product; end
Money.default_currency = Money::Currency.find('EUR')
"12.00".to_money.should == Money.new(1200, :eur)
transaction = Transaction.new(amount: "12.00", tax: "13.00")
- transaction.amount_cents.should == 1200
+ transaction.amount_cents.should == 1200
transaction.valid?.should be_true
end
@@ -109,7 +115,7 @@ class Sub < Product; end
Money.default_currency = Money::Currency.find('EUR')
"12,00".to_money.should == Money.new(1200, :eur)
transaction = Transaction.new(amount: "12,00", tax: "13,00")
- transaction.amount_cents.should == 1200
+ transaction.amount_cents.should == 1200
transaction.valid?.should be_true
end
@@ -247,7 +253,7 @@ class Sub < Product; end
end
it "sets field to nil, in nil assignments if allow_nil is set" do
- @product.optional_price = nil
+ @product.optional_price = nil
@product.save.should be_true
@product.optional_price.should be_nil
end

0 comments on commit bb3124c

Please sign in to comment.