public
Description: Allows you to enter a '$' in fields stored as decimal but are actually currency types
Homepage: http://www.faithfulgeek.org/2008/3/26/acts_as_currency-released
Clone URL: git://github.com/faithfulgeek/acts_as_currency.git
Search Repo:
Initial repository setup; adding required files.
Joe Fiorini (author)
Tue Mar 25 23:41:53 -0700 2008
commit  9f36b48c6ce640bccbaf01d6647fc7ab283642d8
tree    0b573845d0fbd4d9c50a58a279671ce4dece21c3
...
 
...
1
0
@@ -0,0 +1 @@
0
+require 'acts_as_currency'
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
0
@@ -0,0 +1,20 @@
0
+class ActiveRecord::Base
0
+
0
+ def self.acts_as_currency(*args)
0
+
0
+ args.each do |arg|
0
+
0
+ define_method arg.to_s + '=' do |currency_string|
0
+ currency_string.gsub!(/^\$/, '') if currency_string =~ /^\$/
0
+ write_attribute(arg, currency_string)
0
+ end
0
+
0
+ end
0
+
0
+ define_method 'validate' do
0
+ args.each { |arg| errors.add(arg, "should be at least 0.01") if eval(arg.to_s).nil? || eval(arg.to_s) < 0.01 }
0
+ end
0
+
0
+ end
0
+
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
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
35
36
37
38
39
0
@@ -0,0 +1,39 @@
0
+require File.dirname(__FILE__) + '/spec_helper'
0
+require File.dirname(__FILE__) + '/model/product'
0
+require File.dirname(__FILE__) + '/model/consultant'
0
+
0
+# The following spec shows the problem...
0
+describe 'A model, consultant, that does not call acts_as_currency' do
0
+
0
+ it "will lose values after the dollar sign in decimal fields when using a '$'" do
0
+
0
+ consultant = Consultant.new
0
+ consultant.name = 'Joe Testing'
0
+ consultant.hourly_rate = '$100'
0
+
0
+ consultant.hourly_rate.should == BigDecimal.new('0')
0
+
0
+ end
0
+
0
+end
0
+
0
+# ...and this spec shows the solution!
0
+describe 'A model, product, that calls acts_as_currency' do
0
+
0
+ it "will allow '$' character in specified price fields" do
0
+
0
+ product = Product.new
0
+ product.description = 'Sample Product'
0
+ product.quantity = 100
0
+ product.price = '$100.32'
0
+ product.discount_price = '$10.53'
0
+
0
+ product.price.should == BigDecimal.new('100.32')
0
+ product.discount_price.should == BigDecimal.new('10.53')
0
+
0
+ product.discount_price = 10.53
0
+ product.discount_price.should == BigDecimal.new('10.53')
0
+
0
+ end
0
+
0
+end
...
 
 
 
