has_money_fields 0.0.5 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.md +8 -1
- data/has_money_fields.gemspec +1 -0
- data/lib/has_money_fields/version.rb +1 -1
- data/lib/has_money_fields.rb +13 -3
- data/spec/has_money_fields_spec.rb +19 -9
- metadata +24 -2
data/History.md
CHANGED
@@ -1,5 +1,12 @@
|
|
1
1
|
|
2
|
-
0.0
|
2
|
+
0.1.0 / 2013-01-16
|
3
|
+
==================
|
4
|
+
|
5
|
+
* Add constructor to avoid issues in some apps
|
6
|
+
* Tested in quite some production apps with good results, so we bump a major
|
7
|
+
version :)
|
8
|
+
|
9
|
+
0.0.4 / 2012-05-17
|
3
10
|
==================
|
4
11
|
|
5
12
|
* Fix error when using a Fixnum as argument
|
data/has_money_fields.gemspec
CHANGED
@@ -26,6 +26,7 @@ currency data in a safe and clean way.
|
|
26
26
|
s.add_dependency "money"
|
27
27
|
s.add_development_dependency "sqlite3"
|
28
28
|
s.add_development_dependency "rspec", "~> 2"
|
29
|
+
s.add_development_dependency "rake"
|
29
30
|
s.add_development_dependency "guard"
|
30
31
|
s.add_development_dependency "guard-rspec"
|
31
32
|
end
|
data/lib/has_money_fields.rb
CHANGED
@@ -10,8 +10,9 @@ module HasMoneyFields
|
|
10
10
|
|
11
11
|
args.each do |attr|
|
12
12
|
converter = Proc.new do |value|
|
13
|
-
|
14
|
-
value.gsub!
|
13
|
+
# remove all non digit characters from the price
|
14
|
+
value.gsub!(/[^\d|\.|\,]/, "") if value.instance_of?(String)
|
15
|
+
|
15
16
|
if value.respond_to? :to_money
|
16
17
|
value.to_money
|
17
18
|
else
|
@@ -19,6 +20,15 @@ module HasMoneyFields
|
|
19
20
|
end
|
20
21
|
end
|
21
22
|
|
23
|
+
constructor = Proc.new do |cents, currency|
|
24
|
+
|
25
|
+
cents = cents.to_i if cents.instance_of?(String)
|
26
|
+
|
27
|
+
currency = Money.default_currency if currency.nil?
|
28
|
+
|
29
|
+
Money.new(cents, currency)
|
30
|
+
end
|
31
|
+
|
22
32
|
if options[:only_cents]
|
23
33
|
mapping = [["money_#{attr.to_s}", "cents"]]
|
24
34
|
else
|
@@ -28,7 +38,7 @@ module HasMoneyFields
|
|
28
38
|
self.composed_of attr,
|
29
39
|
:class_name => "Money",
|
30
40
|
:mapping => mapping,
|
31
|
-
:constructor =>
|
41
|
+
:constructor => constructor,
|
32
42
|
:converter => converter,
|
33
43
|
:allow_nil => true
|
34
44
|
|
@@ -46,9 +46,9 @@ describe "Model with money fields" do
|
|
46
46
|
subject {@product}
|
47
47
|
|
48
48
|
its(:money_price){ should == 100000 }
|
49
|
-
its(:currency_price){ should == "USD" }
|
49
|
+
its(:currency_price){ should == "USD" }
|
50
50
|
end
|
51
|
-
|
51
|
+
|
52
52
|
context "with price with commas" do
|
53
53
|
before do
|
54
54
|
@product = Product.create! :price => "$1000,50 + VAT"
|
@@ -56,9 +56,9 @@ describe "Model with money fields" do
|
|
56
56
|
subject {@product}
|
57
57
|
|
58
58
|
its(:money_price){ should == 100050 }
|
59
|
-
its(:currency_price){ should == "USD" }
|
59
|
+
its(:currency_price){ should == "USD" }
|
60
60
|
end
|
61
|
-
|
61
|
+
|
62
62
|
context "with price with periods" do
|
63
63
|
before do
|
64
64
|
@product = Product.create! :price => "$1000.50 + VAT"
|
@@ -66,17 +66,27 @@ describe "Model with money fields" do
|
|
66
66
|
subject {@product}
|
67
67
|
|
68
68
|
its(:money_price){ should == 100050 }
|
69
|
-
its(:currency_price){ should == "USD" }
|
69
|
+
its(:currency_price){ should == "USD" }
|
70
70
|
end
|
71
|
-
|
71
|
+
|
72
|
+
context "some test which includes periods" do
|
73
|
+
before do
|
74
|
+
@product = Product.create! :price => "$1000.50 INCL."
|
75
|
+
end
|
76
|
+
subject {@product}
|
77
|
+
|
78
|
+
its(:money_price){ should == 100050 }
|
79
|
+
its(:currency_price){ should == "USD" }
|
80
|
+
end
|
81
|
+
|
72
82
|
|
73
83
|
context "with number only prices" do
|
74
84
|
before do
|
75
|
-
@product = Product.create! :price => 1000
|
85
|
+
@product = Product.create! :price => 1000
|
76
86
|
end
|
77
87
|
subject {@product}
|
78
88
|
|
79
|
-
its(:money_price){ should == 100000 }
|
89
|
+
its(:money_price){ should == 100000 }
|
80
90
|
end
|
81
91
|
|
82
92
|
context "with validations" do
|
@@ -85,7 +95,7 @@ describe "Model with money fields" do
|
|
85
95
|
validates :price, :presence => true
|
86
96
|
end
|
87
97
|
|
88
|
-
@product = Product.new :price =>
|
98
|
+
@product = Product.new :price => ""
|
89
99
|
end
|
90
100
|
|
91
101
|
it "is invalid" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_money_fields
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-01-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -75,6 +75,22 @@ dependencies:
|
|
75
75
|
- - ~>
|
76
76
|
- !ruby/object:Gem::Version
|
77
77
|
version: '2'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rake
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
78
94
|
- !ruby/object:Gem::Dependency
|
79
95
|
name: guard
|
80
96
|
requirement: !ruby/object:Gem::Requirement
|
@@ -145,12 +161,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
145
161
|
- - ! '>='
|
146
162
|
- !ruby/object:Gem::Version
|
147
163
|
version: '0'
|
164
|
+
segments:
|
165
|
+
- 0
|
166
|
+
hash: -2465088672690308953
|
148
167
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
149
168
|
none: false
|
150
169
|
requirements:
|
151
170
|
- - ! '>='
|
152
171
|
- !ruby/object:Gem::Version
|
153
172
|
version: '0'
|
173
|
+
segments:
|
174
|
+
- 0
|
175
|
+
hash: -2465088672690308953
|
154
176
|
requirements: []
|
155
177
|
rubyforge_project: has_money_fields
|
156
178
|
rubygems_version: 1.8.24
|