| Module | ActiveRecord::Acts::Price::ClassMethods |
| In: |
lib/acts_as_price.rb
|
Specify the column_name that needs to act as price
Valid options:
# 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: comma = 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: sprintf("%.2f", super.to_f / 100).gsub(".", ",")
37: else
38: sprintf("%.2f", super.to_f / 100)
39: end
40: end
41: end
42: alias_method "price", column_name
43:
44: define_method("price_in_cents") do
45: ((send column_name).gsub(",", ".").to_f * 100).to_s.to_i
46: end
47: alias_method "#{column_name}_in_cents", "price_in_cents"
48: end