Module ActsAsPriceHelper
In: lib/acts_as_price_helper.rb

Methods

Public Instance methods

[Source]

    # File lib/acts_as_price_helper.rb, line 78
78:   def columns_in_cents 
79:     [:price_in_cents, "#{@column_name}_in_cents"]
80:   end

[Source]

    # File lib/acts_as_price_helper.rb, line 82
82:   def columns_in_doubles
83:     [:price, @column_name]
84:   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:     test_setter_in_doubles "1.41", seperator
24:     test_setter_in_doubles "1,41", seperator
25:     
26:     #test for special cases
27:     #test if 1.5 is returned as 1.50
28:     test_setter_in_doubles "1.5", seperator
29:     #test if 1,5 is returned as 1.50
30:     test_setter_in_doubles "1,5", seperator
31:     
32:     #test if float returns 2.05 correctly 
33:     #float can return 2.05 as 2.04 in some cases
34:     test_setter_in_doubles "2.05", seperator
35:     
36:     #float can return 2,05 as 2.04 in some cases
37:     test_setter_in_doubles "2,05", seperator
38:     
39:     #test an empty_setter
40:     test_setter_in_cents "", seperator
41:     test_setter_in_doubles "", seperator
42:   end

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

[Source]

    # File lib/acts_as_price_helper.rb, line 45
45:   def test_setter_in_cents cents, seperator
46:     columns_in_cents.each do |setter|
47:       @acts_as_price_model.send("#{setter}=", cents)
48:       columns_in_cents.each do |getter|
49:         @acts_as_price_model.send(getter).should == cents.to_i
50:       end
51:       columns_in_doubles.each do |getter|
52:         if cents == ""
53:           @acts_as_price_model.send(getter).should == ""
54:         else
55:           @acts_as_price_model.send(getter).should == sprintf("%.2f", (cents.to_f / 100).to_s).gsub(".", seperator)
56:         end
57:       end
58:     end
59:   end

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

[Source]

    # File lib/acts_as_price_helper.rb, line 62
62:   def test_setter_in_doubles double, seperator
63:     columns_in_doubles.each do |setter|
64:       @acts_as_price_model.send("#{setter}=", double)
65:       columns_in_cents.each do |getter|
66:         @acts_as_price_model.send(getter).should == (double.gsub(",", ".").to_f * 100).to_s.to_i
67:       end
68:       columns_in_doubles.each do |getter|
69:         if double == ""
70:           @acts_as_price_model.send(getter).should == ""
71:         else
72:           @acts_as_price_model.send(getter).should == sprintf("%.2f", double.gsub(",", ".")).gsub(".", seperator)
73:         end
74:       end
75:     end
76:   end

[Validate]