| 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 14
14: def acts_as_price column_name, options = {}
15: validates column_name, :presence => true, :numericality => {:greater_than => 0} if options[:validates] == true
16:
17: #setters
18: define_method("#{column_name}=") do |val|
19: super((val.to_f * 100).to_s)
20: end
21: alias_method("price=", "#{column_name}=")
22:
23: define_method("price_in_cents=") do |val|
24: self.send("price=", val.to_f / 100)
25: end
26: alias_method "#{column_name}_in_cents=", "price_in_cents="
27:
28: #getters
29: define_method(column_name) do
30: if super == 0.0
31: ""
32: else
33: sprintf("%.2f", super.to_f / 100)
34: end
35: end
36: alias_method "price", column_name
37:
38: define_method("price_in_cents") {((send column_name).to_f * 100).to_s.to_i}
39: alias_method "#{column_name}_in_cents", "price_in_cents"
40: end