Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
459 lines (319 sloc) 24.3 KB
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Module: ActsAsPriceHelper</title>
<link rel="stylesheet" href="./rdoc.css" type="text/css" media="screen" />
<script src="./js/jquery.js" type="text/javascript"
charset="utf-8"></script>
<script src="./js/thickbox-compressed.js" type="text/javascript"
charset="utf-8"></script>
<script src="./js/quicksearch.js" type="text/javascript"
charset="utf-8"></script>
<script src="./js/darkfish.js" type="text/javascript"
charset="utf-8"></script>
</head>
<body class="module">
<div id="metadata">
<div id="home-metadata">
<div id="home-section" class="section">
<h3 class="section-header">
<a href="./index.html">Home</a>
<a href="./index.html#classes">Classes</a>
<a href="./index.html#methods">Methods</a>
</h3>
</div>
</div>
<div id="file-metadata">
<div id="file-list-section" class="section">
<h3 class="section-header">In Files</h3>
<div class="section-body">
<ul>
<li><a href="./lib/acts_as_price_helper_rb.html?TB_iframe=true&amp;height=550&amp;width=785"
class="thickbox" title="lib/acts_as_price_helper.rb">lib/acts_as_price_helper.rb</a></li>
</ul>
</div>
</div>
</div>
<div id="class-metadata">
<!-- Parent Class -->
<!-- Namespace Contents -->
<!-- Method Quickref -->
<div id="method-list-section" class="section">
<h3 class="section-header">Methods</h3>
<ul class="link-list">
<li><a href="#method-i-columns_in_cents">#columns_in_cents</a></li>
<li><a href="#method-i-columns_in_doubles">#columns_in_doubles</a></li>
<li><a href="#method-i-test_acts_as_price_methods">#test_acts_as_price_methods</a></li>
<li><a href="#method-i-test_setter_in_cents">#test_setter_in_cents</a></li>
<li><a href="#method-i-test_setter_in_doubles">#test_setter_in_doubles</a></li>
</ul>
</div>
<!-- Included Modules -->
</div>
<div id="project-metadata">
<div id="fileindex-section" class="section project-section">
<h3 class="section-header">Files</h3>
<ul>
<li class="file"><a href="./README_rdoc.html">README.rdoc</a></li>
</ul>
</div>
<div id="classindex-section" class="section project-section">
<h3 class="section-header">Class Index
<span class="search-toggle"><img src="./images/find.png"
height="16" width="16" alt="[+]"
title="show/hide quicksearch" /></span></h3>
<form action="#" method="get" accept-charset="utf-8" class="initially-hidden">
<fieldset>
<legend>Quicksearch</legend>
<input type="text" name="quicksearch" value=""
class="quicksearch-field" />
</fieldset>
</form>
<ul class="link-list">
<li><a href="./ActiveRecord.html">ActiveRecord</a></li>
<li><a href="./ActiveRecord/Acts.html">ActiveRecord::Acts</a></li>
<li><a href="./ActiveRecord/Acts/Price.html">ActiveRecord::Acts::Price</a></li>
<li><a href="./ActiveRecord/Acts/Price/ClassMethods.html">ActiveRecord::Acts::Price::ClassMethods</a></li>
<li><a href="./ActsAsPriceHelper.html">ActsAsPriceHelper</a></li>
</ul>
<div id="no-class-search-results" style="display: none;">No matching classes.</div>
</div>
</div>
</div>
<div id="documentation">
<h1 class="module">ActsAsPriceHelper</h1>
<div id="description">
</div>
<!-- Constants -->
<!-- Attributes -->
<!-- Methods -->
<div id="public-instance-method-details" class="method-section section">
<h3 class="section-header">Public Instance Methods</h3>
<div id="columns-in-cents-method" class="method-detail ">
<a name="method-i-columns_in_cents"></a>
<div class="method-heading">
<span class="method-name">columns_in_cents</span><span
class="method-args">()</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<div class="method-source-code"
id="columns-in-cents-source">
<pre>
<span class="ruby-comment cmt"># File lib/acts_as_price_helper.rb, line 114</span>
114: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">columns_in_cents</span>
115: [<span class="ruby-value">:price_in_cents</span>, <span class="ruby-node">&quot;#{@column_name}_in_cents&quot;</span>]
116: <span class="ruby-keyword kw">end</span></pre>
</div>
</div>
</div>
<div id="columns-in-doubles-method" class="method-detail ">
<a name="method-i-columns_in_doubles"></a>
<div class="method-heading">
<span class="method-name">columns_in_doubles</span><span
class="method-args">()</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<div class="method-source-code"
id="columns-in-doubles-source">
<pre>
<span class="ruby-comment cmt"># File lib/acts_as_price_helper.rb, line 118</span>
118: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">columns_in_doubles</span>
119: [<span class="ruby-value">:price</span>, <span class="ruby-ivar">@column_name</span>]
120: <span class="ruby-keyword kw">end</span></pre>
</div>
</div>
</div>
<div id="test-acts-as-price-methods-method" class="method-detail ">
<a name="method-i-test_acts_as_price_methods"></a>
<div class="method-heading">
<span class="method-name">test_acts_as_price_methods</span><span
class="method-args">(column_name, seperator, options = {})</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>
Test the acts_as_price plugin for the specified column_name and given
seperator.
</p>
<p>
Valid options:
</p>
<ul>
<li><p>
model: Not just the model name, but a full object of the model
</p>
</li>
</ul>
<p>
EXAMPLE:
</p>
<ul>
<li><p>
<a
href="ActsAsPriceHelper.html#method-i-test_acts_as_price_methods">test_acts_as_price_methods</a>
:price, &#8220;,&#8221;, :model => @fuel
</p>
</li>
</ul>
<p>
Note the you can also specify the model in a before block in your tests
</p>
<div class="method-source-code"
id="test-acts-as-price-methods-source">
<pre>
<span class="ruby-comment cmt"># File lib/acts_as_price_helper.rb, line 10</span>
10: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">test_acts_as_price_methods</span> <span class="ruby-identifier">column_name</span>, <span class="ruby-identifier">seperator</span>, <span class="ruby-identifier">options</span> = {}
11: <span class="ruby-ivar">@acts_as_price_model</span> = <span class="ruby-identifier">options</span>[<span class="ruby-value">:model</span>] <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-value">:model</span>]
12:
13: <span class="ruby-ivar">@column_name</span> = <span class="ruby-identifier">column_name</span>
14: <span class="ruby-identifier">columns_in_cents</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">column</span><span class="ruby-operator">|</span>
15: <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">column</span>).<span class="ruby-identifier">should</span> <span class="ruby-operator">==</span> <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">price_in_cents</span>
16: <span class="ruby-keyword kw">end</span>
17: <span class="ruby-identifier">columns_in_doubles</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">column</span><span class="ruby-operator">|</span>
18: <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">column</span>).<span class="ruby-identifier">should</span> <span class="ruby-operator">==</span> <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">price</span>
19: <span class="ruby-keyword kw">end</span>
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:
24: <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">currency</span>
25: <span class="ruby-identifier">currency</span> = <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">currency</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: <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>
28:
29: <span class="ruby-comment cmt">#test for special cases</span>
30: <span class="ruby-comment cmt">#test if 1.5 is returned as 1.50</span>
31: <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>
32: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-node">&quot;#{currency}. 1.505&quot;</span>, <span class="ruby-identifier">seperator</span>
33: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-node">&quot;#{currency}. 1.504&quot;</span>, <span class="ruby-identifier">seperator</span>
34:
35: <span class="ruby-comment cmt">#test if 1,5 is returned as 1.50</span>
36: <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>
37: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-node">&quot;#{currency}. 1,505&quot;</span>, <span class="ruby-identifier">seperator</span>
38: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-node">&quot;#{currency}. 1,504&quot;</span>, <span class="ruby-identifier">seperator</span>
39:
40: <span class="ruby-comment cmt">#test if float returns 2.05 correctly </span>
41: <span class="ruby-comment cmt">#float can return 2.05 as 2.04 in some cases</span>
42: <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>
43:
44: <span class="ruby-comment cmt">#float can return 2,05 as 2.04 in some cases</span>
45: <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>
46: <span class="ruby-keyword kw">else</span>
47: <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>
48: <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>
49:
50: <span class="ruby-comment cmt">#test for special cases</span>
51: <span class="ruby-comment cmt">#test if 1.5 is returned as 1.50</span>
52: <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>
53: <span class="ruby-comment cmt">#test if 1,5 is returned as 1.50</span>
54: <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>
55:
56: <span class="ruby-comment cmt">#test if float returns 2.05 correctly </span>
57: <span class="ruby-comment cmt">#float can return 2.05 as 2.04 in some cases</span>
58: <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>
59:
60: <span class="ruby-comment cmt">#float can return 2,05 as 2.04 in some cases</span>
61: <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>
62: <span class="ruby-keyword kw">end</span>
63:
64: <span class="ruby-comment cmt">#test an empty_setter</span>
65: <span class="ruby-identifier">test_setter_in_cents</span> <span class="ruby-value str">&quot;&quot;</span>, <span class="ruby-identifier">seperator</span>
66: <span class="ruby-identifier">test_setter_in_doubles</span> <span class="ruby-value str">&quot;&quot;</span>, <span class="ruby-identifier">seperator</span>
67: <span class="ruby-keyword kw">end</span></pre>
</div>
</div>
</div>
<div id="test-setter-in-cents-method" class="method-detail ">
<a name="method-i-test_setter_in_cents"></a>
<div class="method-heading">
<span class="method-name">test_setter_in_cents</span><span
class="method-args">(cents, seperator)</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>
test if the setter sets the price correctly if price is given in integers
</p>
<div class="method-source-code"
id="test-setter-in-cents-source">
<pre>
<span class="ruby-comment cmt"># File lib/acts_as_price_helper.rb, line 70</span>
70: <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>
71: <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>
72: <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>)
73: <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>
74: <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_f</span> <span class="ruby-operator">+</span> <span class="ruby-value">0.5</span>).<span class="ruby-identifier">to_i</span>
75: <span class="ruby-keyword kw">end</span>
76: <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>
77: <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>
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-value str">&quot;&quot;</span>
79: <span class="ruby-keyword kw">else</span>
80: <span class="ruby-identifier">currency</span> = <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">currency</span>
81: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">currency</span>
82: <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>)
83: <span class="ruby-keyword kw">else</span>
84: <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>)
85: <span class="ruby-keyword kw">end</span>
86: <span class="ruby-keyword kw">end</span>
87: <span class="ruby-keyword kw">end</span>
88: <span class="ruby-keyword kw">end</span>
89: <span class="ruby-keyword kw">end</span></pre>
</div>
</div>
</div>
<div id="test-setter-in-doubles-method" class="method-detail ">
<a name="method-i-test_setter_in_doubles"></a>
<div class="method-heading">
<span class="method-name">test_setter_in_doubles</span><span
class="method-args">(double, seperator)</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>
test if the setter sets the price correctly if price is given in doubles
</p>
<div class="method-source-code"
id="test-setter-in-doubles-source">
<pre>
<span class="ruby-comment cmt"># File lib/acts_as_price_helper.rb, line 92</span>
92: <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>
93: <span class="ruby-identifier">currency</span> = <span class="ruby-ivar">@acts_as_price_model</span>.<span class="ruby-identifier">currency</span>
94: <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>)
95: <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>
96: <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>)
97: <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>
98: <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-value">100</span>) <span class="ruby-operator">+</span> <span class="ruby-value">0.5</span>).<span class="ruby-identifier">to_s</span>.<span class="ruby-identifier">to_i</span>
99: <span class="ruby-keyword kw">end</span>
100: <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>
101: <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>
102: <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>
103: <span class="ruby-keyword kw">else</span>
104: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">currency</span>
105: <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">to_f</span> <span class="ruby-operator">+</span> <span class="ruby-value">0.0001</span>).<span class="ruby-identifier">gsub</span>(<span class="ruby-value str">&quot;.&quot;</span>, <span class="ruby-identifier">seperator</span>)
106: <span class="ruby-keyword kw">else</span>
107: <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">to_s</span>).<span class="ruby-identifier">gsub</span>(<span class="ruby-value str">&quot;.&quot;</span>, <span class="ruby-identifier">seperator</span>)
108: <span class="ruby-keyword kw">end</span>
109: <span class="ruby-keyword kw">end</span>
110: <span class="ruby-keyword kw">end</span>
111: <span class="ruby-keyword kw">end</span>
112: <span class="ruby-keyword kw">end</span></pre>
</div>
</div>
</div>
</div>
</div>
<div id="rdoc-debugging-section-dump" class="debugging-section">
<p>Disabled; run with --debug to generate this.</p>
</div>
<div id="validator-badges">
<p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
<p><small>Generated with the <a href="http://deveiate.org/projects/Darkfish-Rdoc/">Darkfish
Rdoc Generator</a> 1.1.6</small>.</p>
</div>
</body>
</html>