Files

ActsAsPriceHelper

Public Instance Methods

columns_in_cents() click to toggle source
     # File lib/acts_as_price_helper.rb, line 114
114:   def columns_in_cents 
115:     [:price_in_cents, "#{@column_name}_in_cents"]
116:   end
columns_in_doubles() click to toggle source
     # File lib/acts_as_price_helper.rb, line 118
118:   def columns_in_doubles
119:     [:price, @column_name]
120:   end
test_acts_as_price_methods(column_name, seperator, options = {}) click to toggle source

Test the acts_as_price plugin for the specified column_name and given seperator.

Valid options:

  • model: Not just the model name, but a full object of the model

EXAMPLE:

Note the you can also specify the model in a before block in your tests

    # File lib/acts_as_price_helper.rb, line 10
10:   def test_acts_as_price_methods column_name, seperator, options = {}
11:     @acts_as_price_model = options[:model] if options[:model]
12:     
13:     @column_name = column_name
14:     columns_in_cents.each do |column|
15:       @acts_as_price_model.send(column).should == @acts_as_price_model.price_in_cents
16:     end
17:     columns_in_doubles.each do |column|
18:       @acts_as_price_model.send(column).should == @acts_as_price_model.price
19:     end
20:     
21:     #normal test for setter
22:     test_setter_in_cents "144", seperator
23:     
24:     if @acts_as_price_model.currency
25:       currency = @acts_as_price_model.currency
26:       test_setter_in_doubles "#{currency}. 1.41", seperator
27:       test_setter_in_doubles "#{currency}. 1,41", seperator
28: 
29:       #test for special cases
30:       #test if 1.5 is returned as 1.50
31:       test_setter_in_doubles "#{currency}. 1.5", seperator
32:       test_setter_in_doubles "#{currency}. 1.505", seperator
33:       test_setter_in_doubles "#{currency}. 1.504", seperator
34:       
35:       #test if 1,5 is returned as 1.50
36:       test_setter_in_doubles "#{currency}. 1,5", seperator
37:       test_setter_in_doubles "#{currency}. 1,505", seperator
38:       test_setter_in_doubles "#{currency}. 1,504", seperator
39: 
40:       #test if float returns 2.05 correctly 
41:       #float can return 2.05 as 2.04 in some cases
42:       test_setter_in_doubles "#{currency}. 2.05", seperator
43:       
44:       #float can return 2,05 as 2.04 in some cases
45:       test_setter_in_doubles "#{currency}. 2,05", seperator
46:     else
47:       test_setter_in_doubles "1.41", seperator
48:       test_setter_in_doubles "1,41", seperator
49: 
50:       #test for special cases
51:       #test if 1.5 is returned as 1.50
52:       test_setter_in_doubles "1.5", seperator
53:       #test if 1,5 is returned as 1.50
54:       test_setter_in_doubles "1,5", seperator
55: 
56:       #test if float returns 2.05 correctly 
57:       #float can return 2.05 as 2.04 in some cases
58:       test_setter_in_doubles "2.05", seperator
59:       
60:       #float can return 2,05 as 2.04 in some cases
61:       test_setter_in_doubles "2,05", seperator
62:     end
63:     
64:     #test an empty_setter
65:     test_setter_in_cents "", seperator
66:     test_setter_in_doubles "", seperator
67:   end
test_setter_in_cents(cents, seperator) click to toggle source

test if the setter sets the price correctly if price is given in integers

    # File lib/acts_as_price_helper.rb, line 70
70:   def test_setter_in_cents cents, seperator
71:     columns_in_cents.each do |setter|
72:       @acts_as_price_model.send("#{setter}=", cents)
73:       columns_in_cents.each do |getter|
74:         @acts_as_price_model.send(getter).should == (cents.to_f + 0.5).to_i
75:       end
76:       columns_in_doubles.each do |getter|
77:         if cents == ""
78:           @acts_as_price_model.send(getter).should == ""
79:         else
80:           currency = @acts_as_price_model.currency
81:           if currency
82:             @acts_as_price_model.send(getter).should == currency + ". " + sprintf("%.2f", (cents.to_f / 100).to_s).gsub(".", seperator)
83:           else
84:             @acts_as_price_model.send(getter).should == sprintf("%.2f", (cents.to_f / 100).to_s).gsub(".", seperator)
85:           end
86:         end
87:       end
88:     end
89:   end
test_setter_in_doubles(double, seperator) click to toggle source

test if the setter sets the price correctly if price is given in doubles

     # File lib/acts_as_price_helper.rb, line 92
 92:   def test_setter_in_doubles double, seperator
 93:     currency = @acts_as_price_model.currency
 94:     double = double.gsub("#{currency}. ", "")
 95:     columns_in_doubles.each do |setter|
 96:       @acts_as_price_model.send("#{setter}=", double)
 97:       columns_in_cents.each do |getter|
 98:         @acts_as_price_model.send(getter).should == ((double.gsub(",", ".").to_f * 100) + 0.5).to_s.to_i
 99:       end
100:       columns_in_doubles.each do |getter|
101:         if double == ""
102:           @acts_as_price_model.send(getter).should == ""
103:         else
104:           if currency
105:             @acts_as_price_model.send(getter).should == currency + ". " + sprintf("%.2f", double.gsub(",", ".").to_f + 0.0001).gsub(".", seperator)
106:           else
107:             @acts_as_price_model.send(getter).should == sprintf("%.2f", double.gsub(",", ".").to_s).gsub(".", seperator)
108:           end
109:         end
110:       end
111:     end
112:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.