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
|
# File 'lib/money_column/stores_money.rb', line 8
def stores_money(money_name, options = {})
options.reverse_merge!({
:cents_attribute => "#{money_name}_in_cents",
:allow_nil => true
})
class_eval <<-EOV
def #{money_name}
cents = send('#{options[:cents_attribute]}')
if !#{options[:allow_nil]}
cents ||= 0
end
if cents.blank?
nil
else
my_currency = self.respond_to?(:currency) && !self.currency.nil? ? currency : ::Money.default_currency
Money.new(cents, my_currency)
end
end
def #{money_name}=(amount)
self.#{options[:cents_attribute]} = if amount.blank?
nil
else
amount.to_money.cents
end
end
EOV
end
|