...
1
2
3
0
@@ -0,0 +1,3 @@
0
+sqlite3:
0
+ :adapter: sqlite3
0
+ :dbfile: vendor/plugins/acts_as_currency/spec/db/acts_as_currency.sqlite3.db
...
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
0
@@ -0,0 +1,13 @@
0
+ActiveRecord::Schema.define(:version => 0) do
0
+ create_table :products, :force => true do |t|
0
+ t.column :description, :string
0
+ t.column :quantity, :int
0
+ t.column :price, :decimal
0
+ t.column :discount_price, :decimal
0
+ end
0
+
0
+ create_table :consultants, :force => true do |t|
0
+ t.column :name, :string
0
+ t.column :hourly_rate, :decimal
0
+ end
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
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
35
36
37
0
@@ -0,0 +1,37 @@
0
+# Logfile created on Wed Mar 26 02:19:10 -0400 2008 by /
0
+ SQL (0.000264) select sqlite_version(*)
0
+ SQL (0.003154) CREATE TABLE schema_info (version integer)
0
+ SQL (0.002144) INSERT INTO schema_info (version) VALUES(0)
0
+ SQL (0.001842) UPDATE schema_info SET version = 0
0
+ SQL (0.000247) select sqlite_version(*)
0
+ SQL (0.000000) SQLite3::SQLException: no such table: products: DROP TABLE products
0
+ SQL (0.002771) CREATE TABLE products ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "description" varchar(255) DEFAULT NULL, "quantity" int DEFAULT NULL, "price" decimal DEFAULT NULL, "discount_price" decimal DEFAULT NULL) 
0
+ SQL (0.000000) SQLite3::SQLException: no such table: consultants: DROP TABLE consultants
0
+ SQL (0.002657) CREATE TABLE consultants ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255) DEFAULT NULL, "hourly_rate" decimal DEFAULT NULL) 
0
+ SQL (0.000000) SQLite3::SQLException: table schema_info already exists: CREATE TABLE schema_info (version integer)
0
+ SQL (0.002893) UPDATE schema_info SET version = 0
0
+ SQL (0.000240) select sqlite_version(*)
0
+ SQL (0.003287) DROP TABLE products
0
+ SQL (0.002525) CREATE TABLE products ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "description" varchar(255) DEFAULT NULL, "quantity" int DEFAULT NULL, "price" decimal DEFAULT NULL, "discount_price" decimal DEFAULT NULL) 
0
+ SQL (0.002686) DROP TABLE consultants
0
+ SQL (0.003295) CREATE TABLE consultants ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255) DEFAULT NULL, "hourly_rate" decimal DEFAULT NULL) 
0
+ SQL (0.000000) SQLite3::SQLException: table schema_info already exists: CREATE TABLE schema_info (version integer)
0
+ SQL (0.002115) UPDATE schema_info SET version = 0
0
+ SQL (0.000601)  SELECT name
0
+ FROM sqlite_master
0
+ WHERE type = 'table' AND NOT name = 'sqlite_sequence'
0
+
0
+ SQL (0.000347) select sqlite_version(*)
0
+ SQL (0.002315) DROP TABLE products
0
+ SQL (0.002372) CREATE TABLE products ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "description" varchar(255) DEFAULT NULL, "quantity" int DEFAULT NULL, "price" decimal DEFAULT NULL, "discount_price" decimal DEFAULT NULL) 
0
+ SQL (0.002998) DROP TABLE consultants
0
+ SQL (0.002903) CREATE TABLE consultants ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255) DEFAULT NULL, "hourly_rate" decimal DEFAULT NULL) 
0
+ SQL (0.000000) SQLite3::SQLException: table schema_info already exists: CREATE TABLE schema_info (version integer)
0
+ SQL (0.002485) UPDATE schema_info SET version = 0
0
+ SQL (0.000250) select sqlite_version(*)
0
+ SQL (0.003018) DROP TABLE products
0
+ SQL (0.002419) CREATE TABLE products ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "description" varchar(255) DEFAULT NULL, "quantity" int DEFAULT NULL, "price" decimal DEFAULT NULL, "discount_price" decimal DEFAULT NULL) 
0
+ SQL (0.002720) DROP TABLE consultants
0
+ SQL (0.002945) CREATE TABLE consultants ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255) DEFAULT NULL, "hourly_rate" decimal DEFAULT NULL) 
0
+ SQL (0.000000) SQLite3::SQLException: table schema_info already exists: CREATE TABLE schema_info (version integer)
0
+ SQL (0.002367) UPDATE schema_info SET version = 0
...
 
 
 
0
...
1
2
3
4
0
@@ -0,0 +1,3 @@
0
+class Consultant < ActiveRecord::Base
0
+
0
+end
0
\ No newline at end of file
...
 
 
 
 
 
0
...
1
2
3
4
5
6
0
@@ -0,0 +1,5 @@
0
+require File.dirname(__FILE__) + '/../../lib/acts_as_currency'
0
+
0
+class Product < ActiveRecord::Base
0
+ acts_as_currency :price, :discount_price
0
+end
0
\ No newline at end of file
...
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
0
@@ -0,0 +1,8 @@
0
+require File.dirname(__FILE__) + '/../../../../spec/spec_helper'
0
+
0
+plugin_spec_dir = File.dirname(__FILE__)
0
+ActiveRecord::Base.logger = Logger.new(plugin_spec_dir + "/debug.log")
0
+
0
+databases = YAML::load(IO.read(plugin_spec_dir + "/db/database.yml"))
0
+ActiveRecord::Base.establish_connection(databases[ENV["DB"] || "sqlite3"])
0
+load(File.join(plugin_spec_dir, "db", "schema.rb"))

Comments

    No one has commented yet.