Module: FantasticCurrency::ActiveRecord::ClassMethods

Defined in:
lib/fantastic_currency.rb

Instance Method Summary collapse

Instance Method Details

#currency(field_name) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/fantastic_currency.rb', line 108

def currency(field_name)
    
  define_method "#{field_name.to_s}" do |*args|
    if self[field_name]
      format_currency(self[field_name], { :currency => self[:currency] }.merge(args.first || {}))
    end
  end
    
  define_method "#{field_name.to_s}=" do |value|
    raise "Money doesn't float!" if value.class.name == "Float"
    
    if self[field_name]
      currency = FantasticCurrency::Config.get_currency(self[:currency])
      self[field_name] = (BigDecimal.new(value.to_s) * (10**currency[:precision])).to_i
    end
  end
  
end