Module ActsAsPriceHelper
In: lib/acts_as_price_helper.rb

Methods

Public Instance methods

[Source]

     # File lib/acts_as_price_helper.rb, line 108
108:   def columns_in_cents 
109:     [:price_in_cents, "#{@column_name}_in_cents"]
110:   end

[Source]

     # File lib/acts_as_price_helper.rb, line 112
112:   def columns_in_doubles
113:     [:price, @column_name]
114:   end

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

[Source]

    # 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:     if @acts_as_price_model.currency
24:       currency = @acts_as_price_model.currency
25:       test_setter_in_doubles "#{currency}. 1.41", seperator
26:       test_setter_in_doubles "#{currency}. 1,41", seperator
27: 
28:       #test for special cases
29:       #test if 1.5 is returned as 1.50
30:       test_setter_in_doubles "#{currency}. 1.5", seperator
31:       #test if 1,5 is returned as 1.50
32:       test_setter_in_doubles "#{currency}. 1,5", seperator
33: 
34:       #test if float returns 2.05 correctly 
35:       #float can return 2.05 as 2.04 in some cases
36:       test_setter_in_doubles "#{currency}. 2.05", seperator
37:       
38:       #float can return 2,05 as 2.04 in some cases
39:       test_setter_in_doubles "#{currency}. 2,05", seperator
40:     else
41:       test_setter_in_doubles "1.41", seperator
42:       test_setter_in_doubles "1,41", seperator
43: 
44:       #test for special cases
45:       #test if 1.5 is returned as 1.50
46:       test_setter_in_doubles "1.5", seperator
47:       #test if 1,5 is returned as 1.50
48:       test_setter_in_doubles "1,5", seperator
49: 
50:       #test if float returns 2.05 correctly 
51:       #float can return 2.05 as 2.04 in some cases
52:       test_setter_in_doubles "2.05", seperator
53:       
54:       #float can return 2,05 as 2.04 in some cases
55:       test_setter_in_doubles "2,05", seperator
56:     end
57:     
58:     #test an empty_setter
59:     test_setter_in_cents "", seperator
60:     test_setter_in_doubles "", seperator
61:   end

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

[Source]

    # File lib/acts_as_price_helper.rb, line 64
64:   def test_setter_in_cents cents, seperator
65:     columns_in_cents.each do |setter|
66:       @acts_as_price_model.send("#{setter}=", cents)
67:       columns_in_cents.each do |getter|
68:         @acts_as_price_model.send(getter).should == cents.to_i
69:       end
70:       columns_in_doubles.each do |getter|
71:         if cents == ""
72:           @acts_as_price_model.send(getter).should == ""
73:         else
74:           currency = @acts_as_price_model.currency
75:           if currency
76:             @acts_as_price_model.send(getter).should == currency + ". " + sprintf("%.2f", (cents.to_f / 100).to_s).gsub(".", seperator)
77:           else
78:             @acts_as_price_model.send(getter).should == sprintf("%.2f", (cents.to_f / 100).to_s).gsub(".", seperator)
79:           end
80:         end
81:       end
82:     end
83:   end

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

[Source]

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

[Validate]