acts_as_price 0.1.3 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +6 -4
- data/VERSION +1 -1
- data/acts_as_price.gemspec +1 -1
- data/lib/acts_as_price.rb +14 -4
- data/lib/acts_as_price_helper.rb +47 -17
- data/rdoc/classes/ActiveRecord/Acts/Price/ClassMethods.html +22 -12
- data/rdoc/classes/ActsAsPriceHelper.html +90 -60
- data/rdoc/created.rid +1 -1
- data/rdoc/files/README_rdoc.html +9 -6
- data/rdoc/files/lib/acts_as_price_helper_rb.html +1 -1
- data/rdoc/files/lib/acts_as_price_rb.html +1 -1
- data/spec/advanced_tests/car_spec.rb +3 -3
- data/spec/models/car.rb +1 -1
- data/spec/simple_tests/car_and_fueltype_spec.rb +11 -4
- metadata +4 -4
data/README.rdoc
CHANGED
|
@@ -7,13 +7,15 @@ Create a column in the table of your database which you want to acts as a price.
|
|
|
7
7
|
NOTE THAT THE COLUMN MUST BE OF THE TYPE INTEGER.
|
|
8
8
|
|
|
9
9
|
In your model add the following:
|
|
10
|
-
- acts_as_price column_name, {:validates => true, :comma_seperated => true}
|
|
10
|
+
- acts_as_price column_name, {:validates => true, :comma_seperated => true, :currency => currency}
|
|
11
11
|
|
|
12
12
|
column_name is the name of your database column e.g. price or price_per_liter.
|
|
13
13
|
|
|
14
14
|
validates is optional and add validation to the column
|
|
15
15
|
|
|
16
|
-
comma_seperated is
|
|
16
|
+
comma_seperated is optional and return the price comma seperated instead of dot seperated
|
|
17
|
+
|
|
18
|
+
currency is optional and adds a prefix for the currency to the price.
|
|
17
19
|
|
|
18
20
|
This plugin creates the following getters and setters:
|
|
19
21
|
- 'price_in_cents' and '<column_name>_in_cents' #sets and returns the price in cents
|
|
@@ -21,12 +23,12 @@ This plugin creates the following getters and setters:
|
|
|
21
23
|
|
|
22
24
|
=EXAMPLES
|
|
23
25
|
class Car < ActiveRecord::Base
|
|
24
|
-
acts_as_price :price, :comma_seperated => true
|
|
26
|
+
acts_as_price :price, :comma_seperated => true, :currency => "EUR"
|
|
25
27
|
end
|
|
26
28
|
|
|
27
29
|
car = Car.new :price => 12999
|
|
28
30
|
|
|
29
|
-
car.price -> 12999,00
|
|
31
|
+
car.price -> EUR. 12999,00
|
|
30
32
|
|
|
31
33
|
car.price_in_cents -> 1299900
|
|
32
34
|
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.
|
|
1
|
+
0.2.0
|
data/acts_as_price.gemspec
CHANGED
data/lib/acts_as_price.rb
CHANGED
|
@@ -14,7 +14,7 @@ module ActiveRecord
|
|
|
14
14
|
# * :comma_seperated => true (set and get the price as a comma-seperated value)
|
|
15
15
|
def acts_as_price column_name, options = {}
|
|
16
16
|
validates column_name, :presence => true, :numericality => {:greater_than => 0} if options[:validates] == true
|
|
17
|
-
comma = options[:comma_seperated]
|
|
17
|
+
currency, comma = options[:currency], options[:comma_seperated]
|
|
18
18
|
|
|
19
19
|
#setters
|
|
20
20
|
define_method("#{column_name}=") do |val|
|
|
@@ -33,18 +33,28 @@ module ActiveRecord
|
|
|
33
33
|
""
|
|
34
34
|
else
|
|
35
35
|
if comma
|
|
36
|
-
sprintf("%.2f", super.to_f / 100).gsub(".", ",")
|
|
36
|
+
return_val = sprintf("%.2f", super.to_f / 100).gsub(".", ",")
|
|
37
37
|
else
|
|
38
|
-
sprintf("%.2f", super.to_f / 100)
|
|
38
|
+
return_val = sprintf("%.2f", super.to_f / 100)
|
|
39
39
|
end
|
|
40
|
+
return_val = currency ? "#{currency}. #{return_val}" : return_val
|
|
41
|
+
return_val
|
|
40
42
|
end
|
|
41
43
|
end
|
|
42
44
|
alias_method "price", column_name
|
|
43
45
|
|
|
44
46
|
define_method("price_in_cents") do
|
|
45
|
-
|
|
47
|
+
if currency
|
|
48
|
+
((send column_name).gsub("#{currency}. ", "").gsub(",", ".").to_f * 100).to_s.to_i
|
|
49
|
+
else
|
|
50
|
+
((send column_name).gsub(",", ".").to_f * 100).to_s.to_i
|
|
51
|
+
end
|
|
46
52
|
end
|
|
47
53
|
alias_method "#{column_name}_in_cents", "price_in_cents"
|
|
54
|
+
|
|
55
|
+
define_method("currency") do
|
|
56
|
+
currency
|
|
57
|
+
end
|
|
48
58
|
end
|
|
49
59
|
end
|
|
50
60
|
end
|
data/lib/acts_as_price_helper.rb
CHANGED
|
@@ -20,21 +20,40 @@ module ActsAsPriceHelper
|
|
|
20
20
|
|
|
21
21
|
#normal test for setter
|
|
22
22
|
test_setter_in_cents "144", seperator
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
23
|
+
if @acts_as_price_model.currency
|
|
24
|
+
currency = @acts_as_price_model.currency
|
|
25
|
+
test_setter_in_doubles "#{currency}. 1.41", seperator
|
|
26
|
+
test_setter_in_doubles "#{currency}. 1,41", seperator
|
|
27
|
+
|
|
28
|
+
#test for special cases
|
|
29
|
+
#test if 1.5 is returned as 1.50
|
|
30
|
+
test_setter_in_doubles "#{currency}. 1.5", seperator
|
|
31
|
+
#test if 1,5 is returned as 1.50
|
|
32
|
+
test_setter_in_doubles "#{currency}. 1,5", seperator
|
|
33
|
+
|
|
34
|
+
#test if float returns 2.05 correctly
|
|
35
|
+
#float can return 2.05 as 2.04 in some cases
|
|
36
|
+
test_setter_in_doubles "#{currency}. 2.05", seperator
|
|
37
|
+
|
|
38
|
+
#float can return 2,05 as 2.04 in some cases
|
|
39
|
+
test_setter_in_doubles "#{currency}. 2,05", seperator
|
|
40
|
+
else
|
|
41
|
+
test_setter_in_doubles "1.41", seperator
|
|
42
|
+
test_setter_in_doubles "1,41", seperator
|
|
43
|
+
|
|
44
|
+
#test for special cases
|
|
45
|
+
#test if 1.5 is returned as 1.50
|
|
46
|
+
test_setter_in_doubles "1.5", seperator
|
|
47
|
+
#test if 1,5 is returned as 1.50
|
|
48
|
+
test_setter_in_doubles "1,5", seperator
|
|
49
|
+
|
|
50
|
+
#test if float returns 2.05 correctly
|
|
51
|
+
#float can return 2.05 as 2.04 in some cases
|
|
52
|
+
test_setter_in_doubles "2.05", seperator
|
|
53
|
+
|
|
54
|
+
#float can return 2,05 as 2.04 in some cases
|
|
55
|
+
test_setter_in_doubles "2,05", seperator
|
|
56
|
+
end
|
|
38
57
|
|
|
39
58
|
#test an empty_setter
|
|
40
59
|
test_setter_in_cents "", seperator
|
|
@@ -52,7 +71,12 @@ module ActsAsPriceHelper
|
|
|
52
71
|
if cents == ""
|
|
53
72
|
@acts_as_price_model.send(getter).should == ""
|
|
54
73
|
else
|
|
55
|
-
@acts_as_price_model.
|
|
74
|
+
currency = @acts_as_price_model.currency
|
|
75
|
+
if currency
|
|
76
|
+
@acts_as_price_model.send(getter).should == currency + ". " + sprintf("%.2f", (cents.to_f / 100).to_s).gsub(".", seperator)
|
|
77
|
+
else
|
|
78
|
+
@acts_as_price_model.send(getter).should == sprintf("%.2f", (cents.to_f / 100).to_s).gsub(".", seperator)
|
|
79
|
+
end
|
|
56
80
|
end
|
|
57
81
|
end
|
|
58
82
|
end
|
|
@@ -60,6 +84,8 @@ module ActsAsPriceHelper
|
|
|
60
84
|
|
|
61
85
|
# test if the setter sets the price correctly if price is given in doubles
|
|
62
86
|
def test_setter_in_doubles double, seperator
|
|
87
|
+
currency = @acts_as_price_model.currency
|
|
88
|
+
double = double.gsub("#{currency}. ", "")
|
|
63
89
|
columns_in_doubles.each do |setter|
|
|
64
90
|
@acts_as_price_model.send("#{setter}=", double)
|
|
65
91
|
columns_in_cents.each do |getter|
|
|
@@ -69,7 +95,11 @@ module ActsAsPriceHelper
|
|
|
69
95
|
if double == ""
|
|
70
96
|
@acts_as_price_model.send(getter).should == ""
|
|
71
97
|
else
|
|
72
|
-
|
|
98
|
+
if currency
|
|
99
|
+
@acts_as_price_model.send(getter).should == currency + ". " + sprintf("%.2f", double.gsub(",", ".")).gsub(".", seperator)
|
|
100
|
+
else
|
|
101
|
+
@acts_as_price_model.send(getter).should == sprintf("%.2f", double.gsub(",", ".")).gsub(".", seperator)
|
|
102
|
+
end
|
|
73
103
|
end
|
|
74
104
|
end
|
|
75
105
|
end
|
|
@@ -135,7 +135,7 @@ value)
|
|
|
135
135
|
<span class="ruby-comment cmt"># File lib/acts_as_price.rb, line 15</span>
|
|
136
136
|
15: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">acts_as_price</span> <span class="ruby-identifier">column_name</span>, <span class="ruby-identifier">options</span> = {}
|
|
137
137
|
16: <span class="ruby-identifier">validates</span> <span class="ruby-identifier">column_name</span>, <span class="ruby-identifier">:presence</span> =<span class="ruby-operator">></span> <span class="ruby-keyword kw">true</span>, <span class="ruby-identifier">:numericality</span> =<span class="ruby-operator">></span> {<span class="ruby-identifier">:greater_than</span> =<span class="ruby-operator">></span> <span class="ruby-value">0</span>} <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:validates</span>] <span class="ruby-operator">==</span> <span class="ruby-keyword kw">true</span>
|
|
138
|
-
17: <span class="ruby-identifier">comma</span> = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:comma_seperated</span>]
|
|
138
|
+
17: <span class="ruby-identifier">currency</span>, <span class="ruby-identifier">comma</span> = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:currency</span>], <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:comma_seperated</span>]
|
|
139
139
|
18:
|
|
140
140
|
19: <span class="ruby-comment cmt">#setters</span>
|
|
141
141
|
20: <span class="ruby-identifier">define_method</span>(<span class="ruby-node">"#{column_name}="</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">val</span><span class="ruby-operator">|</span>
|
|
@@ -154,19 +154,29 @@ value)
|
|
|
154
154
|
33: <span class="ruby-value str">""</span>
|
|
155
155
|
34: <span class="ruby-keyword kw">else</span>
|
|
156
156
|
35: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">comma</span>
|
|
157
|
-
36: <span class="ruby-identifier">sprintf</span>(<span class="ruby-value str">"%.2f"</span>, <span class="ruby-keyword kw">super</span>.<span class="ruby-identifier">to_f</span> <span class="ruby-operator">/</span> <span class="ruby-value">100</span>).<span class="ruby-identifier">gsub</span>(<span class="ruby-value str">"."</span>, <span class="ruby-value str">","</span>)
|
|
157
|
+
36: <span class="ruby-identifier">return_val</span> = <span class="ruby-identifier">sprintf</span>(<span class="ruby-value str">"%.2f"</span>, <span class="ruby-keyword kw">super</span>.<span class="ruby-identifier">to_f</span> <span class="ruby-operator">/</span> <span class="ruby-value">100</span>).<span class="ruby-identifier">gsub</span>(<span class="ruby-value str">"."</span>, <span class="ruby-value str">","</span>)
|
|
158
158
|
37: <span class="ruby-keyword kw">else</span>
|
|
159
|
-
38: <span class="ruby-identifier">sprintf</span>(<span class="ruby-value str">"%.2f"</span>, <span class="ruby-keyword kw">super</span>.<span class="ruby-identifier">to_f</span> <span class="ruby-operator">/</span> <span class="ruby-value">100</span>)
|
|
159
|
+
38: <span class="ruby-identifier">return_val</span> = <span class="ruby-identifier">sprintf</span>(<span class="ruby-value str">"%.2f"</span>, <span class="ruby-keyword kw">super</span>.<span class="ruby-identifier">to_f</span> <span class="ruby-operator">/</span> <span class="ruby-value">100</span>)
|
|
160
160
|
39: <span class="ruby-keyword kw">end</span>
|
|
161
|
-
40:
|
|
162
|
-
41:
|
|
163
|
-
42:
|
|
164
|
-
43:
|
|
165
|
-
44: <span class="ruby-identifier">
|
|
166
|
-
45:
|
|
167
|
-
46: <span class="ruby-keyword kw">
|
|
168
|
-
47:
|
|
169
|
-
48:
|
|
161
|
+
40: <span class="ruby-identifier">return_val</span> = <span class="ruby-identifier">currency</span> <span class="ruby-value">? </span><span class="ruby-node">"#{currency}. #{return_val}"</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">return_val</span>
|
|
162
|
+
41: <span class="ruby-identifier">return_val</span>
|
|
163
|
+
42: <span class="ruby-keyword kw">end</span>
|
|
164
|
+
43: <span class="ruby-keyword kw">end</span>
|
|
165
|
+
44: <span class="ruby-identifier">alias_method</span> <span class="ruby-value str">"price"</span>, <span class="ruby-identifier">column_name</span>
|
|
166
|
+
45:
|
|
167
|
+
46: <span class="ruby-identifier">define_method</span>(<span class="ruby-value str">"price_in_cents"</span>) <span class="ruby-keyword kw">do</span>
|
|
168
|
+
47: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">currency</span>
|
|
169
|
+
48: ((<span class="ruby-identifier">send</span> <span class="ruby-identifier">column_name</span>).<span class="ruby-identifier">gsub</span>(<span class="ruby-node">"#{currency}. "</span>, <span class="ruby-value str">""</span>).<span class="ruby-identifier">gsub</span>(<span class="ruby-value str">","</span>, <span class="ruby-value str">"."</span>).<span class="ruby-identifier">to_f</span> <span class="ruby-operator">*</span> <span class="ruby-value">100</span>).<span class="ruby-identifier">to_s</span>.<span class="ruby-identifier">to_i</span>
|
|
170
|
+
49: <span class="ruby-keyword kw">else</span>
|
|
171
|
+
50: ((<span class="ruby-identifier">send</span> <span class="ruby-identifier">column_name</span>).<span class="ruby-identifier">gsub</span>(<span class="ruby-value str">","</span>, <span class="ruby-value str">"."</span>).<span class="ruby-identifier">to_f</span> <span class="ruby-operator">*</span> <span class="ruby-value">100</span>).<span class="ruby-identifier">to_s</span>.<span class="ruby-identifier">to_i</span>
|
|
172
|
+
51: <span class="ruby-keyword kw">end</span>
|
|
173
|
+
52: <span class="ruby-keyword kw">end</span>
|
|
174
|
+
53: <span class="ruby-identifier">alias_method</span> <span class="ruby-node">"#{column_name}_in_cents"</span>, <span class="ruby-value str">"price_in_cents"</span>
|
|
175
|
+
54:
|
|
176
|
+
55: <span class="ruby-identifier">define_method</span>(<span class="ruby-value str">"currency"</span>) <span class="ruby-keyword kw">do</span>
|
|
177
|
+
56: <span class="ruby-identifier">currency</span>
|
|
178
|
+
57: <span class="ruby-keyword kw">end</span>
|
|
179
|
+
58: <span class="ruby-keyword kw">end</span>
|
|
170
180
|
</pre>
|
|
171
181
|
</div>
|
|
172
182
|
</div>
|
|
@@ -120,10 +120,10 @@
|
|
|
120
120
|
onclick="toggleCode('M000006-source');return false;">[Source]</a></p>
|
|
121
121
|
<div class="method-source-code" id="M000006-source">
|
|
122
122
|
<pre>
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
123
|
+
<span class="ruby-comment cmt"># File lib/acts_as_price_helper.rb, line 108</span>
|
|
124
|
+
108: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">columns_in_cents</span>
|
|
125
|
+
109: [<span class="ruby-identifier">:price_in_cents</span>, <span class="ruby-node">"#{@column_name}_in_cents"</span>]
|
|
126
|
+
110: <span class="ruby-keyword kw">end</span>
|
|
127
127
|
</pre>
|
|
128
128
|
</div>
|
|
129
129
|
</div>
|
|
@@ -143,10 +143,10 @@
|
|
|
143
143
|
onclick="toggleCode('M000007-source');return false;">[Source]</a></p>
|
|
144
144
|
<div class="method-source-code" id="M000007-source">
|
|
145
145
|
<pre>
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
146
|
+
<span class="ruby-comment cmt"># File lib/acts_as_price_helper.rb, line 112</span>
|
|
147
|
+
112: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">columns_in_doubles</span>
|
|
148
|
+
113: [<span class="ruby-identifier">:price</span>, <span class="ruby-ivar">@column_name</span>]
|
|
149
|
+
114: <span class="ruby-keyword kw">end</span>
|
|
150
150
|
</pre>
|
|
151
151
|
</div>
|
|
152
152
|
</div>
|
|
@@ -204,26 +204,45 @@ Note the you can also specify the model in a before block in your tests
|
|
|
204
204
|
20:
|
|
205
205
|
21: <span class="ruby-comment cmt">#normal test for setter</span>
|
|
206
206
|
22: <span class="ruby-identifier">test_setter_in_cents</span> <span class="ruby-value str">"144"</span>, <span class="ruby-identifier">seperator</span>
|
|
207
|
-
23: <span class="ruby-
|
|
208
|
-
24:
|
|
209
|
-
25:
|
|
210
|
-
26:
|
|
211
|
-
27:
|
|
212
|
-
28:
|
|
213
|
-
29:
|
|
214
|
-
30:
|
|
215
|
-
31:
|
|
216
|
-
32:
|
|
217
|
-
33:
|
|
218
|
-
34:
|
|
219
|
-
35:
|
|
220
|
-
36:
|
|
221
|
-
37:
|
|
222
|
-
38:
|
|
223
|
-
39:
|
|
224
|
-
40: <span class="ruby-
|
|
225
|
-
41:
|
|
226
|
-
42:
|
|
207
|
+
23: <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">currency</span>
|
|
208
|
+
24: <span class="ruby-identifier">currency</span> = <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">currency</span>
|
|
209
|
+
25: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-node">"#{currency}. 1.41"</span>, <span class="ruby-identifier">seperator</span>
|
|
210
|
+
26: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-node">"#{currency}. 1,41"</span>, <span class="ruby-identifier">seperator</span>
|
|
211
|
+
27:
|
|
212
|
+
28: <span class="ruby-comment cmt">#test for special cases</span>
|
|
213
|
+
29: <span class="ruby-comment cmt">#test if 1.5 is returned as 1.50</span>
|
|
214
|
+
30: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-node">"#{currency}. 1.5"</span>, <span class="ruby-identifier">seperator</span>
|
|
215
|
+
31: <span class="ruby-comment cmt">#test if 1,5 is returned as 1.50</span>
|
|
216
|
+
32: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-node">"#{currency}. 1,5"</span>, <span class="ruby-identifier">seperator</span>
|
|
217
|
+
33:
|
|
218
|
+
34: <span class="ruby-comment cmt">#test if float returns 2.05 correctly </span>
|
|
219
|
+
35: <span class="ruby-comment cmt">#float can return 2.05 as 2.04 in some cases</span>
|
|
220
|
+
36: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-node">"#{currency}. 2.05"</span>, <span class="ruby-identifier">seperator</span>
|
|
221
|
+
37:
|
|
222
|
+
38: <span class="ruby-comment cmt">#float can return 2,05 as 2.04 in some cases</span>
|
|
223
|
+
39: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-node">"#{currency}. 2,05"</span>, <span class="ruby-identifier">seperator</span>
|
|
224
|
+
40: <span class="ruby-keyword kw">else</span>
|
|
225
|
+
41: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-value str">"1.41"</span>, <span class="ruby-identifier">seperator</span>
|
|
226
|
+
42: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-value str">"1,41"</span>, <span class="ruby-identifier">seperator</span>
|
|
227
|
+
43:
|
|
228
|
+
44: <span class="ruby-comment cmt">#test for special cases</span>
|
|
229
|
+
45: <span class="ruby-comment cmt">#test if 1.5 is returned as 1.50</span>
|
|
230
|
+
46: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-value str">"1.5"</span>, <span class="ruby-identifier">seperator</span>
|
|
231
|
+
47: <span class="ruby-comment cmt">#test if 1,5 is returned as 1.50</span>
|
|
232
|
+
48: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-value str">"1,5"</span>, <span class="ruby-identifier">seperator</span>
|
|
233
|
+
49:
|
|
234
|
+
50: <span class="ruby-comment cmt">#test if float returns 2.05 correctly </span>
|
|
235
|
+
51: <span class="ruby-comment cmt">#float can return 2.05 as 2.04 in some cases</span>
|
|
236
|
+
52: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-value str">"2.05"</span>, <span class="ruby-identifier">seperator</span>
|
|
237
|
+
53:
|
|
238
|
+
54: <span class="ruby-comment cmt">#float can return 2,05 as 2.04 in some cases</span>
|
|
239
|
+
55: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-value str">"2,05"</span>, <span class="ruby-identifier">seperator</span>
|
|
240
|
+
56: <span class="ruby-keyword kw">end</span>
|
|
241
|
+
57:
|
|
242
|
+
58: <span class="ruby-comment cmt">#test an empty_setter</span>
|
|
243
|
+
59: <span class="ruby-identifier">test_setter_in_cents</span> <span class="ruby-value str">""</span>, <span class="ruby-identifier">seperator</span>
|
|
244
|
+
60: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-value str">""</span>, <span class="ruby-identifier">seperator</span>
|
|
245
|
+
61: <span class="ruby-keyword kw">end</span>
|
|
227
246
|
</pre>
|
|
228
247
|
</div>
|
|
229
248
|
</div>
|
|
@@ -246,22 +265,27 @@ test if the setter sets the price correctly if price is given in integers
|
|
|
246
265
|
onclick="toggleCode('M000004-source');return false;">[Source]</a></p>
|
|
247
266
|
<div class="method-source-code" id="M000004-source">
|
|
248
267
|
<pre>
|
|
249
|
-
<span class="ruby-comment cmt"># File lib/acts_as_price_helper.rb, line
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
268
|
+
<span class="ruby-comment cmt"># File lib/acts_as_price_helper.rb, line 64</span>
|
|
269
|
+
64: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">test_setter_in_cents</span> <span class="ruby-identifier">cents</span>, <span class="ruby-identifier">seperator</span>
|
|
270
|
+
65: <span class="ruby-identifier">columns_in_cents</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">setter</span><span class="ruby-operator">|</span>
|
|
271
|
+
66: <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">send</span>(<span class="ruby-node">"#{setter}="</span>, <span class="ruby-identifier">cents</span>)
|
|
272
|
+
67: <span class="ruby-identifier">columns_in_cents</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">getter</span><span class="ruby-operator">|</span>
|
|
273
|
+
68: <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">getter</span>).<span class="ruby-identifier">should</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">cents</span>.<span class="ruby-identifier">to_i</span>
|
|
274
|
+
69: <span class="ruby-keyword kw">end</span>
|
|
275
|
+
70: <span class="ruby-identifier">columns_in_doubles</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">getter</span><span class="ruby-operator">|</span>
|
|
276
|
+
71: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">cents</span> <span class="ruby-operator">==</span> <span class="ruby-value str">""</span>
|
|
277
|
+
72: <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">getter</span>).<span class="ruby-identifier">should</span> <span class="ruby-operator">==</span> <span class="ruby-value str">""</span>
|
|
278
|
+
73: <span class="ruby-keyword kw">else</span>
|
|
279
|
+
74: <span class="ruby-identifier">currency</span> = <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">currency</span>
|
|
280
|
+
75: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">currency</span>
|
|
281
|
+
76: <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">getter</span>).<span class="ruby-identifier">should</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">currency</span> <span class="ruby-operator">+</span> <span class="ruby-value str">". "</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">sprintf</span>(<span class="ruby-value str">"%.2f"</span>, (<span class="ruby-identifier">cents</span>.<span class="ruby-identifier">to_f</span> <span class="ruby-operator">/</span> <span class="ruby-value">100</span>).<span class="ruby-identifier">to_s</span>).<span class="ruby-identifier">gsub</span>(<span class="ruby-value str">"."</span>, <span class="ruby-identifier">seperator</span>)
|
|
282
|
+
77: <span class="ruby-keyword kw">else</span>
|
|
283
|
+
78: <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">getter</span>).<span class="ruby-identifier">should</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">sprintf</span>(<span class="ruby-value str">"%.2f"</span>, (<span class="ruby-identifier">cents</span>.<span class="ruby-identifier">to_f</span> <span class="ruby-operator">/</span> <span class="ruby-value">100</span>).<span class="ruby-identifier">to_s</span>).<span class="ruby-identifier">gsub</span>(<span class="ruby-value str">"."</span>, <span class="ruby-identifier">seperator</span>)
|
|
284
|
+
79: <span class="ruby-keyword kw">end</span>
|
|
285
|
+
80: <span class="ruby-keyword kw">end</span>
|
|
286
|
+
81: <span class="ruby-keyword kw">end</span>
|
|
287
|
+
82: <span class="ruby-keyword kw">end</span>
|
|
288
|
+
83: <span class="ruby-keyword kw">end</span>
|
|
265
289
|
</pre>
|
|
266
290
|
</div>
|
|
267
291
|
</div>
|
|
@@ -284,22 +308,28 @@ test if the setter sets the price correctly if price is given in doubles
|
|
|
284
308
|
onclick="toggleCode('M000005-source');return false;">[Source]</a></p>
|
|
285
309
|
<div class="method-source-code" id="M000005-source">
|
|
286
310
|
<pre>
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
311
|
+
<span class="ruby-comment cmt"># File lib/acts_as_price_helper.rb, line 86</span>
|
|
312
|
+
86: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-identifier">double</span>, <span class="ruby-identifier">seperator</span>
|
|
313
|
+
87: <span class="ruby-identifier">currency</span> = <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">currency</span>
|
|
314
|
+
88: <span class="ruby-identifier">double</span> = <span class="ruby-identifier">double</span>.<span class="ruby-identifier">gsub</span>(<span class="ruby-node">"#{currency}. "</span>, <span class="ruby-value str">""</span>)
|
|
315
|
+
89: <span class="ruby-identifier">columns_in_doubles</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">setter</span><span class="ruby-operator">|</span>
|
|
316
|
+
90: <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">send</span>(<span class="ruby-node">"#{setter}="</span>, <span class="ruby-identifier">double</span>)
|
|
317
|
+
91: <span class="ruby-identifier">columns_in_cents</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">getter</span><span class="ruby-operator">|</span>
|
|
318
|
+
92: <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">getter</span>).<span class="ruby-identifier">should</span> <span class="ruby-operator">==</span> (<span class="ruby-identifier">double</span>.<span class="ruby-identifier">gsub</span>(<span class="ruby-value str">","</span>, <span class="ruby-value str">"."</span>).<span class="ruby-identifier">to_f</span> <span class="ruby-operator">*</span> <span class="ruby-value">100</span>).<span class="ruby-identifier">to_s</span>.<span class="ruby-identifier">to_i</span>
|
|
319
|
+
93: <span class="ruby-keyword kw">end</span>
|
|
320
|
+
94: <span class="ruby-identifier">columns_in_doubles</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">getter</span><span class="ruby-operator">|</span>
|
|
321
|
+
95: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">double</span> <span class="ruby-operator">==</span> <span class="ruby-value str">""</span>
|
|
322
|
+
96: <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">getter</span>).<span class="ruby-identifier">should</span> <span class="ruby-operator">==</span> <span class="ruby-value str">""</span>
|
|
323
|
+
97: <span class="ruby-keyword kw">else</span>
|
|
324
|
+
98: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">currency</span>
|
|
325
|
+
99: <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">getter</span>).<span class="ruby-identifier">should</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">currency</span> <span class="ruby-operator">+</span> <span class="ruby-value str">". "</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">sprintf</span>(<span class="ruby-value str">"%.2f"</span>, <span class="ruby-identifier">double</span>.<span class="ruby-identifier">gsub</span>(<span class="ruby-value str">","</span>, <span class="ruby-value str">"."</span>)).<span class="ruby-identifier">gsub</span>(<span class="ruby-value str">"."</span>, <span class="ruby-identifier">seperator</span>)
|
|
326
|
+
100: <span class="ruby-keyword kw">else</span>
|
|
327
|
+
101: <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">getter</span>).<span class="ruby-identifier">should</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">sprintf</span>(<span class="ruby-value str">"%.2f"</span>, <span class="ruby-identifier">double</span>.<span class="ruby-identifier">gsub</span>(<span class="ruby-value str">","</span>, <span class="ruby-value str">"."</span>)).<span class="ruby-identifier">gsub</span>(<span class="ruby-value str">"."</span>, <span class="ruby-identifier">seperator</span>)
|
|
328
|
+
102: <span class="ruby-keyword kw">end</span>
|
|
329
|
+
103: <span class="ruby-keyword kw">end</span>
|
|
330
|
+
104: <span class="ruby-keyword kw">end</span>
|
|
331
|
+
105: <span class="ruby-keyword kw">end</span>
|
|
332
|
+
106: <span class="ruby-keyword kw">end</span>
|
|
303
333
|
</pre>
|
|
304
334
|
</div>
|
|
305
335
|
</div>
|
data/rdoc/created.rid
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
Sun, 01 May 2011
|
|
1
|
+
Sun, 01 May 2011 08:58:34 -0700
|
data/rdoc/files/README_rdoc.html
CHANGED
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
</tr>
|
|
57
57
|
<tr class="top-aligned-row">
|
|
58
58
|
<td><strong>Last Update:</strong></td>
|
|
59
|
-
<td>Sun May 01 07:
|
|
59
|
+
<td>Sun May 01 07:51:31 -0700 2011</td>
|
|
60
60
|
</tr>
|
|
61
61
|
</table>
|
|
62
62
|
</div>
|
|
@@ -86,7 +86,7 @@ In your model add the following:
|
|
|
86
86
|
</p>
|
|
87
87
|
<ul>
|
|
88
88
|
<li>acts_as_price column_name, {:validates => true, :comma_seperated =>
|
|
89
|
-
true}
|
|
89
|
+
true, :currency => currency}
|
|
90
90
|
|
|
91
91
|
</li>
|
|
92
92
|
</ul>
|
|
@@ -98,8 +98,11 @@ price_per_liter.
|
|
|
98
98
|
validates is optional and add validation to the column
|
|
99
99
|
</p>
|
|
100
100
|
<p>
|
|
101
|
-
comma_seperated is
|
|
102
|
-
|
|
101
|
+
comma_seperated is optional and return the price comma seperated instead of
|
|
102
|
+
dot seperated
|
|
103
|
+
</p>
|
|
104
|
+
<p>
|
|
105
|
+
currency is optional and adds a prefix for the currency to the price.
|
|
103
106
|
</p>
|
|
104
107
|
<p>
|
|
105
108
|
This plugin creates the following getters and setters:
|
|
@@ -117,14 +120,14 @@ the price
|
|
|
117
120
|
<h1>EXAMPLES</h1>
|
|
118
121
|
<pre>
|
|
119
122
|
class Car < ActiveRecord::Base
|
|
120
|
-
acts_as_price :price, :comma_seperated => true
|
|
123
|
+
acts_as_price :price, :comma_seperated => true, :currency => "EUR"
|
|
121
124
|
end
|
|
122
125
|
</pre>
|
|
123
126
|
<p>
|
|
124
127
|
car = Car.new :price => 12999
|
|
125
128
|
</p>
|
|
126
129
|
<p>
|
|
127
|
-
car.price -> 12999,00
|
|
130
|
+
car.price -> EUR. 12999,00
|
|
128
131
|
</p>
|
|
129
132
|
<p>
|
|
130
133
|
car.price_in_cents -> 1299900
|
|
@@ -32,7 +32,7 @@ describe Car do
|
|
|
32
32
|
end
|
|
33
33
|
it "should return the price seperated by a comma" do
|
|
34
34
|
columns_in_doubles.each do |column|
|
|
35
|
-
@acts_as_price_model.send(column).should == "23995,99"
|
|
35
|
+
@acts_as_price_model.send(column).should == "EUR. 23995,99"
|
|
36
36
|
end
|
|
37
37
|
end
|
|
38
38
|
end
|
|
@@ -50,8 +50,8 @@ describe Car do
|
|
|
50
50
|
|
|
51
51
|
context "given a float as price" do
|
|
52
52
|
it "should convert it to the right price in cents" do
|
|
53
|
-
test_setter_in_doubles "25500,5", ","
|
|
54
|
-
test_setter_in_doubles "21599,05", ","
|
|
53
|
+
test_setter_in_doubles "EUR. 25500,5", ","
|
|
54
|
+
test_setter_in_doubles "EUR. 21599,05", ","
|
|
55
55
|
end
|
|
56
56
|
end
|
|
57
57
|
|
data/spec/models/car.rb
CHANGED
|
@@ -5,7 +5,7 @@ describe Car do
|
|
|
5
5
|
context "adding a price as comma seperated value" do
|
|
6
6
|
it "should return the price as comma-seperated value" do
|
|
7
7
|
car = stub_model(Car, :price => "23000,59")
|
|
8
|
-
car.price.should == "23000,59"
|
|
8
|
+
car.price.should == "EUR. 23000,59"
|
|
9
9
|
end
|
|
10
10
|
it "should return the pricce in cents" do
|
|
11
11
|
car = stub_model(Car, :price => "23000,59")
|
|
@@ -16,16 +16,23 @@ describe Car do
|
|
|
16
16
|
context "adding the price as a dot seperated value" do
|
|
17
17
|
it "should return the price as a comma seperated value" do
|
|
18
18
|
car = stub_model(Car, :price => "23000.59")
|
|
19
|
-
car.price.should == "23000,59"
|
|
19
|
+
car.price.should == "EUR. 23000,59"
|
|
20
20
|
car = stub_model(Car, :price => "23000.5")
|
|
21
|
-
car.price.should == "23000,50"
|
|
21
|
+
car.price.should == "EUR. 23000,50"
|
|
22
22
|
end
|
|
23
23
|
end
|
|
24
24
|
|
|
25
25
|
context "adding the price as an integer" do
|
|
26
26
|
it "should return the price as a comma-seperated value" do
|
|
27
27
|
car = stub_model(Car, :price => "23000")
|
|
28
|
-
car.price.should == "23000,00"
|
|
28
|
+
car.price.should == "EUR. 23000,00"
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
context "using a currency" do
|
|
33
|
+
it "should return a currency" do
|
|
34
|
+
car = stub_model(Car)
|
|
35
|
+
car.currency.should == "EUR"
|
|
29
36
|
end
|
|
30
37
|
end
|
|
31
38
|
end
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: acts_as_price
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
hash:
|
|
4
|
+
hash: 23
|
|
5
5
|
prerelease: false
|
|
6
6
|
segments:
|
|
7
7
|
- 0
|
|
8
|
-
-
|
|
9
|
-
-
|
|
10
|
-
version: 0.
|
|
8
|
+
- 2
|
|
9
|
+
- 0
|
|
10
|
+
version: 0.2.0
|
|
11
11
|
platform: ruby
|
|
12
12
|
authors:
|
|
13
13
|
- Jeroen van Ingen
|