Module: ActsAsMoney::ClassMethods

Defined in:
lib/money/acts_as_money.rb

Instance Method Summary collapse

Instance Method Details

#has_money(*attributes) ⇒ Object

class Product

has_money :value, :tax, :opts => opts

end

Opts: :cents => “pennys” #=> @product.pennys :currency => “currency” #=> @product.currency :allow_nil => true :with_currency => false :with_cents => true #=> 1000.to_money #=> #<Money @cents=1000>



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/money/acts_as_money.rb', line 25

def has_money(*attributes)
  config = {:with_currency => true, :with_cents => false,
            :allow_nil => false }.update(attributes.extract_options!)

  for attribute in attributes do
    mapping = [[config[:cents] || "#{attribute}_cents", 'cents']]
    mapping << [config[:currency] || "#{attribute}_currency", 'currency'] if config[:with_currency]

    composed_of attribute, :class_name => 'Money', :allow_nil => config[:allow_nil],
       :mapping => mapping, :converter => lambda { |m| (m||0).to_money(config[:with_cents]) }
  end

end