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
Search Repo:
acts_as_currency / lib / acts_as_currency.rb
100644 20 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
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