lib/money/acts_as_money.rb in nofxx-money-2.2.2 vs lib/money/acts_as_money.rb in nofxx-money-2.3.1
- old
+ new
@@ -10,28 +10,29 @@
# class Product
# has_money :value, :tax, :opts => opts
# end
#
# @product.value.class #=> "Money"
- # @product.value_in_cents #=> "1000"
+ # @product.value_cents #=> "1000"
# @product.tax_currency #=> "USD"
#
# Opts:
# :cents => "pennys" #=> @product.pennys
# :currency => "currency" #=> @product.currency
# :allow_nil => true
- # :with_currency => true
+ # :with_currency => false
+ # :with_cents => true #=> 1000.to_money #=> #<Money @cents=1000>
#
def has_money(*attributes)
- config = {:with_currency => true, :converter => lambda { |m| m.to_money },
- :allow_nil => false }.update(attributes.extract_options!)
+ config = {:with_currency => true, :with_cents => false,
+ :allow_nil => true }.update(attributes.extract_options!)
- attributes.each do |attr|
- mapping = [[config[:cents] || "#{attr}_cents", 'cents']]
- mapping << [config[:currency] || "#{attr}_currency", 'currency'] if config[:with_currency]
+ for attribute in attributes do
+ mapping = [[config[:cents] || "#{attribute}_cents", 'cents']]
+ mapping << [config[:currency] || "#{attribute}_currency", 'currency'] if config[:with_currency]
- composed_of attr, :class_name => 'Money',:allow_nil => config[:allow_nil],
- :mapping => mapping, :converter => config[:converter]
+ 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
end