Browse files

Currency added

  • Loading branch information...
1 parent 5a518c1 commit 73cb81bca68a98aed9e630ef331b30fb17d2bf12 @jeroeningen committed May 1, 2011
View
10 README.rdoc
@@ -7,26 +7,28 @@ 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, :comma_seperated => true}
+- acts_as_price column_name, {:validates => true, :comma_seperated => true, :currency => currency}
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
+comma_seperated is optional and return the price comma seperated instead of dot seperated
+
+currency is optional and adds a prefix for the currency to the price.
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
+ acts_as_price :price, :comma_seperated => true, :currency => "EUR"
end
car = Car.new :price => 12999
-car.price -> 12999,00
+car.price -> EUR. 12999,00
car.price_in_cents -> 1299900
View
14 acts_as_price.gemspec
@@ -5,11 +5,11 @@
Gem::Specification.new do |s|
s.name = %q{acts_as_price}
- s.version = "0.1.2"
+ s.version = "0.1.3"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Jeroen van Ingen"]
- s.date = %q{2011-04-29}
+ s.date = %q{2011-05-01}
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}
s.email = %q{jeroeningen@gmail.com}
s.extra_rdoc_files = [
@@ -40,10 +40,11 @@ Gem::Specification.new do |s|
"rdoc/fr_method_index.html",
"rdoc/index.html",
"rdoc/rdoc-style.css",
- "spec/car_spec.rb",
- "spec/fueltype_spec.rb",
+ "spec/advanced_tests/car_spec.rb",
+ "spec/advanced_tests/fueltype_spec.rb",
"spec/models/car.rb",
"spec/models/fueltype.rb",
+ "spec/simple_tests/car_and_fueltype_spec.rb",
"spec/spec_helper.rb",
"uninstall.rb"
]
@@ -53,10 +54,11 @@ Gem::Specification.new do |s|
s.rubygems_version = %q{1.3.7}
s.summary = %q{A specified database column acts as a price}
s.test_files = [
- "spec/car_spec.rb",
- "spec/fueltype_spec.rb",
+ "spec/advanced_tests/car_spec.rb",
+ "spec/advanced_tests/fueltype_spec.rb",
"spec/models/car.rb",
"spec/models/fueltype.rb",
+ "spec/simple_tests/car_and_fueltype_spec.rb",
"spec/spec_helper.rb"
]
View
18 lib/acts_as_price.rb
@@ -14,7 +14,7 @@ module ClassMethods
# * :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]
+ currency, comma = options[:currency], options[:comma_seperated]
#setters
define_method("#{column_name}=") do |val|
@@ -33,18 +33,28 @@ def acts_as_price column_name, options = {}
""
else
if comma
- sprintf("%.2f", super.to_f / 100).gsub(".", ",")
+ return_val = sprintf("%.2f", super.to_f / 100).gsub(".", ",")
else
- sprintf("%.2f", super.to_f / 100)
+ return_val = sprintf("%.2f", super.to_f / 100)
end
+ return_val = currency ? "#{currency}. #{return_val}" : return_val
+ return_val
end
end
alias_method "price", column_name
define_method("price_in_cents") do
- ((send column_name).gsub(",", ".").to_f * 100).to_s.to_i
+ if currency
+ ((send column_name).gsub("#{currency}. ", "").gsub(",", ".").to_f * 100).to_s.to_i
+ else
+ ((send column_name).gsub(",", ".").to_f * 100).to_s.to_i
+ end
end
alias_method "#{column_name}_in_cents", "price_in_cents"
+
+ define_method("currency") do
+ currency
+ end
end
end
end
View
64 lib/acts_as_price_helper.rb
@@ -20,21 +20,40 @@ def test_acts_as_price_methods column_name, seperator, options = {}
#normal test for setter
test_setter_in_cents "144", seperator
- test_setter_in_doubles "1.41", seperator
- test_setter_in_doubles "1,41", seperator
-
- #test for special cases
- #test if 1.5 is returned as 1.50
- test_setter_in_doubles "1.5", seperator
- #test if 1,5 is returned as 1.50
- test_setter_in_doubles "1,5", seperator
-
- #test if float returns 2.05 correctly
- #float can return 2.05 as 2.04 in some cases
- test_setter_in_doubles "2.05", seperator
-
- #float can return 2,05 as 2.04 in some cases
- test_setter_in_doubles "2,05", seperator
+ if @acts_as_price_model.currency
+ currency = @acts_as_price_model.currency
+ test_setter_in_doubles "#{currency}. 1.41", seperator
+ test_setter_in_doubles "#{currency}. 1,41", seperator
+
+ #test for special cases
+ #test if 1.5 is returned as 1.50
+ test_setter_in_doubles "#{currency}. 1.5", seperator
+ #test if 1,5 is returned as 1.50
+ test_setter_in_doubles "#{currency}. 1,5", seperator
+
+ #test if float returns 2.05 correctly
+ #float can return 2.05 as 2.04 in some cases
+ test_setter_in_doubles "#{currency}. 2.05", seperator
+
+ #float can return 2,05 as 2.04 in some cases
+ test_setter_in_doubles "#{currency}. 2,05", seperator
+ else
+ test_setter_in_doubles "1.41", seperator
+ test_setter_in_doubles "1,41", seperator
+
+ #test for special cases
+ #test if 1.5 is returned as 1.50
+ test_setter_in_doubles "1.5", seperator
+ #test if 1,5 is returned as 1.50
+ test_setter_in_doubles "1,5", seperator
+
+ #test if float returns 2.05 correctly
+ #float can return 2.05 as 2.04 in some cases
+ test_setter_in_doubles "2.05", seperator
+
+ #float can return 2,05 as 2.04 in some cases
+ test_setter_in_doubles "2,05", seperator
+ end
#test an empty_setter
test_setter_in_cents "", seperator
@@ -52,14 +71,21 @@ def test_setter_in_cents cents, seperator
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).gsub(".", seperator)
+ currency = @acts_as_price_model.currency
+ if currency
+ @acts_as_price_model.send(getter).should == currency + ". " + sprintf("%.2f", (cents.to_f / 100).to_s).gsub(".", seperator)
+ else
+ @acts_as_price_model.send(getter).should == sprintf("%.2f", (cents.to_f / 100).to_s).gsub(".", seperator)
+ end
end
end
end
end
# test if the setter sets the price correctly if price is given in doubles
def test_setter_in_doubles double, seperator
+ currency = @acts_as_price_model.currency
+ double = double.gsub("#{currency}. ", "")
columns_in_doubles.each do |setter|
@acts_as_price_model.send("#{setter}=", double)
columns_in_cents.each do |getter|
@@ -69,7 +95,11 @@ def test_setter_in_doubles double, seperator
if double == ""
@acts_as_price_model.send(getter).should == ""
else
- @acts_as_price_model.send(getter).should == sprintf("%.2f", double.gsub(",", ".")).gsub(".", seperator)
+ if currency
+ @acts_as_price_model.send(getter).should == currency + ". " + sprintf("%.2f", double.gsub(",", ".")).gsub(".", seperator)
+ else
+ @acts_as_price_model.send(getter).should == sprintf("%.2f", double.gsub(",", ".")).gsub(".", seperator)
+ end
end
end
end
View
34 rdoc/classes/ActiveRecord/Acts/Price/ClassMethods.html
@@ -135,7 +135,7 @@ <h3 class="section-bar">Public Instance methods</h3>
<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>]
+17: <span class="ruby-identifier">currency</span>, <span class="ruby-identifier">comma</span> = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:currency</span>], <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:comma_seperated</span>]
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>
@@ -154,19 +154,29 @@ <h3 class="section-bar">Public Instance methods</h3>
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>)
+36: <span class="ruby-identifier">return_val</span> = <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>)
+38: <span class="ruby-identifier">return_val</span> = <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>
+40: <span class="ruby-identifier">return_val</span> = <span class="ruby-identifier">currency</span> <span class="ruby-value">? </span><span class="ruby-node">&quot;#{currency}. #{return_val}&quot;</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">return_val</span>
+41: <span class="ruby-identifier">return_val</span>
+42: <span class="ruby-keyword kw">end</span>
+43: <span class="ruby-keyword kw">end</span>
+44: <span class="ruby-identifier">alias_method</span> <span class="ruby-value str">&quot;price&quot;</span>, <span class="ruby-identifier">column_name</span>
+45:
+46: <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>
+47: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">currency</span>
+48: ((<span class="ruby-identifier">send</span> <span class="ruby-identifier">column_name</span>).<span class="ruby-identifier">gsub</span>(<span class="ruby-node">&quot;#{currency}. &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-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>
+49: <span class="ruby-keyword kw">else</span>
+50: ((<span class="ruby-identifier">send</span> <span class="ruby-identifier">column_name</span>).<span class="ruby-identifier">gsub</span>(<span class="ruby-value str">&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>
+51: <span class="ruby-keyword kw">end</span>
+52: <span class="ruby-keyword kw">end</span>
+53: <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>
+54:
+55: <span class="ruby-identifier">define_method</span>(<span class="ruby-value str">&quot;currency&quot;</span>) <span class="ruby-keyword kw">do</span>
+56: <span class="ruby-identifier">currency</span>
+57: <span class="ruby-keyword kw">end</span>
+58: <span class="ruby-keyword kw">end</span>
</pre>
</div>
</div>
View
150 rdoc/classes/ActsAsPriceHelper.html
@@ -120,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 78</span>
-78: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">columns_in_cents</span>
-79: [<span class="ruby-identifier">:price_in_cents</span>, <span class="ruby-node">&quot;#{@column_name}_in_cents&quot;</span>]
-80: <span class="ruby-keyword kw">end</span>
+ <span class="ruby-comment cmt"># File lib/acts_as_price_helper.rb, line 108</span>
+108: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">columns_in_cents</span>
+109: [<span class="ruby-identifier">:price_in_cents</span>, <span class="ruby-node">&quot;#{@column_name}_in_cents&quot;</span>]
+110: <span class="ruby-keyword kw">end</span>
</pre>
</div>
</div>
@@ -143,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 82</span>
-82: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">columns_in_doubles</span>
-83: [<span class="ruby-identifier">:price</span>, <span class="ruby-ivar">@column_name</span>]
-84: <span class="ruby-keyword kw">end</span>
+ <span class="ruby-comment cmt"># File lib/acts_as_price_helper.rb, line 112</span>
+112: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">columns_in_doubles</span>
+113: [<span class="ruby-identifier">:price</span>, <span class="ruby-ivar">@column_name</span>]
+114: <span class="ruby-keyword kw">end</span>
</pre>
</div>
</div>
@@ -204,26 +204,45 @@ <h3 class="section-bar">Public Instance methods</h3>
20:
21: <span class="ruby-comment cmt">#normal test for setter</span>
22: <span class="ruby-identifier">test_setter_in_cents</span> <span class="ruby-value str">&quot;144&quot;</span>, <span class="ruby-identifier">seperator</span>
-23: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-value str">&quot;1.41&quot;</span>, <span class="ruby-identifier">seperator</span>
-24: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-value str">&quot;1,41&quot;</span>, <span class="ruby-identifier">seperator</span>
-25:
-26: <span class="ruby-comment cmt">#test for special cases</span>
-27: <span class="ruby-comment cmt">#test if 1.5 is returned as 1.50</span>
-28: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-value str">&quot;1.5&quot;</span>, <span class="ruby-identifier">seperator</span>
-29: <span class="ruby-comment cmt">#test if 1,5 is returned as 1.50</span>
-30: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-value str">&quot;1,5&quot;</span>, <span class="ruby-identifier">seperator</span>
-31:
-32: <span class="ruby-comment cmt">#test if float returns 2.05 correctly </span>
-33: <span class="ruby-comment cmt">#float can return 2.05 as 2.04 in some cases</span>
-34: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-value str">&quot;2.05&quot;</span>, <span class="ruby-identifier">seperator</span>
-35:
-36: <span class="ruby-comment cmt">#float can return 2,05 as 2.04 in some cases</span>
-37: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-value str">&quot;2,05&quot;</span>, <span class="ruby-identifier">seperator</span>
-38:
-39: <span class="ruby-comment cmt">#test an empty_setter</span>
-40: <span class="ruby-identifier">test_setter_in_cents</span> <span class="ruby-value str">&quot;&quot;</span>, <span class="ruby-identifier">seperator</span>
-41: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-value str">&quot;&quot;</span>, <span class="ruby-identifier">seperator</span>
-42: <span class="ruby-keyword kw">end</span>
+23: <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">currency</span>
+24: <span class="ruby-identifier">currency</span> = <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">currency</span>
+25: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-node">&quot;#{currency}. 1.41&quot;</span>, <span class="ruby-identifier">seperator</span>
+26: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-node">&quot;#{currency}. 1,41&quot;</span>, <span class="ruby-identifier">seperator</span>
+27:
+28: <span class="ruby-comment cmt">#test for special cases</span>
+29: <span class="ruby-comment cmt">#test if 1.5 is returned as 1.50</span>
+30: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-node">&quot;#{currency}. 1.5&quot;</span>, <span class="ruby-identifier">seperator</span>
+31: <span class="ruby-comment cmt">#test if 1,5 is returned as 1.50</span>
+32: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-node">&quot;#{currency}. 1,5&quot;</span>, <span class="ruby-identifier">seperator</span>
+33:
+34: <span class="ruby-comment cmt">#test if float returns 2.05 correctly </span>
+35: <span class="ruby-comment cmt">#float can return 2.05 as 2.04 in some cases</span>
+36: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-node">&quot;#{currency}. 2.05&quot;</span>, <span class="ruby-identifier">seperator</span>
+37:
+38: <span class="ruby-comment cmt">#float can return 2,05 as 2.04 in some cases</span>
+39: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-node">&quot;#{currency}. 2,05&quot;</span>, <span class="ruby-identifier">seperator</span>
+40: <span class="ruby-keyword kw">else</span>
+41: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-value str">&quot;1.41&quot;</span>, <span class="ruby-identifier">seperator</span>
+42: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-value str">&quot;1,41&quot;</span>, <span class="ruby-identifier">seperator</span>
+43:
+44: <span class="ruby-comment cmt">#test for special cases</span>
+45: <span class="ruby-comment cmt">#test if 1.5 is returned as 1.50</span>
+46: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-value str">&quot;1.5&quot;</span>, <span class="ruby-identifier">seperator</span>
+47: <span class="ruby-comment cmt">#test if 1,5 is returned as 1.50</span>
+48: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-value str">&quot;1,5&quot;</span>, <span class="ruby-identifier">seperator</span>
+49:
+50: <span class="ruby-comment cmt">#test if float returns 2.05 correctly </span>
+51: <span class="ruby-comment cmt">#float can return 2.05 as 2.04 in some cases</span>
+52: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-value str">&quot;2.05&quot;</span>, <span class="ruby-identifier">seperator</span>
+53:
+54: <span class="ruby-comment cmt">#float can return 2,05 as 2.04 in some cases</span>
+55: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-value str">&quot;2,05&quot;</span>, <span class="ruby-identifier">seperator</span>
+56: <span class="ruby-keyword kw">end</span>
+57:
+58: <span class="ruby-comment cmt">#test an empty_setter</span>
+59: <span class="ruby-identifier">test_setter_in_cents</span> <span class="ruby-value str">&quot;&quot;</span>, <span class="ruby-identifier">seperator</span>
+60: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-value str">&quot;&quot;</span>, <span class="ruby-identifier">seperator</span>
+61: <span class="ruby-keyword kw">end</span>
</pre>
</div>
</div>
@@ -246,22 +265,27 @@ <h3 class="section-bar">Public Instance methods</h3>
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 45</span>
-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>
-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>
-47: <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>)
-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>
-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>
-50: <span class="ruby-keyword kw">end</span>
-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>
-52: <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>
-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">&quot;&quot;</span>
-54: <span class="ruby-keyword kw">else</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">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>)
-56: <span class="ruby-keyword kw">end</span>
-57: <span class="ruby-keyword kw">end</span>
-58: <span class="ruby-keyword kw">end</span>
-59: <span class="ruby-keyword kw">end</span>
+ <span class="ruby-comment cmt"># File lib/acts_as_price_helper.rb, line 64</span>
+64: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">test_setter_in_cents</span> <span class="ruby-identifier">cents</span>, <span class="ruby-identifier">seperator</span>
+65: <span class="ruby-identifier">columns_in_cents</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">setter</span><span class="ruby-operator">|</span>
+66: <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>)
+67: <span class="ruby-identifier">columns_in_cents</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">getter</span><span class="ruby-operator">|</span>
+68: <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">getter</span>).<span class="ruby-identifier">should</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">cents</span>.<span class="ruby-identifier">to_i</span>
+69: <span class="ruby-keyword kw">end</span>
+70: <span class="ruby-identifier">columns_in_doubles</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">getter</span><span class="ruby-operator">|</span>
+71: <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>
+72: <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">getter</span>).<span class="ruby-identifier">should</span> <span class="ruby-operator">==</span> <span class="ruby-value str">&quot;&quot;</span>
+73: <span class="ruby-keyword kw">else</span>
+74: <span class="ruby-identifier">currency</span> = <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">currency</span>
+75: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">currency</span>
+76: <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">getter</span>).<span class="ruby-identifier">should</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">currency</span> <span class="ruby-operator">+</span> <span class="ruby-value str">&quot;. &quot;</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>)
+77: <span class="ruby-keyword kw">else</span>
+78: <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">getter</span>).<span class="ruby-identifier">should</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">sprintf</span>(<span class="ruby-value str">&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>)
+79: <span class="ruby-keyword kw">end</span>
+80: <span class="ruby-keyword kw">end</span>
+81: <span class="ruby-keyword kw">end</span>
+82: <span class="ruby-keyword kw">end</span>
+83: <span class="ruby-keyword kw">end</span>
</pre>
</div>
</div>
@@ -284,22 +308,28 @@ <h3 class="section-bar">Public Instance methods</h3>
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 62</span>
-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>
-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>
-64: <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>)
-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>
-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">&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>
-67: <span class="ruby-keyword kw">end</span>
-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>
-69: <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>
-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">&quot;&quot;</span>
-71: <span class="ruby-keyword kw">else</span>
-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">&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>)
-73: <span class="ruby-keyword kw">end</span>
-74: <span class="ruby-keyword kw">end</span>
-75: <span class="ruby-keyword kw">end</span>
-76: <span class="ruby-keyword kw">end</span>
+ <span class="ruby-comment cmt"># File lib/acts_as_price_helper.rb, line 86</span>
+ 86: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-identifier">double</span>, <span class="ruby-identifier">seperator</span>
+ 87: <span class="ruby-identifier">currency</span> = <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">currency</span>
+ 88: <span class="ruby-identifier">double</span> = <span class="ruby-identifier">double</span>.<span class="ruby-identifier">gsub</span>(<span class="ruby-node">&quot;#{currency}. &quot;</span>, <span class="ruby-value str">&quot;&quot;</span>)
+ 89: <span class="ruby-identifier">columns_in_doubles</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">setter</span><span class="ruby-operator">|</span>
+ 90: <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>)
+ 91: <span class="ruby-identifier">columns_in_cents</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">getter</span><span class="ruby-operator">|</span>
+ 92: <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">getter</span>).<span class="ruby-identifier">should</span> <span class="ruby-operator">==</span> (<span class="ruby-identifier">double</span>.<span class="ruby-identifier">gsub</span>(<span class="ruby-value str">&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>
+ 93: <span class="ruby-keyword kw">end</span>
+ 94: <span class="ruby-identifier">columns_in_doubles</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">getter</span><span class="ruby-operator">|</span>
+ 95: <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>
+ 96: <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">getter</span>).<span class="ruby-identifier">should</span> <span class="ruby-operator">==</span> <span class="ruby-value str">&quot;&quot;</span>
+ 97: <span class="ruby-keyword kw">else</span>
+ 98: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">currency</span>
+ 99: <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">getter</span>).<span class="ruby-identifier">should</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">currency</span> <span class="ruby-operator">+</span> <span class="ruby-value str">&quot;. &quot;</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>)
+100: <span class="ruby-keyword kw">else</span>
+101: <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">getter</span>).<span class="ruby-identifier">should</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">sprintf</span>(<span class="ruby-value str">&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>)
+102: <span class="ruby-keyword kw">end</span>
+103: <span class="ruby-keyword kw">end</span>
+104: <span class="ruby-keyword kw">end</span>
+105: <span class="ruby-keyword kw">end</span>
+106: <span class="ruby-keyword kw">end</span>
</pre>
</div>
</div>
View
2 rdoc/created.rid
@@ -1 +1 @@
-Sun, 01 May 2011 07:33:52 -0700
+Sun, 01 May 2011 08:58:34 -0700
View
15 rdoc/files/README_rdoc.html
@@ -56,7 +56,7 @@
</tr>
<tr class="top-aligned-row">
<td><strong>Last Update:</strong></td>
- <td>Sun May 01 07:30:16 -0700 2011</td>
+ <td>Sun May 01 07:51:31 -0700 2011</td>
</tr>
</table>
</div>
@@ -86,7 +86,7 @@
</p>
<ul>
<li>acts_as_price column_name, {:validates =&gt; true, :comma_seperated =&gt;
-true}
+true, :currency =&gt; currency}
</li>
</ul>
@@ -98,8 +98,11 @@
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
+comma_seperated is optional and return the price comma seperated instead of
+dot seperated
+</p>
+<p>
+currency is optional and adds a prefix for the currency to the price.
</p>
<p>
This plugin creates the following getters and setters:
@@ -117,14 +120,14 @@
<h1>EXAMPLES</h1>
<pre>
class Car &lt; ActiveRecord::Base
- acts_as_price :price, :comma_seperated =&gt; true
+ acts_as_price :price, :comma_seperated =&gt; true, :currency =&gt; &quot;EUR&quot;
end
</pre>
<p>
car = Car.new :price =&gt; 12999
</p>
<p>
-car.price -&gt; 12999,00
+car.price -&gt; EUR. 12999,00
</p>
<p>
car.price_in_cents -&gt; 1299900
View
2 rdoc/files/lib/acts_as_price_helper_rb.html
@@ -56,7 +56,7 @@
</tr>
<tr class="top-aligned-row">
<td><strong>Last Update:</strong></td>
- <td>Fri Apr 29 16:03:40 -0700 2011</td>
+ <td>Sun May 01 08:55:07 -0700 2011</td>
</tr>
</table>
</div>
View
2 rdoc/files/lib/acts_as_price_rb.html
@@ -56,7 +56,7 @@
</tr>
<tr class="top-aligned-row">
<td><strong>Last Update:</strong></td>
- <td>Fri Apr 29 15:34:16 -0700 2011</td>
+ <td>Sun May 01 08:27:30 -0700 2011</td>
</tr>
</table>
</div>
View
6 spec/advanced_tests/car_spec.rb
@@ -32,7 +32,7 @@
end
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 == "EUR. 23995,99"
end
end
end
@@ -50,8 +50,8 @@
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 "EUR. 25500,5", ","
+ test_setter_in_doubles "EUR. 21599,05", ","
end
end
View
2 spec/models/car.rb
@@ -3,5 +3,5 @@ class Car < ActiveRecord::Base
validates :brand, :presence => true
validates :cartype, :presence => true
- acts_as_price :price, :comma_seperated => true
+ acts_as_price :price, :comma_seperated => true, :currency => "EUR"
end
View
15 spec/simple_tests/car_and_fueltype_spec.rb
@@ -5,7 +5,7 @@
context "adding a price as comma seperated value" do
it "should return the price as comma-seperated value" do
car = stub_model(Car, :price => "23000,59")
- car.price.should == "23000,59"
+ car.price.should == "EUR. 23000,59"
end
it "should return the pricce in cents" do
car = stub_model(Car, :price => "23000,59")
@@ -16,16 +16,23 @@
context "adding the price as a dot seperated value" do
it "should return the price as a comma seperated value" do
car = stub_model(Car, :price => "23000.59")
- car.price.should == "23000,59"
+ car.price.should == "EUR. 23000,59"
car = stub_model(Car, :price => "23000.5")
- car.price.should == "23000,50"
+ car.price.should == "EUR. 23000,50"
end
end
context "adding the price as an integer" do
it "should return the price as a comma-seperated value" do
car = stub_model(Car, :price => "23000")
- car.price.should == "23000,00"
+ car.price.should == "EUR. 23000,00"
+ end
+ end
+
+ context "using a currency" do
+ it "should return a currency" do
+ car = stub_model(Car)
+ car.currency.should == "EUR"
end
end
end

0 comments on commit 73cb81b

Please sign in to comment.