Sha256: e3f411dbbe3923c6e9cd05471a240e09a979f78a5401b1b900da2e9eb35a9b01

Contents?: true

Size: 827 Bytes

Versions: 6

Compression:

Stored size: 827 Bytes

Contents

raise "ActiveRecord needs to be already required" unless defined?(ActiveRecord::Base)

class ActiveRecord::Base
  # Convenience wrapper around ActiveRecord's composed_of method for Money attributes that need to be persisted.
  #
  # @param [Array of Symbols]
  # @example
  #   composed_of_money :unit_price
  #   composed_of_money :unit_price, :total_price
  def self.composed_of_money(*fields)
    fields.each do |field|
      composed_of field,
                  :class_name => "Money",
                  :mapping => [[field, "amount"]],
                  :constructor => Proc.new { |amount| Money.new(amount || 0, Money.default_currency) },
                  :converter => Proc.new { |value| value.respond_to?(:to_money) ? value.to_money : raise(ArgumentError, "Can't convert #{value.class} to Money") }
    end
  end
end

Version data entries

6 entries across 1 versions & 1 rubygems

Version Path
apl-library-0.0.90 vendor/bundle/ruby/1.8/gems/decimal-money-5.1.1.patched/lib/money/active_record_extensions.rb
apl-library-0.0.90 vendor/bundle/ruby/1.9.1/gems/decimal-money-5.1.1.patched/lib/money/active_record_extensions.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.8/gems/decimal-money-5.1.1.patched/lib/money/active_record_extensions.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.9.1/gems/decimal-money-5.1.1.patched/lib/money/active_record_extensions.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/2.1.0/gems/decimal-money-5.1.1.patched/lib/money/active_record_extensions.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/decimal-money-5.1.1.patched/lib/money/active_record_extensions.rb