Sha256: a0c73fb1c74f97f2a2277f352798f3f2ac5083e790d30583bcd69647740e98a0

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

# 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 => false
    # :with_cents => true       #=>  1000.to_money #=> #<Money @cents=1000>
    #
    def has_money(*attributes)
      config = {:with_currency => true, :with_cents => false,
                :allow_nil => true }.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

  end

end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
ShadowBelmolve-money-2.3.1 lib/money/acts_as_money.rb
nofxx-money-2.3.1 lib/money/acts_as_money.rb