Permalink
Browse files

Add ability to use numericality validations on monetize

  • Loading branch information...
1 parent bb3124c commit 8ad4b3a262d99fba59cf52531e95fc777463b5d8 @soulcutter soulcutter committed Jan 24, 2013
@@ -23,9 +23,10 @@ def validate_each(record, attr, value)
thousands_separator = I18n.t('number.currency.format.delimiter', default: currency.thousands_separator)
symbol = I18n.t('number.currency.format.unit', default: currency.symbol)
- raw_value = raw_value.to_s.gsub(symbol, "").gsub(/^-/, "")
+ raw_value = raw_value.to_s.gsub(symbol, "")
+ abs_raw_value = raw_value.gsub(/^-/, "")
- decimal_pieces = raw_value.split(decimal_mark)
+ decimal_pieces = abs_raw_value.split(decimal_mark)
# check for numbers like '12.23.45' or '....'
unless [1, 2].include? decimal_pieces.length
@@ -57,10 +57,17 @@ def monetized_attributes
# Include numericality validation if needed
if MoneyRails.include_validations
- validates_numericality_of subunit_name, :allow_nil => options[:allow_nil]
-
+ validation_options = {
+ :allow_nil => options[:allow_nil],
+ :numericality => true
+ }
+ validates subunit_name, validation_options
+
+ validation_options = { :allow_nil => options[:allow_nil] }
+ validation_options = options[:numericality].merge(validation_options) if options[:numericality]
+
# Allow only Money objects or Numeric values!
- validates name.to_sym, 'money_rails/active_model/money' => { :allow_nil => options[:allow_nil] }
+ validates name.to_sym, 'money_rails/active_model/money' => validation_options
end
define_method name do |*args|
@@ -91,6 +91,20 @@ class Sub < Product; end
@product.errors[:price].first.should match(/Must be a valid/)
end
+ it "fails validation with the proper error message using numericality validations" do
+ @product.price_in_a_range = "-123"
+ @product.valid?.should be_false
+ @product.errors[:price_in_a_range].first.should match(/Must be greater than zero and less than \$10k/)
+
+ @product.price_in_a_range = "123"
+
+ @product.valid?.should be_true
+
+ @product.price_in_a_range = "10001"
+ @product.valid?.should be_false
+ @product.errors[:price_in_a_range].first.should match(/Must be greater than zero and less than \$10k/)
+ end
+
it "passes validation if money value has correct format" do
@product.price = "12,230.24"
@product.save.should be_true
@@ -23,4 +23,11 @@ class Product < ActiveRecord::Base
monetize :sale_price_amount, :as => :sale_price,
:with_model_currency => :sale_price_currency_code
+ monetize :price_in_a_range_cents, :allow_nil => true,
+ :numericality => {
+ :greater_than_or_equal_to => 0,
+ :less_than_or_equal_to => 10000,
+ :message => "Must be greater than zero and less than $10k"
+ }
+
end
Binary file not shown.
@@ -0,0 +1,5 @@
+class AddPriceInARangeCentsToProducts < ActiveRecord::Migration
+ def change
+ add_column :products, :price_in_a_range_cents, :integer
+ end
+end
View
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120712202655) do
+ActiveRecord::Schema.define(:version => 20130124023419) do
create_table "dummy_products", :force => true do |t|
t.string "currency"
@@ -20,6 +20,9 @@
t.datetime "updated_at", :null => false
end
+ create_table "items", :force => true do |t|
+ end
+
create_table "products", :force => true do |t|
t.integer "price_cents"
t.integer "discount"
@@ -29,6 +32,7 @@
t.integer "optional_price_cents"
t.integer "sale_price_amount", :default => 0, :null => false
t.string "sale_price_currency_code"
+ t.integer "price_in_a_range_cents"
end
create_table "services", :force => true do |t|
@@ -1,5 +1,5 @@
CREATE TABLE "dummy_products" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "currency" varchar(255), "price_cents" integer, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
-CREATE TABLE "products" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "price_cents" integer, "discount" integer, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL, "bonus_cents" integer, "optional_price_cents" integer);
+CREATE TABLE "products" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "price_cents" integer, "discount" integer, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL, "bonus_cents" integer, "optional_price_cents" integer, "price_in_a_range_cents" integer);
CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL);
CREATE TABLE "services" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "charge_cents" integer, "discount_cents" integer, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
CREATE TABLE "transactions" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "amount_cents" integer, "tax_cents" integer, "currency" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
@@ -14,4 +14,6 @@ INSERT INTO schema_migrations (version) VALUES ('20120528181002');
INSERT INTO schema_migrations (version) VALUES ('20120528210103');
-INSERT INTO schema_migrations (version) VALUES ('20120607210247');
+INSERT INTO schema_migrations (version) VALUES ('20120607210247');
+
+INSERT INTO schema_migrations (version) VALUES ('20130124023419');
View
Binary file not shown.

0 comments on commit 8ad4b3a

Please sign in to comment.