public
Description: Allows you to enter a '$' in fields stored as decimal but are actually currency types
Homepage: http://www.faithfulgeek.org/2008/3/26/acts_as_currency-released
Clone URL: git://github.com/faithfulgeek/acts_as_currency.git
Joe Fiorini (author)
Tue Mar 25 23:41:53 -0700 2008
commit  9f36b48c6ce640bccbaf01d6647fc7ab283642d8
tree    0b573845d0fbd4d9c50a58a279671ce4dece21c3
acts_as_currency / lib / acts_as_currency.rb
100644 21 lines (13 sloc) 0.426 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class ActiveRecord::Base
 
  def self.acts_as_currency(*args)
 
    args.each do |arg|
    
      define_method arg.to_s + '=' do |currency_string|
        currency_string.gsub!(/^\$/, '') if currency_string =~ /^\$/
        write_attribute(arg, currency_string)
      end
    
    end
 
    define_method 'validate' do
      args.each { |arg| errors.add(arg, "should be at least 0.01") if eval(arg.to_s).nil? || eval(arg.to_s) < 0.01 }
    end
      
  end
 
end