forked from nofxx/money
-
Notifications
You must be signed in to change notification settings - Fork 0
/
acts_as_money.rb
40 lines (34 loc) · 1.15 KB
/
acts_as_money.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Money require 'money'
# based on github.com/collectiveidea/acts_as_money
module ActsAsMoney #:nodoc:
def self.included(base) #:nodoc:
base.extend ClassMethods
end
module ClassMethods
#
# class Product
# has_money :value, :tax, :opts => opts
# end
#
# @product.value.class #=> "Money"
# @product.value_cents #=> "1000"
# @product.tax_currency #=> "USD"
#
# Opts:
# :cents => "pennys" #=> @product.pennys
# :currency => "currency" #=> @product.currency
# :allow_nil => true
# :with_currency => true
#
def has_money(*attributes)
config = {:with_currency => true, :converter => lambda { |m| m ||=0; m.to_money },
:allow_nil => false }.update(attributes.extract_options!)
attributes.each do |attr|
mapping = [[config[:cents] || "#{attr}_cents", 'cents']]
mapping << [config[:currency] || "#{attr}_currency", 'currency'] if config[:with_currency]
composed_of attr, :class_name => 'Money', :allow_nil => config[:allow_nil],
:mapping => mapping, :converter => config[:converter]
end
end
end
end