Permalink
Browse files

acts as price now works with comma seperated values

  • Loading branch information...
1 parent 9661219 commit c98e63e1fe9226f4ef4c624057c5c0031ada3146 @jeroeningen committed Apr 29, 2011
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.
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
@@ -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
@@ -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>
Oops, something went wrong.

0 comments on commit c98e63e

Please sign in to comment.