Module: FantasticCurrency::ActiveRecord::ClassMethods

Defined in:
lib/fantastic_currency.rb

Instance Method Summary collapse

Instance Method Details

#currency(field_name) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/fantastic_currency.rb', line 115

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
  
  alias_method "#{field_name.to_s}_before_type_cast", "#{field_name}"
    
  define_method "#{field_name.to_s}=" do |value|
    raise "Money doesn't float!" if value.class.name == "Float"
    
    currency = FantasticCurrency::Config.get_currency(self[:currency])
    self[field_name] = (BigDecimal.new(value.to_s) * (10**currency[:precision])).to_i
  end
  
end