Module ActiveRecord::Acts::Price::ClassMethods
In: lib/acts_as_price.rb

Methods

Public Instance methods

Specify the column_name that needs to act as price

Valid options:

  • :validates => true (performs validates_presence_of and validates_numericality_of)
  • :comma_seperated => true (set and get the price as a comma-seperated value)

[Source]

    # File lib/acts_as_price.rb, line 15
15:         def acts_as_price column_name, options = {}
16:           validates column_name, :presence => true, :numericality => {:greater_than => 0} if options[:validates] == true
17:           currency, comma = options[:currency], options[:comma_seperated]
18:           
19:           #setters
20:           define_method("#{column_name}=") do |val|
21:             super((val.to_s.gsub(",", ".").to_f * 100).to_s)
22:           end
23:           alias_method("price=", "#{column_name}=")
24:           
25:           define_method("price_in_cents=") do |val|
26:             self.send("price=", val.to_f / 100)
27:           end
28:           alias_method "#{column_name}_in_cents=", "price_in_cents="
29:           
30:           #getters
31:           define_method(column_name) do
32:             if super == 0.0
33:               ""
34:             else
35:               if comma
36:                 return_val = sprintf("%.2f", super.to_f / 100).gsub(".", ",")
37:               else
38:                 return_val = sprintf("%.2f", super.to_f / 100)
39:               end
40:               return_val = currency ? "#{currency}. #{return_val}" : return_val
41:               return_val
42:             end
43:           end
44:           alias_method "price", column_name
45:           
46:           define_method("price_in_cents") do
47:             if currency
48:               ((send column_name).gsub("#{currency}. ", "").gsub(",", ".").to_f * 100).to_s.to_i
49:             else
50:               ((send column_name).gsub(",", ".").to_f * 100).to_s.to_i
51:             end
52:           end
53:           alias_method "#{column_name}_in_cents", "price_in_cents"
54:           
55:           define_method("currency") do
56:             currency
57:           end
58:         end

[Validate]