Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

acts as price now works with comma seperated values

  • Loading branch information...
commit c98e63e1fe9226f4ef4c624057c5c0031ada3146 1 parent 9661219
Jeroen van Ingen authored
28 README.rdoc
View
@@ -7,38 +7,49 @@ Create a column in the table of your database which you want to acts as a price.
NOTE THAT THE COLUMN MUST BE OF THE TYPE INTEGER.
In your model add the following:
-- acts_as_price column_name, :validates = true
+- acts_as_price column_name, {:validates => true, :comma_seperated => true}
column_name is the name of your database column e.g. price or price_per_liter.
validates is optional and add validation to the column
+comma_seperated is also optional and return the price comma seperated instead of dot seperated
+
This plugin creates the following getters and setters:
- 'price_in_cents' and '<column_name>_in_cents' #sets and returns the price in cents
- 'price' and '<column_name>' #sets and returns the price
=EXAMPLES
+ class Car < ActiveRecord::Base
+ acts_as_price :price, :comma_seperated => true
+ end
+
car = Car.new :price => 12999
-car.price #12999.00
+car.price -> 12999,00
-car.price_in_cents #1299900
+car.price_in_cents -> 1299900
+ class Fueltype < ActiveRecord::Base
+ acts_as_price :price_per_liter, :validates => true
+ end
+
fuel = Fueltype.new :price_per_liter => 1.12
-fuel.price #1.12
+fuel.price -> 1.12
-fuel.price_in_cents #112
+fuel.price_in_cents -> 112
-fuel.price_per_liter #1.12
+fuel.price_per_liter -> 1.12
-fuel.price_per_liter_in_cents #1.12
+fuel.price_per_liter_in_cents -> 1.12
=RSPEC INTERGRATION
Acts As Price comes with a helper method to automatically test the plugin using Rspec.
To use this method please add the following to your spec_helper.rb file:
- config.include(ActsAsPriceHelper)
+- and if you using the gem, please also add: require 'acts_as_price_helper'
Now you may add the following code to your spec-files for your models:
context "given an valid model" do
it "should acts as price" do
@@ -49,9 +60,6 @@ Now you may add the following code to your spec-files for your models:
=TESTING
To test the plugin use the command 'rake test' or 'rspec spec/*_spec.rb' inside the dir '~/vendor/plugins/acts_as_price'
-=KNOWN BUGS
-The acts_as_price plugin does not accept comma-seperated prices at the moment.
-
=NOTES
This plugin comes with Rspec tests.
However this plugin assumes that you have a database configuration, it actually don't use the database for the Rspec tests.
14 lib/acts_as_price.rb
View
@@ -11,12 +11,14 @@ module ClassMethods
#
# Valid options:
# * :validates => true (performs validates_presence_of and validates_numericality_of)
+ # * :comma_seperated => true (set and get the price as a comma-seperated value)
def acts_as_price column_name, options = {}
validates column_name, :presence => true, :numericality => {:greater_than => 0} if options[:validates] == true
+ comma = options[:comma_seperated]
#setters
define_method("#{column_name}=") do |val|
- super((val.to_f * 100).to_s)
+ super((val.to_s.gsub(",", ".").to_f * 100).to_s)
end
alias_method("price=", "#{column_name}=")
@@ -30,12 +32,18 @@ def acts_as_price column_name, options = {}
if super == 0.0
""
else
- sprintf("%.2f", super.to_f / 100)
+ if comma
+ sprintf("%.2f", super.to_f / 100).gsub(".", ",")
+ else
+ sprintf("%.2f", super.to_f / 100)
+ end
end
end
alias_method "price", column_name
- define_method("price_in_cents") {((send column_name).to_f * 100).to_s.to_i}
+ define_method("price_in_cents") do
+ ((send column_name).gsub(",", ".").to_f * 100).to_s.to_i
+ end
alias_method "#{column_name}_in_cents", "price_in_cents"
end
end
21 lib/acts_as_price_helper.rb
View
@@ -30,7 +30,8 @@ def test_acts_as_price_methods column_name, acts_as_price_model = nil
test_setter_in_doubles ""
end
- def test_setter_in_cents cents
+ # test if the setter sets the price correctly if price is given in integers
+ def test_setter_in_cents cents, seperator
columns_in_cents.each do |setter|
@acts_as_price_model.send("#{setter}=", cents)
columns_in_cents.each do |getter|
@@ -40,23 +41,24 @@ def test_setter_in_cents cents
if cents == ""
@acts_as_price_model.send(getter).should == ""
else
- @acts_as_price_model.send(getter).should == sprintf("%.2f", (cents.to_f / 100).to_s)
+ @acts_as_price_model.send(getter).should == sprintf("%.2f", (cents.to_f / 100).to_s).gsub(".", seperator)
end
end
end
end
- def test_setter_in_doubles double
+ # test if the setter sets the price correctly if price is given in doubles
+ def test_setter_in_doubles double, seperator
columns_in_doubles.each do |setter|
@acts_as_price_model.send("#{setter}=", double)
columns_in_cents.each do |getter|
- @acts_as_price_model.send(getter).should == (double.to_f * 100).to_s.to_i
+ @acts_as_price_model.send(getter).should == (double.gsub(",", ".").to_f * 100).to_s.to_i
end
columns_in_doubles.each do |getter|
if double == ""
@acts_as_price_model.send(getter).should == ""
else
- @acts_as_price_model.send(getter).should == sprintf("%.2f", double)
+ @acts_as_price_model.send(getter).should == sprintf("%.2f", double.gsub(",", ".")).gsub(".", seperator)
end
end
end
@@ -69,8 +71,9 @@ def columns_in_cents
def columns_in_doubles
[:price, @column_name]
end
-
- def columns
- columns_in_doubles + columns_in_cents
- end
+
+# function not used
+# def columns
+# columns_in_doubles + columns_in_cents
+# end
end
67 rdoc/classes/ActiveRecord/Acts/Price/ClassMethods.html
View
@@ -123,39 +123,50 @@ <h3 class="section-bar">Public Instance methods</h3>
validates_numericality_of)
</li>
+<li>:comma_seperated =&gt; true (set and get the price as a comma-seperated
+value)
+
+</li>
</ul>
<p><a class="source-toggle" href="#"
onclick="toggleCode('M000002-source');return false;">[Source]</a></p>
<div class="method-source-code" id="M000002-source">
<pre>
- <span class="ruby-comment cmt"># File lib/acts_as_price.rb, line 14</span>
-14: <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> = {}
-15: <span class="ruby-identifier">validates</span> <span class="ruby-identifier">column_name</span>, <span class="ruby-identifier">:presence</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword kw">true</span>, <span class="ruby-identifier">:numericality</span> =<span class="ruby-operator">&gt;</span> {<span class="ruby-identifier">:greater_than</span> =<span class="ruby-operator">&gt;</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>
-16:
-17: <span class="ruby-comment cmt">#setters</span>
-18: <span class="ruby-identifier">define_method</span>(<span class="ruby-node">&quot;#{column_name}=&quot;</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">val</span><span class="ruby-operator">|</span>
-19: <span class="ruby-keyword kw">super</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>).<span class="ruby-identifier">to_s</span>)
-20: <span class="ruby-keyword kw">end</span>
-21: <span class="ruby-identifier">alias_method</span>(<span class="ruby-value str">&quot;price=&quot;</span>, <span class="ruby-node">&quot;#{column_name}=&quot;</span>)
-22:
-23: <span class="ruby-identifier">define_method</span>(<span class="ruby-value str">&quot;price_in_cents=&quot;</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">val</span><span class="ruby-operator">|</span>
-24: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">send</span>(<span class="ruby-value str">&quot;price=&quot;</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>)
-25: <span class="ruby-keyword kw">end</span>
-26: <span class="ruby-identifier">alias_method</span> <span class="ruby-node">&quot;#{column_name}_in_cents=&quot;</span>, <span class="ruby-value str">&quot;price_in_cents=&quot;</span>
-27:
-28: <span class="ruby-comment cmt">#getters</span>
-29: <span class="ruby-identifier">define_method</span>(<span class="ruby-identifier">column_name</span>) <span class="ruby-keyword kw">do</span>
-30: <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>
-31: <span class="ruby-value str">&quot;&quot;</span>
-32: <span class="ruby-keyword kw">else</span>
-33: <span class="ruby-identifier">sprintf</span>(<span class="ruby-value str">&quot;%.2f&quot;</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>)
-34: <span class="ruby-keyword kw">end</span>
-35: <span class="ruby-keyword kw">end</span>
-36: <span class="ruby-identifier">alias_method</span> <span class="ruby-value str">&quot;price&quot;</span>, <span class="ruby-identifier">column_name</span>
-37:
-38: <span class="ruby-identifier">define_method</span>(<span class="ruby-value str">&quot;price_in_cents&quot;</span>) {((<span class="ruby-identifier">send</span> <span class="ruby-identifier">column_name</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>}
-39: <span class="ruby-identifier">alias_method</span> <span class="ruby-node">&quot;#{column_name}_in_cents&quot;</span>, <span class="ruby-value str">&quot;price_in_cents&quot;</span>
-40: <span class="ruby-keyword kw">end</span>
+ <span class="ruby-comment cmt"># File lib/acts_as_price.rb, line 15</span>
+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> = {}
+16: <span class="ruby-identifier">validates</span> <span class="ruby-identifier">column_name</span>, <span class="ruby-identifier">:presence</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-keyword kw">true</span>, <span class="ruby-identifier">:numericality</span> =<span class="ruby-operator">&gt;</span> {<span class="ruby-identifier">:greater_than</span> =<span class="ruby-operator">&gt;</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>
+17: <span class="ruby-identifier">comma</span> = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:comma_seperated</span>]
+18:
+19: <span class="ruby-comment cmt">#setters</span>
+20: <span class="ruby-identifier">define_method</span>(<span class="ruby-node">&quot;#{column_name}=&quot;</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">val</span><span class="ruby-operator">|</span>
+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">&quot;,&quot;</span>, <span class="ruby-value str">&quot;.&quot;</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>)
+22: <span class="ruby-keyword kw">end</span>
+23: <span class="ruby-identifier">alias_method</span>(<span class="ruby-value str">&quot;price=&quot;</span>, <span class="ruby-node">&quot;#{column_name}=&quot;</span>)
+24:
+25: <span class="ruby-identifier">define_method</span>(<span class="ruby-value str">&quot;price_in_cents=&quot;</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">val</span><span class="ruby-operator">|</span>
+26: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">send</span>(<span class="ruby-value str">&quot;price=&quot;</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>)
+27: <span class="ruby-keyword kw">end</span>
+28: <span class="ruby-identifier">alias_method</span> <span class="ruby-node">&quot;#{column_name}_in_cents=&quot;</span>, <span class="ruby-value str">&quot;price_in_cents=&quot;</span>
+29:
+30: <span class="ruby-comment cmt">#getters</span>
+31: <span class="ruby-identifier">define_method</span>(<span class="ruby-identifier">column_name</span>) <span class="ruby-keyword kw">do</span>
+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>
+33: <span class="ruby-value str">&quot;&quot;</span>
+34: <span class="ruby-keyword kw">else</span>
+35: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">comma</span>
+36: <span class="ruby-identifier">sprintf</span>(<span class="ruby-value str">&quot;%.2f&quot;</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">&quot;.&quot;</span>, <span class="ruby-value str">&quot;,&quot;</span>)
+37: <span class="ruby-keyword kw">else</span>
+38: <span class="ruby-identifier">sprintf</span>(<span class="ruby-value str">&quot;%.2f&quot;</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>)
+39: <span class="ruby-keyword kw">end</span>
+40: <span class="ruby-keyword kw">end</span>
+41: <span class="ruby-keyword kw">end</span>
+42: <span class="ruby-identifier">alias_method</span> <span class="ruby-value str">&quot;price&quot;</span>, <span class="ruby-identifier">column_name</span>
+43:
+44: <span class="ruby-identifier">define_method</span>(<span class="ruby-value str">&quot;price_in_cents&quot;</span>) <span class="ruby-keyword kw">do</span>
+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">&quot;,&quot;</span>, <span class="ruby-value str">&quot;.&quot;</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>
+46: <span class="ruby-keyword kw">end</span>
+47: <span class="ruby-identifier">alias_method</span> <span class="ruby-node">&quot;#{column_name}_in_cents&quot;</span>, <span class="ruby-value str">&quot;price_in_cents&quot;</span>
+48: <span class="ruby-keyword kw">end</span>
</pre>
</div>
</div>
114 rdoc/classes/ActsAsPriceHelper.html
View
@@ -80,7 +80,6 @@
<h3 class="section-bar">Methods</h3>
<div class="name-list">
- <a href="#M000008">columns</a>&nbsp;&nbsp;
<a href="#M000006">columns_in_cents</a>&nbsp;&nbsp;
<a href="#M000007">columns_in_doubles</a>&nbsp;&nbsp;
<a href="#M000003">test_acts_as_price_methods</a>&nbsp;&nbsp;
@@ -107,29 +106,6 @@ <h3 class="section-bar">Methods</h3>
<div id="methods">
<h3 class="section-bar">Public Instance methods</h3>
- <div id="method-M000008" class="method-detail">
- <a name="M000008"></a>
-
- <div class="method-heading">
- <a href="#M000008" class="method-signature">
- <span class="method-name">columns</span><span class="method-args">()</span>
- </a>
- </div>
-
- <div class="method-description">
- <p><a class="source-toggle" href="#"
- onclick="toggleCode('M000008-source');return false;">[Source]</a></p>
- <div class="method-source-code" id="M000008-source">
-<pre>
- <span class="ruby-comment cmt"># File lib/acts_as_price_helper.rb, line 73</span>
-73: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">columns</span>
-74: <span class="ruby-identifier">columns_in_doubles</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">columns_in_cents</span>
-75: <span class="ruby-keyword kw">end</span>
-</pre>
- </div>
- </div>
- </div>
-
<div id="method-M000006" class="method-detail">
<a name="M000006"></a>
@@ -144,10 +120,10 @@ <h3 class="section-bar">Public Instance methods</h3>
onclick="toggleCode('M000006-source');return false;">[Source]</a></p>
<div class="method-source-code" id="M000006-source">
<pre>
- <span class="ruby-comment cmt"># File lib/acts_as_price_helper.rb, line 65</span>
-65: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">columns_in_cents</span>
-66: [<span class="ruby-identifier">:price_in_cents</span>, <span class="ruby-node">&quot;#{@column_name}_in_cents&quot;</span>]
-67: <span class="ruby-keyword kw">end</span>
+ <span class="ruby-comment cmt"># File lib/acts_as_price_helper.rb, line 67</span>
+67: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">columns_in_cents</span>
+68: [<span class="ruby-identifier">:price_in_cents</span>, <span class="ruby-node">&quot;#{@column_name}_in_cents&quot;</span>]
+69: <span class="ruby-keyword kw">end</span>
</pre>
</div>
</div>
@@ -167,10 +143,10 @@ <h3 class="section-bar">Public Instance methods</h3>
onclick="toggleCode('M000007-source');return false;">[Source]</a></p>
<div class="method-source-code" id="M000007-source">
<pre>
- <span class="ruby-comment cmt"># File lib/acts_as_price_helper.rb, line 69</span>
-69: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">columns_in_doubles</span>
-70: [<span class="ruby-identifier">:price</span>, <span class="ruby-ivar">@column_name</span>]
-71: <span class="ruby-keyword kw">end</span>
+ <span class="ruby-comment cmt"># File lib/acts_as_price_helper.rb, line 71</span>
+71: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">columns_in_doubles</span>
+72: [<span class="ruby-identifier">:price</span>, <span class="ruby-ivar">@column_name</span>]
+73: <span class="ruby-keyword kw">end</span>
</pre>
</div>
</div>
@@ -234,31 +210,34 @@ <h3 class="section-bar">Public Instance methods</h3>
<div class="method-heading">
<a href="#M000004" class="method-signature">
- <span class="method-name">test_setter_in_cents</span><span class="method-args">(cents)</span>
+ <span class="method-name">test_setter_in_cents</span><span class="method-args">(cents, seperator)</span>
</a>
</div>
<div class="method-description">
+ <p>
+test if the setter sets the price correctly if price is given in integers
+</p>
<p><a class="source-toggle" href="#"
onclick="toggleCode('M000004-source');return false;">[Source]</a></p>
<div class="method-source-code" id="M000004-source">
<pre>
- <span class="ruby-comment cmt"># File lib/acts_as_price_helper.rb, line 33</span>
-33: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">test_setter_in_cents</span> <span class="ruby-identifier">cents</span>
-34: <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>
-35: <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">send</span>(<span class="ruby-node">&quot;#{setter}=&quot;</span>, <span class="ruby-identifier">cents</span>)
-36: <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>
-37: <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>
-38: <span class="ruby-keyword kw">end</span>
-39: <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>
-40: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">cents</span> <span class="ruby-operator">==</span> <span class="ruby-value str">&quot;&quot;</span>
-41: <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">&quot;&quot;</span>
-42: <span class="ruby-keyword kw">else</span>
-43: <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">&quot;%.2f&quot;</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>)
-44: <span class="ruby-keyword kw">end</span>
-45: <span class="ruby-keyword kw">end</span>
-46: <span class="ruby-keyword kw">end</span>
-47: <span class="ruby-keyword kw">end</span>
+ <span class="ruby-comment cmt"># File lib/acts_as_price_helper.rb, line 34</span>
+34: <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>
+35: <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>
+36: <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">send</span>(<span class="ruby-node">&quot;#{setter}=&quot;</span>, <span class="ruby-identifier">cents</span>)
+37: <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>
+38: <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>
+39: <span class="ruby-keyword kw">end</span>
+40: <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>
+41: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">cents</span> <span class="ruby-operator">==</span> <span class="ruby-value str">&quot;&quot;</span>
+42: <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">&quot;&quot;</span>
+43: <span class="ruby-keyword kw">else</span>
+44: <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">&quot;%.2f&quot;</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">&quot;.&quot;</span>, <span class="ruby-identifier">seperator</span>)
+45: <span class="ruby-keyword kw">end</span>
+46: <span class="ruby-keyword kw">end</span>
+47: <span class="ruby-keyword kw">end</span>
+48: <span class="ruby-keyword kw">end</span>
</pre>
</div>
</div>
@@ -269,31 +248,34 @@ <h3 class="section-bar">Public Instance methods</h3>
<div class="method-heading">
<a href="#M000005" class="method-signature">
- <span class="method-name">test_setter_in_doubles</span><span class="method-args">(double)</span>
+ <span class="method-name">test_setter_in_doubles</span><span class="method-args">(double, seperator)</span>
</a>
</div>
<div class="method-description">
+ <p>
+test if the setter sets the price correctly if price is given in doubles
+</p>
<p><a class="source-toggle" href="#"
onclick="toggleCode('M000005-source');return false;">[Source]</a></p>
<div class="method-source-code" id="M000005-source">
<pre>
- <span class="ruby-comment cmt"># File lib/acts_as_price_helper.rb, line 49</span>
-49: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-identifier">double</span>
-50: <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>
-51: <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">send</span>(<span class="ruby-node">&quot;#{setter}=&quot;</span>, <span class="ruby-identifier">double</span>)
-52: <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>
-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-identifier">double</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>
-54: <span class="ruby-keyword kw">end</span>
-55: <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>
-56: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">double</span> <span class="ruby-operator">==</span> <span class="ruby-value str">&quot;&quot;</span>
-57: <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">&quot;&quot;</span>
-58: <span class="ruby-keyword kw">else</span>
-59: <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">&quot;%.2f&quot;</span>, <span class="ruby-identifier">double</span>)
-60: <span class="ruby-keyword kw">end</span>
-61: <span class="ruby-keyword kw">end</span>
-62: <span class="ruby-keyword kw">end</span>
-63: <span class="ruby-keyword kw">end</span>
+ <span class="ruby-comment cmt"># File lib/acts_as_price_helper.rb, line 51</span>
+51: <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>
+52: <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>
+53: <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">send</span>(<span class="ruby-node">&quot;#{setter}=&quot;</span>, <span class="ruby-identifier">double</span>)
+54: <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>
+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">double</span>.<span class="ruby-identifier">gsub</span>(<span class="ruby-value str">&quot;,&quot;</span>, <span class="ruby-value str">&quot;.&quot;</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>
+56: <span class="ruby-keyword kw">end</span>
+57: <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>
+58: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">double</span> <span class="ruby-operator">==</span> <span class="ruby-value str">&quot;&quot;</span>
+59: <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">&quot;&quot;</span>
+60: <span class="ruby-keyword kw">else</span>
+61: <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">&quot;%.2f&quot;</span>, <span class="ruby-identifier">double</span>.<span class="ruby-identifier">gsub</span>(<span class="ruby-value str">&quot;,&quot;</span>, <span class="ruby-value str">&quot;.&quot;</span>)).<span class="ruby-identifier">gsub</span>(<span class="ruby-value str">&quot;.&quot;</span>, <span class="ruby-identifier">seperator</span>)
+62: <span class="ruby-keyword kw">end</span>
+63: <span class="ruby-keyword kw">end</span>
+64: <span class="ruby-keyword kw">end</span>
+65: <span class="ruby-keyword kw">end</span>
</pre>
</div>
</div>
2  rdoc/created.rid
View
@@ -1 +1 @@
-Mon, 25 Apr 2011 15:18:30 -0700
+Fri, 29 Apr 2011 15:37:20 -0700
40 rdoc/files/README_rdoc.html
View
@@ -56,7 +56,7 @@
</tr>
<tr class="top-aligned-row">
<td><strong>Last Update:</strong></td>
- <td>Mon Apr 25 15:18:27 -0700 2011</td>
+ <td>Fri Apr 29 15:27:33 -0700 2011</td>
</tr>
</table>
</div>
@@ -85,15 +85,23 @@
In your model add the following:
</p>
<ul>
-<li>acts_as_price &lt;column_name&gt;, :validates = true
+<li>acts_as_price column_name, {:validates =&gt; true, :comma_seperated =&gt;
+true}
</li>
</ul>
<p>
-&lt;column_name&gt; is the name of your database column e.g. price or
+column_name is the name of your database column e.g. price or
price_per_liter.
</p>
<p>
+validates is optional and add validation to the column
+</p>
+<p>
+comma_seperated is also optional and return the price comma seperated
+instead of dot seperated
+</p>
+<p>
This plugin creates the following getters and setters:
</p>
<ul>
@@ -107,29 +115,39 @@
</li>
</ul>
<h1>EXAMPLES</h1>
+<pre>
+ class Car &lt; ActiveRecord::Base
+ acts_as_price :price, :comma_seperated =&gt; true
+ end
+</pre>
<p>
car = Car.new :price =&gt; 12999
</p>
<p>
-car.price 12999.00
+car.price -&gt; 12999,00
</p>
<p>
-car.price_in_cents 1299900
+car.price_in_cents -&gt; 1299900
</p>
+<pre>
+ class Fueltype &lt; ActiveRecord::Base
+ acts_as_price :price_per_liter, :validates =&gt; true
+ end
+</pre>
<p>
fuel = Fueltype.new :price_per_liter =&gt; 1.12
</p>
<p>
-fuel.price 1.12
+fuel.price -&gt; 1.12
</p>
<p>
-fuel.price_in_cents 112
+fuel.price_in_cents -&gt; 112
</p>
<p>
-fuel.price_per_liter 1.12
+fuel.price_per_liter -&gt; 1.12
</p>
<p>
-fuel.price_per_liter_in_cents 1.12
+fuel.price_per_liter_in_cents -&gt; 1.12
</p>
<h1>RSPEC INTERGRATION</h1>
<p>
@@ -143,6 +161,10 @@
<li>config.include(ActsAsPriceHelper)
</li>
+<li>and if you using the gem, please also add: require
+&#8216;acts_as_price_helper&#8216;
+
+</li>
</ul>
<p>
Now you may add the following code to your spec-files for your models:
2  rdoc/files/lib/acts_as_price_helper_rb.html
View
@@ -56,7 +56,7 @@
</tr>
<tr class="top-aligned-row">
<td><strong>Last Update:</strong></td>
- <td>Mon Apr 25 15:00:23 -0700 2011</td>
+ <td>Fri Apr 29 15:16:53 -0700 2011</td>
</tr>
</table>
</div>
2  rdoc/files/lib/acts_as_price_rb.html
View
@@ -56,7 +56,7 @@
</tr>
<tr class="top-aligned-row">
<td><strong>Last Update:</strong></td>
- <td>Mon Apr 25 15:00:23 -0700 2011</td>
+ <td>Fri Apr 29 15:34:16 -0700 2011</td>
</tr>
</table>
</div>
1  rdoc/fr_method_index.html
View
@@ -21,7 +21,6 @@
<h1 class="section-bar">Methods</h1>
<div id="index-entries">
<a href="classes/ActiveRecord/Acts/Price/ClassMethods.html#M000002">acts_as_price (ActiveRecord::Acts::Price::ClassMethods)</a><br />
- <a href="classes/ActsAsPriceHelper.html#M000008">columns (ActsAsPriceHelper)</a><br />
<a href="classes/ActsAsPriceHelper.html#M000006">columns_in_cents (ActsAsPriceHelper)</a><br />
<a href="classes/ActsAsPriceHelper.html#M000007">columns_in_doubles (ActsAsPriceHelper)</a><br />
<a href="classes/ActiveRecord/Acts/Price.html#M000001">included (ActiveRecord::Acts::Price)</a><br />
27 spec/car_spec.rb
View
@@ -3,7 +3,7 @@
describe Car do
before(:each) do
@column_name = :price
- @acts_as_price_model = stub_model(Car, :brand => "Ford", :cartype => "Focus", :price => "23995.99")
+ @acts_as_price_model = stub_model(Car, :brand => "Ford", :cartype => "Focus", :price => "23995,99")
end
context "given an empty car" do
@@ -30,36 +30,35 @@
@acts_as_price_model.send(column).should == 2399599
end
end
- it "should return the price" do
+ it "should return the price seperated by a comma" do
columns_in_doubles.each do |column|
- @acts_as_price_model.send(column).should == "23995.99"
+ @acts_as_price_model.send(column).should == "23995,99"
end
end
end
- context "set the price for hundred different prices" do
- 2200000.upto(2200100) do |i|
- it "should set the price in cents and return it correctly" do
- test_setter_in_cents i.to_s
+ context "set the price for hundred different prices to test if you got no floating point problems" do
+ 2200000.upto(2200099) do |i|
+ it "should set the price in cents and return it correctly (seperated by comma)" do
+ test_setter_in_cents i.to_s, ","
end
- it "should set the price and return it correctly" do
- test_setter_in_doubles sprintf("%.2f", i.to_f / 100)
+ it "should set the price and return it correctly (seperated by comma)" do
+ test_setter_in_doubles sprintf("%.2f", i.to_f / 100).gsub(".", ","), ","
end
end
-
end
context "given a float as price" do
it "should convert it to the right price in cents" do
- test_setter_in_doubles "25500.5"
- test_setter_in_doubles "21599.05"
+ test_setter_in_doubles "25500,5", ","
+ test_setter_in_doubles "21599,05", ","
end
end
context "given the price is zero" do
it "should return an empty price per liter" do
- test_setter_in_cents ""
- test_setter_in_doubles ""
+ test_setter_in_cents "", ","
+ test_setter_in_doubles "", ","
end
end
end
15 spec/fueltype_spec.rb
View
@@ -4,6 +4,7 @@
before(:each) do
@column_name = :price_per_liter
@acts_as_price_model = stub_model(Fueltype, :name => "Euro 95", :price_per_liter => "1.55")
+ @seperator = "."
end
context "given an empty fueltype" do
@@ -37,28 +38,28 @@
end
end
- context "set the price per liter for hundred different prices" do
+ context "set the price per liter for hundred different prices to test if you got no floating point problems" do
1.upto(100).each do |i|
it "should set the price per liter in cents and return it correctly" do
- test_setter_in_cents i.to_s
+ test_setter_in_cents i.to_s, @seperator
end
it "should set the price per liter and return it correctly" do
- test_setter_in_doubles sprintf("%.2f", i.to_f / 100)
+ test_setter_in_doubles sprintf("%.2f", i.to_f / 100), @seperator
end
end
end
context "given a float as price per liter" do
it "should convert it to the right price in cents" do
- test_setter_in_doubles "1.5"
- test_setter_in_doubles "2.05"
+ test_setter_in_doubles "1.5", @seperator
+ test_setter_in_doubles "2.05", @seperator
end
end
context "given the price is zero" do
it "should return an empty price per liter" do
- test_setter_in_cents ""
- test_setter_in_doubles ""
+ test_setter_in_cents "", @seperator
+ test_setter_in_doubles "", @seperator
end
end
end
2  spec/models/car.rb
View
@@ -3,5 +3,5 @@ class Car < ActiveRecord::Base
validates :brand, :presence => true
validates :cartype, :presence => true
- acts_as_price :price
+ acts_as_price :price, :comma_seperated => true
end
Please sign in to comment.
Something went wrong with that request. Please try again.