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
|
# File 'lib/counterfeit/active_record.rb', line 9
def has_counterfeit(attr, options={})
options = options.with_indifferent_access
default_currency = options.delete(:currency).try(:to_s).try(:upcase)
amount_attr = options.delete(:amount_attribute) || "#{attr}_in_cents"
currency_attr = options.delete(:currency_attribute) || "#{attr}_currency"
mapping = [[ amount_attr, 'cents' ], [ currency_attr, 'currency_as_string' ]]
after_initialize do
self[currency_attr] ||= default_currency || Money.default_currency.iso_code
end
constructor = lambda { |cents, currency| Money.new(cents || 0, currency) }
converter = lambda do |value|
if value.respond_to?(:to_money)
value.to_money(default_currency)
else
raise ArgumentError, "Can't convert #{value.class} to Money"
end
end
composed_of attr.to_sym,
:class_name => 'Money',
:mapping => mapping,
:constructor => constructor,
:converter => converter
end
|