acts_as_price 0.1.1 → 0.1.2
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 +18 -10
- data/VERSION +1 -1
- data/acts_as_price.gemspec +2 -2
- data/lib/acts_as_price.rb +11 -3
- data/lib/acts_as_price_helper.rb +29 -19
- data/rdoc/classes/ActiveRecord/Acts/Price/ClassMethods.html +39 -28
- data/rdoc/classes/ActsAsPriceHelper.html +102 -96
- data/rdoc/created.rid +1 -1
- data/rdoc/files/README_rdoc.html +31 -9
- data/rdoc/files/lib/acts_as_price_helper_rb.html +1 -1
- data/rdoc/files/lib/acts_as_price_rb.html +1 -1
- data/rdoc/fr_method_index.html +0 -1
- data/spec/car_spec.rb +13 -14
- data/spec/fueltype_spec.rb +8 -7
- data/spec/models/car.rb +1 -1
- metadata +4 -4
data/README.rdoc
CHANGED
|
@@ -7,38 +7,49 @@ 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
|
|
10
|
+
- acts_as_price column_name, {:validates => true, :comma_seperated => true}
|
|
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 also optional and return the price comma seperated instead of dot seperated
|
|
17
|
+
|
|
16
18
|
This plugin creates the following getters and setters:
|
|
17
19
|
- 'price_in_cents' and '<column_name>_in_cents' #sets and returns the price in cents
|
|
18
20
|
- 'price' and '<column_name>' #sets and returns the price
|
|
19
21
|
|
|
20
22
|
=EXAMPLES
|
|
23
|
+
class Car < ActiveRecord::Base
|
|
24
|
+
acts_as_price :price, :comma_seperated => true
|
|
25
|
+
end
|
|
26
|
+
|
|
21
27
|
car = Car.new :price => 12999
|
|
22
28
|
|
|
23
|
-
car.price
|
|
29
|
+
car.price -> 12999,00
|
|
24
30
|
|
|
25
|
-
car.price_in_cents
|
|
31
|
+
car.price_in_cents -> 1299900
|
|
26
32
|
|
|
33
|
+
class Fueltype < ActiveRecord::Base
|
|
34
|
+
acts_as_price :price_per_liter, :validates => true
|
|
35
|
+
end
|
|
36
|
+
|
|
27
37
|
fuel = Fueltype.new :price_per_liter => 1.12
|
|
28
38
|
|
|
29
|
-
fuel.price
|
|
39
|
+
fuel.price -> 1.12
|
|
30
40
|
|
|
31
|
-
fuel.price_in_cents
|
|
41
|
+
fuel.price_in_cents -> 112
|
|
32
42
|
|
|
33
|
-
fuel.price_per_liter
|
|
43
|
+
fuel.price_per_liter -> 1.12
|
|
34
44
|
|
|
35
|
-
fuel.price_per_liter_in_cents
|
|
45
|
+
fuel.price_per_liter_in_cents -> 1.12
|
|
36
46
|
|
|
37
47
|
=RSPEC INTERGRATION
|
|
38
48
|
Acts As Price comes with a helper method to automatically test the plugin using Rspec.
|
|
39
49
|
|
|
40
50
|
To use this method please add the following to your spec_helper.rb file:
|
|
41
51
|
- config.include(ActsAsPriceHelper)
|
|
52
|
+
- and if you using the gem, please also add: require 'acts_as_price_helper'
|
|
42
53
|
Now you may add the following code to your spec-files for your models:
|
|
43
54
|
context "given an valid model" do
|
|
44
55
|
it "should acts as price" do
|
|
@@ -49,9 +60,6 @@ Now you may add the following code to your spec-files for your models:
|
|
|
49
60
|
=TESTING
|
|
50
61
|
To test the plugin use the command 'rake test' or 'rspec spec/*_spec.rb' inside the dir '~/vendor/plugins/acts_as_price'
|
|
51
62
|
|
|
52
|
-
=KNOWN BUGS
|
|
53
|
-
The acts_as_price plugin does not accept comma-seperated prices at the moment.
|
|
54
|
-
|
|
55
63
|
=NOTES
|
|
56
64
|
This plugin comes with Rspec tests.
|
|
57
65
|
However this plugin assumes that you have a database configuration, it actually don't use the database for the Rspec tests.
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.1.
|
|
1
|
+
0.1.2
|
data/acts_as_price.gemspec
CHANGED
|
@@ -5,11 +5,11 @@
|
|
|
5
5
|
|
|
6
6
|
Gem::Specification.new do |s|
|
|
7
7
|
s.name = %q{acts_as_price}
|
|
8
|
-
s.version = "0.1.
|
|
8
|
+
s.version = "0.1.2"
|
|
9
9
|
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
|
11
11
|
s.authors = ["Jeroen van Ingen"]
|
|
12
|
-
s.date = %q{2011-04-
|
|
12
|
+
s.date = %q{2011-04-29}
|
|
13
13
|
s.description = %q{A specified database column acts as a price and creates on the fly methods like 'price' and 'price_in_cents'. For more information visit: http://github.com/jeroeningen/acts_as_price}
|
|
14
14
|
s.email = %q{jeroeningen@gmail.com}
|
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/acts_as_price.rb
CHANGED
|
@@ -11,12 +11,14 @@ module ActiveRecord
|
|
|
11
11
|
#
|
|
12
12
|
# Valid options:
|
|
13
13
|
# * :validates => true (performs validates_presence_of and validates_numericality_of)
|
|
14
|
+
# * :comma_seperated => true (set and get the price as a comma-seperated value)
|
|
14
15
|
def acts_as_price column_name, options = {}
|
|
15
16
|
validates column_name, :presence => true, :numericality => {:greater_than => 0} if options[:validates] == true
|
|
17
|
+
comma = options[:comma_seperated]
|
|
16
18
|
|
|
17
19
|
#setters
|
|
18
20
|
define_method("#{column_name}=") do |val|
|
|
19
|
-
super((val.to_f * 100).to_s)
|
|
21
|
+
super((val.to_s.gsub(",", ".").to_f * 100).to_s)
|
|
20
22
|
end
|
|
21
23
|
alias_method("price=", "#{column_name}=")
|
|
22
24
|
|
|
@@ -30,12 +32,18 @@ module ActiveRecord
|
|
|
30
32
|
if super == 0.0
|
|
31
33
|
""
|
|
32
34
|
else
|
|
33
|
-
|
|
35
|
+
if comma
|
|
36
|
+
sprintf("%.2f", super.to_f / 100).gsub(".", ",")
|
|
37
|
+
else
|
|
38
|
+
sprintf("%.2f", super.to_f / 100)
|
|
39
|
+
end
|
|
34
40
|
end
|
|
35
41
|
end
|
|
36
42
|
alias_method "price", column_name
|
|
37
43
|
|
|
38
|
-
define_method("price_in_cents")
|
|
44
|
+
define_method("price_in_cents") do
|
|
45
|
+
((send column_name).gsub(",", ".").to_f * 100).to_s.to_i
|
|
46
|
+
end
|
|
39
47
|
alias_method "#{column_name}_in_cents", "price_in_cents"
|
|
40
48
|
end
|
|
41
49
|
end
|
data/lib/acts_as_price_helper.rb
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
module ActsAsPriceHelper
|
|
2
|
-
# Test the acts_as_price plugin for the specified column_name and
|
|
3
|
-
#
|
|
2
|
+
# Test the acts_as_price plugin for the specified column_name and given seperator.
|
|
3
|
+
#
|
|
4
|
+
# Valid options:
|
|
5
|
+
# * model: Not just the model name, but a full object of the model
|
|
6
|
+
#
|
|
7
|
+
# EXAMPLE:
|
|
8
|
+
# * test_acts_as_price_methods :price, ",", :model => @fuel
|
|
4
9
|
# Note the you can also specify the model in a before block in your tests
|
|
5
|
-
def test_acts_as_price_methods column_name,
|
|
6
|
-
@acts_as_price_model =
|
|
10
|
+
def test_acts_as_price_methods column_name, seperator, options = {}
|
|
11
|
+
@acts_as_price_model = options[:model] if options[:model]
|
|
7
12
|
|
|
8
13
|
@column_name = column_name
|
|
9
14
|
columns_in_cents.each do |column|
|
|
@@ -14,23 +19,30 @@ module ActsAsPriceHelper
|
|
|
14
19
|
end
|
|
15
20
|
|
|
16
21
|
#normal test for setter
|
|
17
|
-
test_setter_in_cents "144"
|
|
18
|
-
test_setter_in_doubles "1.41"
|
|
22
|
+
test_setter_in_cents "144", seperator
|
|
23
|
+
test_setter_in_doubles "1.41", seperator
|
|
24
|
+
test_setter_in_doubles "1,41", seperator
|
|
19
25
|
|
|
20
26
|
#test for special cases
|
|
21
27
|
#test if 1.5 is returned as 1.50
|
|
22
|
-
test_setter_in_doubles "1.5"
|
|
28
|
+
test_setter_in_doubles "1.5", seperator
|
|
29
|
+
#test if 1,5 is returned as 1.50
|
|
30
|
+
test_setter_in_doubles "1,5", seperator
|
|
23
31
|
|
|
24
32
|
#test if float returns 2.05 correctly
|
|
25
33
|
#float can return 2.05 as 2.04 in some cases
|
|
26
|
-
test_setter_in_doubles "2.05"
|
|
34
|
+
test_setter_in_doubles "2.05", seperator
|
|
35
|
+
|
|
36
|
+
#float can return 2,05 as 2.04 in some cases
|
|
37
|
+
test_setter_in_doubles "2,05", seperator
|
|
27
38
|
|
|
28
39
|
#test an empty_setter
|
|
29
|
-
test_setter_in_cents ""
|
|
30
|
-
test_setter_in_doubles ""
|
|
40
|
+
test_setter_in_cents "", seperator
|
|
41
|
+
test_setter_in_doubles "", seperator
|
|
31
42
|
end
|
|
32
43
|
|
|
33
|
-
|
|
44
|
+
# test if the setter sets the price correctly if price is given in integers
|
|
45
|
+
def test_setter_in_cents cents, seperator
|
|
34
46
|
columns_in_cents.each do |setter|
|
|
35
47
|
@acts_as_price_model.send("#{setter}=", cents)
|
|
36
48
|
columns_in_cents.each do |getter|
|
|
@@ -40,23 +52,24 @@ module ActsAsPriceHelper
|
|
|
40
52
|
if cents == ""
|
|
41
53
|
@acts_as_price_model.send(getter).should == ""
|
|
42
54
|
else
|
|
43
|
-
@acts_as_price_model.send(getter).should == sprintf("%.2f", (cents.to_f / 100).to_s)
|
|
55
|
+
@acts_as_price_model.send(getter).should == sprintf("%.2f", (cents.to_f / 100).to_s).gsub(".", seperator)
|
|
44
56
|
end
|
|
45
57
|
end
|
|
46
58
|
end
|
|
47
59
|
end
|
|
48
60
|
|
|
49
|
-
|
|
61
|
+
# test if the setter sets the price correctly if price is given in doubles
|
|
62
|
+
def test_setter_in_doubles double, seperator
|
|
50
63
|
columns_in_doubles.each do |setter|
|
|
51
64
|
@acts_as_price_model.send("#{setter}=", double)
|
|
52
65
|
columns_in_cents.each do |getter|
|
|
53
|
-
@acts_as_price_model.send(getter).should == (double.to_f * 100).to_s.to_i
|
|
66
|
+
@acts_as_price_model.send(getter).should == (double.gsub(",", ".").to_f * 100).to_s.to_i
|
|
54
67
|
end
|
|
55
68
|
columns_in_doubles.each do |getter|
|
|
56
69
|
if double == ""
|
|
57
70
|
@acts_as_price_model.send(getter).should == ""
|
|
58
71
|
else
|
|
59
|
-
@acts_as_price_model.send(getter).should == sprintf("%.2f", double)
|
|
72
|
+
@acts_as_price_model.send(getter).should == sprintf("%.2f", double.gsub(",", ".")).gsub(".", seperator)
|
|
60
73
|
end
|
|
61
74
|
end
|
|
62
75
|
end
|
|
@@ -69,8 +82,5 @@ module ActsAsPriceHelper
|
|
|
69
82
|
def columns_in_doubles
|
|
70
83
|
[:price, @column_name]
|
|
71
84
|
end
|
|
72
|
-
|
|
73
|
-
def columns
|
|
74
|
-
columns_in_doubles + columns_in_cents
|
|
75
|
-
end
|
|
85
|
+
|
|
76
86
|
end
|
|
@@ -122,40 +122,51 @@ Valid options:
|
|
|
122
122
|
<li>:validates => true (performs validates_presence_of and
|
|
123
123
|
validates_numericality_of)
|
|
124
124
|
|
|
125
|
+
</li>
|
|
126
|
+
<li>:comma_seperated => true (set and get the price as a comma-seperated
|
|
127
|
+
value)
|
|
128
|
+
|
|
125
129
|
</li>
|
|
126
130
|
</ul>
|
|
127
131
|
<p><a class="source-toggle" href="#"
|
|
128
132
|
onclick="toggleCode('M000002-source');return false;">[Source]</a></p>
|
|
129
133
|
<div class="method-source-code" id="M000002-source">
|
|
130
134
|
<pre>
|
|
131
|
-
<span class="ruby-comment cmt"># File lib/acts_as_price.rb, line
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
135
|
+
<span class="ruby-comment cmt"># File lib/acts_as_price.rb, line 15</span>
|
|
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
|
+
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>]
|
|
139
|
+
18:
|
|
140
|
+
19: <span class="ruby-comment cmt">#setters</span>
|
|
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>
|
|
142
|
+
21: <span class="ruby-keyword kw">super</span>((<span class="ruby-identifier">val</span>.<span class="ruby-identifier">to_s</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>)
|
|
143
|
+
22: <span class="ruby-keyword kw">end</span>
|
|
144
|
+
23: <span class="ruby-identifier">alias_method</span>(<span class="ruby-value str">"price="</span>, <span class="ruby-node">"#{column_name}="</span>)
|
|
145
|
+
24:
|
|
146
|
+
25: <span class="ruby-identifier">define_method</span>(<span class="ruby-value str">"price_in_cents="</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">val</span><span class="ruby-operator">|</span>
|
|
147
|
+
26: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">send</span>(<span class="ruby-value str">"price="</span>, <span class="ruby-identifier">val</span>.<span class="ruby-identifier">to_f</span> <span class="ruby-operator">/</span> <span class="ruby-value">100</span>)
|
|
148
|
+
27: <span class="ruby-keyword kw">end</span>
|
|
149
|
+
28: <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>
|
|
150
|
+
29:
|
|
151
|
+
30: <span class="ruby-comment cmt">#getters</span>
|
|
152
|
+
31: <span class="ruby-identifier">define_method</span>(<span class="ruby-identifier">column_name</span>) <span class="ruby-keyword kw">do</span>
|
|
153
|
+
32: <span class="ruby-keyword kw">if</span> <span class="ruby-keyword kw">super</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span><span class="ruby-value">.0</span>
|
|
154
|
+
33: <span class="ruby-value str">""</span>
|
|
155
|
+
34: <span class="ruby-keyword kw">else</span>
|
|
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>)
|
|
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>)
|
|
160
|
+
39: <span class="ruby-keyword kw">end</span>
|
|
161
|
+
40: <span class="ruby-keyword kw">end</span>
|
|
162
|
+
41: <span class="ruby-keyword kw">end</span>
|
|
163
|
+
42: <span class="ruby-identifier">alias_method</span> <span class="ruby-value str">"price"</span>, <span class="ruby-identifier">column_name</span>
|
|
164
|
+
43:
|
|
165
|
+
44: <span class="ruby-identifier">define_method</span>(<span class="ruby-value str">"price_in_cents"</span>) <span class="ruby-keyword kw">do</span>
|
|
166
|
+
45: ((<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>
|
|
167
|
+
46: <span class="ruby-keyword kw">end</span>
|
|
168
|
+
47: <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>
|
|
169
|
+
48: <span class="ruby-keyword kw">end</span>
|
|
159
170
|
</pre>
|
|
160
171
|
</div>
|
|
161
172
|
</div>
|
|
@@ -80,7 +80,6 @@
|
|
|
80
80
|
<h3 class="section-bar">Methods</h3>
|
|
81
81
|
|
|
82
82
|
<div class="name-list">
|
|
83
|
-
<a href="#M000008">columns</a>
|
|
84
83
|
<a href="#M000006">columns_in_cents</a>
|
|
85
84
|
<a href="#M000007">columns_in_doubles</a>
|
|
86
85
|
<a href="#M000003">test_acts_as_price_methods</a>
|
|
@@ -107,29 +106,6 @@
|
|
|
107
106
|
<div id="methods">
|
|
108
107
|
<h3 class="section-bar">Public Instance methods</h3>
|
|
109
108
|
|
|
110
|
-
<div id="method-M000008" class="method-detail">
|
|
111
|
-
<a name="M000008"></a>
|
|
112
|
-
|
|
113
|
-
<div class="method-heading">
|
|
114
|
-
<a href="#M000008" class="method-signature">
|
|
115
|
-
<span class="method-name">columns</span><span class="method-args">()</span>
|
|
116
|
-
</a>
|
|
117
|
-
</div>
|
|
118
|
-
|
|
119
|
-
<div class="method-description">
|
|
120
|
-
<p><a class="source-toggle" href="#"
|
|
121
|
-
onclick="toggleCode('M000008-source');return false;">[Source]</a></p>
|
|
122
|
-
<div class="method-source-code" id="M000008-source">
|
|
123
|
-
<pre>
|
|
124
|
-
<span class="ruby-comment cmt"># File lib/acts_as_price_helper.rb, line 73</span>
|
|
125
|
-
73: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">columns</span>
|
|
126
|
-
74: <span class="ruby-identifier">columns_in_doubles</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">columns_in_cents</span>
|
|
127
|
-
75: <span class="ruby-keyword kw">end</span>
|
|
128
|
-
</pre>
|
|
129
|
-
</div>
|
|
130
|
-
</div>
|
|
131
|
-
</div>
|
|
132
|
-
|
|
133
109
|
<div id="method-M000006" class="method-detail">
|
|
134
110
|
<a name="M000006"></a>
|
|
135
111
|
|
|
@@ -144,10 +120,10 @@
|
|
|
144
120
|
onclick="toggleCode('M000006-source');return false;">[Source]</a></p>
|
|
145
121
|
<div class="method-source-code" id="M000006-source">
|
|
146
122
|
<pre>
|
|
147
|
-
<span class="ruby-comment cmt"># File lib/acts_as_price_helper.rb, line
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
123
|
+
<span class="ruby-comment cmt"># File lib/acts_as_price_helper.rb, line 78</span>
|
|
124
|
+
78: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">columns_in_cents</span>
|
|
125
|
+
79: [<span class="ruby-identifier">:price_in_cents</span>, <span class="ruby-node">"#{@column_name}_in_cents"</span>]
|
|
126
|
+
80: <span class="ruby-keyword kw">end</span>
|
|
151
127
|
</pre>
|
|
152
128
|
</div>
|
|
153
129
|
</div>
|
|
@@ -167,10 +143,10 @@
|
|
|
167
143
|
onclick="toggleCode('M000007-source');return false;">[Source]</a></p>
|
|
168
144
|
<div class="method-source-code" id="M000007-source">
|
|
169
145
|
<pre>
|
|
170
|
-
<span class="ruby-comment cmt"># File lib/acts_as_price_helper.rb, line
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
146
|
+
<span class="ruby-comment cmt"># File lib/acts_as_price_helper.rb, line 82</span>
|
|
147
|
+
82: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">columns_in_doubles</span>
|
|
148
|
+
83: [<span class="ruby-identifier">:price</span>, <span class="ruby-ivar">@column_name</span>]
|
|
149
|
+
84: <span class="ruby-keyword kw">end</span>
|
|
174
150
|
</pre>
|
|
175
151
|
</div>
|
|
176
152
|
</div>
|
|
@@ -181,14 +157,32 @@
|
|
|
181
157
|
|
|
182
158
|
<div class="method-heading">
|
|
183
159
|
<a href="#M000003" class="method-signature">
|
|
184
|
-
<span class="method-name">test_acts_as_price_methods</span><span class="method-args">(column_name,
|
|
160
|
+
<span class="method-name">test_acts_as_price_methods</span><span class="method-args">(column_name, seperator, options = {})</span>
|
|
185
161
|
</a>
|
|
186
162
|
</div>
|
|
187
163
|
|
|
188
164
|
<div class="method-description">
|
|
189
165
|
<p>
|
|
190
|
-
Test the acts_as_price plugin for the specified column_name and
|
|
166
|
+
Test the acts_as_price plugin for the specified column_name and given
|
|
167
|
+
seperator.
|
|
168
|
+
</p>
|
|
169
|
+
<p>
|
|
170
|
+
Valid options:
|
|
191
171
|
</p>
|
|
172
|
+
<ul>
|
|
173
|
+
<li>model: Not just the model name, but a full object of the model
|
|
174
|
+
|
|
175
|
+
</li>
|
|
176
|
+
</ul>
|
|
177
|
+
<p>
|
|
178
|
+
EXAMPLE:
|
|
179
|
+
</p>
|
|
180
|
+
<ul>
|
|
181
|
+
<li><a href="ActsAsPriceHelper.html#M000003">test_acts_as_price_methods</a>
|
|
182
|
+
:price, ",", :model => @fuel
|
|
183
|
+
|
|
184
|
+
</li>
|
|
185
|
+
</ul>
|
|
192
186
|
<p>
|
|
193
187
|
Note the you can also specify the model in a before block in your tests
|
|
194
188
|
</p>
|
|
@@ -196,34 +190,40 @@ Note the you can also specify the model in a before block in your tests
|
|
|
196
190
|
onclick="toggleCode('M000003-source');return false;">[Source]</a></p>
|
|
197
191
|
<div class="method-source-code" id="M000003-source">
|
|
198
192
|
<pre>
|
|
199
|
-
<span class="ruby-comment cmt"># File lib/acts_as_price_helper.rb, line
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
193
|
+
<span class="ruby-comment cmt"># File lib/acts_as_price_helper.rb, line 10</span>
|
|
194
|
+
10: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">test_acts_as_price_methods</span> <span class="ruby-identifier">column_name</span>, <span class="ruby-identifier">seperator</span>, <span class="ruby-identifier">options</span> = {}
|
|
195
|
+
11: <span class="ruby-ivar">@acts_as_price_model</span> = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:model</span>] <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:model</span>]
|
|
196
|
+
12:
|
|
197
|
+
13: <span class="ruby-ivar">@column_name</span> = <span class="ruby-identifier">column_name</span>
|
|
198
|
+
14: <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">column</span><span class="ruby-operator">|</span>
|
|
199
|
+
15: <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">column</span>).<span class="ruby-identifier">should</span> <span class="ruby-operator">==</span> <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">price_in_cents</span>
|
|
200
|
+
16: <span class="ruby-keyword kw">end</span>
|
|
201
|
+
17: <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">column</span><span class="ruby-operator">|</span>
|
|
202
|
+
18: <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">column</span>).<span class="ruby-identifier">should</span> <span class="ruby-operator">==</span> <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">price</span>
|
|
203
|
+
19: <span class="ruby-keyword kw">end</span>
|
|
204
|
+
20:
|
|
205
|
+
21: <span class="ruby-comment cmt">#normal test for setter</span>
|
|
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-identifier">test_setter_in_doubles</span> <span class="ruby-value str">"1.41"</span>, <span class="ruby-identifier">seperator</span>
|
|
208
|
+
24: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-value str">"1,41"</span>, <span class="ruby-identifier">seperator</span>
|
|
209
|
+
25:
|
|
210
|
+
26: <span class="ruby-comment cmt">#test for special cases</span>
|
|
211
|
+
27: <span class="ruby-comment cmt">#test if 1.5 is returned as 1.50</span>
|
|
212
|
+
28: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-value str">"1.5"</span>, <span class="ruby-identifier">seperator</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-value str">"1,5"</span>, <span class="ruby-identifier">seperator</span>
|
|
215
|
+
31:
|
|
216
|
+
32: <span class="ruby-comment cmt">#test if float returns 2.05 correctly </span>
|
|
217
|
+
33: <span class="ruby-comment cmt">#float can return 2.05 as 2.04 in some cases</span>
|
|
218
|
+
34: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-value str">"2.05"</span>, <span class="ruby-identifier">seperator</span>
|
|
219
|
+
35:
|
|
220
|
+
36: <span class="ruby-comment cmt">#float can return 2,05 as 2.04 in some cases</span>
|
|
221
|
+
37: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-value str">"2,05"</span>, <span class="ruby-identifier">seperator</span>
|
|
222
|
+
38:
|
|
223
|
+
39: <span class="ruby-comment cmt">#test an empty_setter</span>
|
|
224
|
+
40: <span class="ruby-identifier">test_setter_in_cents</span> <span class="ruby-value str">""</span>, <span class="ruby-identifier">seperator</span>
|
|
225
|
+
41: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-value str">""</span>, <span class="ruby-identifier">seperator</span>
|
|
226
|
+
42: <span class="ruby-keyword kw">end</span>
|
|
227
227
|
</pre>
|
|
228
228
|
</div>
|
|
229
229
|
</div>
|
|
@@ -234,31 +234,34 @@ Note the you can also specify the model in a before block in your tests
|
|
|
234
234
|
|
|
235
235
|
<div class="method-heading">
|
|
236
236
|
<a href="#M000004" class="method-signature">
|
|
237
|
-
<span class="method-name">test_setter_in_cents</span><span class="method-args">(cents)</span>
|
|
237
|
+
<span class="method-name">test_setter_in_cents</span><span class="method-args">(cents, seperator)</span>
|
|
238
238
|
</a>
|
|
239
239
|
</div>
|
|
240
240
|
|
|
241
241
|
<div class="method-description">
|
|
242
|
+
<p>
|
|
243
|
+
test if the setter sets the price correctly if price is given in integers
|
|
244
|
+
</p>
|
|
242
245
|
<p><a class="source-toggle" href="#"
|
|
243
246
|
onclick="toggleCode('M000004-source');return false;">[Source]</a></p>
|
|
244
247
|
<div class="method-source-code" id="M000004-source">
|
|
245
248
|
<pre>
|
|
246
|
-
<span class="ruby-comment cmt"># File lib/acts_as_price_helper.rb, line
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
249
|
+
<span class="ruby-comment cmt"># File lib/acts_as_price_helper.rb, line 45</span>
|
|
250
|
+
45: <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>
|
|
251
|
+
46: <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>
|
|
252
|
+
47: <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>)
|
|
253
|
+
48: <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>
|
|
254
|
+
49: <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>
|
|
255
|
+
50: <span class="ruby-keyword kw">end</span>
|
|
256
|
+
51: <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>
|
|
257
|
+
52: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">cents</span> <span class="ruby-operator">==</span> <span class="ruby-value str">""</span>
|
|
258
|
+
53: <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>
|
|
259
|
+
54: <span class="ruby-keyword kw">else</span>
|
|
260
|
+
55: <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>)
|
|
261
|
+
56: <span class="ruby-keyword kw">end</span>
|
|
262
|
+
57: <span class="ruby-keyword kw">end</span>
|
|
263
|
+
58: <span class="ruby-keyword kw">end</span>
|
|
264
|
+
59: <span class="ruby-keyword kw">end</span>
|
|
262
265
|
</pre>
|
|
263
266
|
</div>
|
|
264
267
|
</div>
|
|
@@ -269,31 +272,34 @@ Note the you can also specify the model in a before block in your tests
|
|
|
269
272
|
|
|
270
273
|
<div class="method-heading">
|
|
271
274
|
<a href="#M000005" class="method-signature">
|
|
272
|
-
<span class="method-name">test_setter_in_doubles</span><span class="method-args">(double)</span>
|
|
275
|
+
<span class="method-name">test_setter_in_doubles</span><span class="method-args">(double, seperator)</span>
|
|
273
276
|
</a>
|
|
274
277
|
</div>
|
|
275
278
|
|
|
276
279
|
<div class="method-description">
|
|
280
|
+
<p>
|
|
281
|
+
test if the setter sets the price correctly if price is given in doubles
|
|
282
|
+
</p>
|
|
277
283
|
<p><a class="source-toggle" href="#"
|
|
278
284
|
onclick="toggleCode('M000005-source');return false;">[Source]</a></p>
|
|
279
285
|
<div class="method-source-code" id="M000005-source">
|
|
280
286
|
<pre>
|
|
281
|
-
<span class="ruby-comment cmt"># File lib/acts_as_price_helper.rb, line
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
287
|
+
<span class="ruby-comment cmt"># File lib/acts_as_price_helper.rb, line 62</span>
|
|
288
|
+
62: <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>
|
|
289
|
+
63: <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>
|
|
290
|
+
64: <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>)
|
|
291
|
+
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">getter</span><span class="ruby-operator">|</span>
|
|
292
|
+
66: <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>
|
|
293
|
+
67: <span class="ruby-keyword kw">end</span>
|
|
294
|
+
68: <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>
|
|
295
|
+
69: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">double</span> <span class="ruby-operator">==</span> <span class="ruby-value str">""</span>
|
|
296
|
+
70: <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>
|
|
297
|
+
71: <span class="ruby-keyword kw">else</span>
|
|
298
|
+
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-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>)
|
|
299
|
+
73: <span class="ruby-keyword kw">end</span>
|
|
300
|
+
74: <span class="ruby-keyword kw">end</span>
|
|
301
|
+
75: <span class="ruby-keyword kw">end</span>
|
|
302
|
+
76: <span class="ruby-keyword kw">end</span>
|
|
297
303
|
</pre>
|
|
298
304
|
</div>
|
|
299
305
|
</div>
|
data/rdoc/created.rid
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
Fri, 29 Apr 2011 16:03:45 -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>
|
|
59
|
+
<td>Fri Apr 29 15:27:33 -0700 2011</td>
|
|
60
60
|
</tr>
|
|
61
61
|
</table>
|
|
62
62
|
</div>
|
|
@@ -85,15 +85,23 @@ NOTE THAT THE COLUMN MUST BE OF THE TYPE INTEGER.
|
|
|
85
85
|
In your model add the following:
|
|
86
86
|
</p>
|
|
87
87
|
<ul>
|
|
88
|
-
<li>acts_as_price
|
|
88
|
+
<li>acts_as_price column_name, {:validates => true, :comma_seperated =>
|
|
89
|
+
true}
|
|
89
90
|
|
|
90
91
|
</li>
|
|
91
92
|
</ul>
|
|
92
93
|
<p>
|
|
93
|
-
|
|
94
|
+
column_name is the name of your database column e.g. price or
|
|
94
95
|
price_per_liter.
|
|
95
96
|
</p>
|
|
96
97
|
<p>
|
|
98
|
+
validates is optional and add validation to the column
|
|
99
|
+
</p>
|
|
100
|
+
<p>
|
|
101
|
+
comma_seperated is also optional and return the price comma seperated
|
|
102
|
+
instead of dot seperated
|
|
103
|
+
</p>
|
|
104
|
+
<p>
|
|
97
105
|
This plugin creates the following getters and setters:
|
|
98
106
|
</p>
|
|
99
107
|
<ul>
|
|
@@ -107,29 +115,39 @@ the price
|
|
|
107
115
|
</li>
|
|
108
116
|
</ul>
|
|
109
117
|
<h1>EXAMPLES</h1>
|
|
118
|
+
<pre>
|
|
119
|
+
class Car < ActiveRecord::Base
|
|
120
|
+
acts_as_price :price, :comma_seperated => true
|
|
121
|
+
end
|
|
122
|
+
</pre>
|
|
110
123
|
<p>
|
|
111
124
|
car = Car.new :price => 12999
|
|
112
125
|
</p>
|
|
113
126
|
<p>
|
|
114
|
-
car.price 12999
|
|
127
|
+
car.price -> 12999,00
|
|
115
128
|
</p>
|
|
116
129
|
<p>
|
|
117
|
-
car.price_in_cents 1299900
|
|
130
|
+
car.price_in_cents -> 1299900
|
|
118
131
|
</p>
|
|
132
|
+
<pre>
|
|
133
|
+
class Fueltype < ActiveRecord::Base
|
|
134
|
+
acts_as_price :price_per_liter, :validates => true
|
|
135
|
+
end
|
|
136
|
+
</pre>
|
|
119
137
|
<p>
|
|
120
138
|
fuel = Fueltype.new :price_per_liter => 1.12
|
|
121
139
|
</p>
|
|
122
140
|
<p>
|
|
123
|
-
fuel.price 1.12
|
|
141
|
+
fuel.price -> 1.12
|
|
124
142
|
</p>
|
|
125
143
|
<p>
|
|
126
|
-
fuel.price_in_cents 112
|
|
144
|
+
fuel.price_in_cents -> 112
|
|
127
145
|
</p>
|
|
128
146
|
<p>
|
|
129
|
-
fuel.price_per_liter 1.12
|
|
147
|
+
fuel.price_per_liter -> 1.12
|
|
130
148
|
</p>
|
|
131
149
|
<p>
|
|
132
|
-
fuel.price_per_liter_in_cents 1.12
|
|
150
|
+
fuel.price_per_liter_in_cents -> 1.12
|
|
133
151
|
</p>
|
|
134
152
|
<h1>RSPEC INTERGRATION</h1>
|
|
135
153
|
<p>
|
|
@@ -142,6 +160,10 @@ To use this method please add the following to your spec_helper.rb file:
|
|
|
142
160
|
<ul>
|
|
143
161
|
<li>config.include(ActsAsPriceHelper)
|
|
144
162
|
|
|
163
|
+
</li>
|
|
164
|
+
<li>and if you using the gem, please also add: require
|
|
165
|
+
‘acts_as_price_helper‘
|
|
166
|
+
|
|
145
167
|
</li>
|
|
146
168
|
</ul>
|
|
147
169
|
<p>
|
data/rdoc/fr_method_index.html
CHANGED
|
@@ -21,7 +21,6 @@
|
|
|
21
21
|
<h1 class="section-bar">Methods</h1>
|
|
22
22
|
<div id="index-entries">
|
|
23
23
|
<a href="classes/ActiveRecord/Acts/Price/ClassMethods.html#M000002">acts_as_price (ActiveRecord::Acts::Price::ClassMethods)</a><br />
|
|
24
|
-
<a href="classes/ActsAsPriceHelper.html#M000008">columns (ActsAsPriceHelper)</a><br />
|
|
25
24
|
<a href="classes/ActsAsPriceHelper.html#M000006">columns_in_cents (ActsAsPriceHelper)</a><br />
|
|
26
25
|
<a href="classes/ActsAsPriceHelper.html#M000007">columns_in_doubles (ActsAsPriceHelper)</a><br />
|
|
27
26
|
<a href="classes/ActiveRecord/Acts/Price.html#M000001">included (ActiveRecord::Acts::Price)</a><br />
|
data/spec/car_spec.rb
CHANGED
|
@@ -3,7 +3,7 @@ require "#{File.dirname(__FILE__)}/spec_helper"
|
|
|
3
3
|
describe Car do
|
|
4
4
|
before(:each) do
|
|
5
5
|
@column_name = :price
|
|
6
|
-
@acts_as_price_model = stub_model(Car, :brand => "Ford", :cartype => "Focus", :price => "23995
|
|
6
|
+
@acts_as_price_model = stub_model(Car, :brand => "Ford", :cartype => "Focus", :price => "23995,99")
|
|
7
7
|
end
|
|
8
8
|
|
|
9
9
|
context "given an empty car" do
|
|
@@ -30,36 +30,35 @@ describe Car do
|
|
|
30
30
|
@acts_as_price_model.send(column).should == 2399599
|
|
31
31
|
end
|
|
32
32
|
end
|
|
33
|
-
it "should return the price" do
|
|
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
|
|
35
|
+
@acts_as_price_model.send(column).should == "23995,99"
|
|
36
36
|
end
|
|
37
37
|
end
|
|
38
38
|
end
|
|
39
39
|
|
|
40
|
-
context "set the price for hundred different prices" do
|
|
41
|
-
2200000.upto(
|
|
42
|
-
it "should set the price in cents and return it correctly" do
|
|
43
|
-
test_setter_in_cents i.to_s
|
|
40
|
+
context "set the price for hundred different prices to test if you got no floating point problems" do
|
|
41
|
+
2200000.upto(2200099) do |i|
|
|
42
|
+
it "should set the price in cents and return it correctly (seperated by comma)" do
|
|
43
|
+
test_setter_in_cents i.to_s, ","
|
|
44
44
|
end
|
|
45
|
-
it "should set the price and return it correctly" do
|
|
46
|
-
test_setter_in_doubles sprintf("%.2f", i.to_f / 100)
|
|
45
|
+
it "should set the price and return it correctly (seperated by comma)" do
|
|
46
|
+
test_setter_in_doubles sprintf("%.2f", i.to_f / 100).gsub(".", ","), ","
|
|
47
47
|
end
|
|
48
48
|
end
|
|
49
|
-
|
|
50
49
|
end
|
|
51
50
|
|
|
52
51
|
context "given a float as price" do
|
|
53
52
|
it "should convert it to the right price in cents" do
|
|
54
|
-
test_setter_in_doubles "25500
|
|
55
|
-
test_setter_in_doubles "21599
|
|
53
|
+
test_setter_in_doubles "25500,5", ","
|
|
54
|
+
test_setter_in_doubles "21599,05", ","
|
|
56
55
|
end
|
|
57
56
|
end
|
|
58
57
|
|
|
59
58
|
context "given the price is zero" do
|
|
60
59
|
it "should return an empty price per liter" do
|
|
61
|
-
test_setter_in_cents ""
|
|
62
|
-
test_setter_in_doubles ""
|
|
60
|
+
test_setter_in_cents "", ","
|
|
61
|
+
test_setter_in_doubles "", ","
|
|
63
62
|
end
|
|
64
63
|
end
|
|
65
64
|
end
|
data/spec/fueltype_spec.rb
CHANGED
|
@@ -4,6 +4,7 @@ describe Fueltype do
|
|
|
4
4
|
before(:each) do
|
|
5
5
|
@column_name = :price_per_liter
|
|
6
6
|
@acts_as_price_model = stub_model(Fueltype, :name => "Euro 95", :price_per_liter => "1.55")
|
|
7
|
+
@seperator = "."
|
|
7
8
|
end
|
|
8
9
|
|
|
9
10
|
context "given an empty fueltype" do
|
|
@@ -37,28 +38,28 @@ describe Fueltype do
|
|
|
37
38
|
end
|
|
38
39
|
end
|
|
39
40
|
|
|
40
|
-
context "set the price per liter for hundred different prices" do
|
|
41
|
+
context "set the price per liter for hundred different prices to test if you got no floating point problems" do
|
|
41
42
|
1.upto(100).each do |i|
|
|
42
43
|
it "should set the price per liter in cents and return it correctly" do
|
|
43
|
-
test_setter_in_cents i.to_s
|
|
44
|
+
test_setter_in_cents i.to_s, @seperator
|
|
44
45
|
end
|
|
45
46
|
it "should set the price per liter and return it correctly" do
|
|
46
|
-
test_setter_in_doubles sprintf("%.2f", i.to_f / 100)
|
|
47
|
+
test_setter_in_doubles sprintf("%.2f", i.to_f / 100), @seperator
|
|
47
48
|
end
|
|
48
49
|
end
|
|
49
50
|
end
|
|
50
51
|
|
|
51
52
|
context "given a float as price per liter" do
|
|
52
53
|
it "should convert it to the right price in cents" do
|
|
53
|
-
test_setter_in_doubles "1.5"
|
|
54
|
-
test_setter_in_doubles "2.05"
|
|
54
|
+
test_setter_in_doubles "1.5", @seperator
|
|
55
|
+
test_setter_in_doubles "2.05", @seperator
|
|
55
56
|
end
|
|
56
57
|
end
|
|
57
58
|
|
|
58
59
|
context "given the price is zero" do
|
|
59
60
|
it "should return an empty price per liter" do
|
|
60
|
-
test_setter_in_cents ""
|
|
61
|
-
test_setter_in_doubles ""
|
|
61
|
+
test_setter_in_cents "", @seperator
|
|
62
|
+
test_setter_in_doubles "", @seperator
|
|
62
63
|
end
|
|
63
64
|
end
|
|
64
65
|
end
|
data/spec/models/car.rb
CHANGED
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: 31
|
|
5
5
|
prerelease: false
|
|
6
6
|
segments:
|
|
7
7
|
- 0
|
|
8
8
|
- 1
|
|
9
|
-
-
|
|
10
|
-
version: 0.1.
|
|
9
|
+
- 2
|
|
10
|
+
version: 0.1.2
|
|
11
11
|
platform: ruby
|
|
12
12
|
authors:
|
|
13
13
|
- Jeroen van Ingen
|
|
@@ -15,7 +15,7 @@ autorequire:
|
|
|
15
15
|
bindir: bin
|
|
16
16
|
cert_chain: []
|
|
17
17
|
|
|
18
|
-
date: 2011-04-
|
|
18
|
+
date: 2011-04-29 00:00:00 -07:00
|
|
19
19
|
default_executable:
|
|
20
20
|
dependencies:
|
|
21
21
|
- !ruby/object:Gem::Dependency
|