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