acts_as_money

DESCRIPTION:

Improved version of acts_as_money.

FEATURES/PROBLEMS:

  • Fixes a problem in acts_as_money 0.2.5 [rubygems.org/gems/acts_as_money/versions/0.2.5] that causes strings such as “3,25” and “3.25” to be interpreted differently. This gem handles both inputs equally.

  • This gem simply tries to call “to_money” on the received object which seems much simpler to me.

  • This also means that it doesn’t matter what you enter, be it 3 (Fixnum), 3.00 (Float), “3.00” (String) or “3,00” (String).

  • Leveraging the “to_money” method provided by the money gem seems like a good thing to do.

SYNOPSIS:

(modified description from the acts_as_money gem)

acts_as_money is a fairly trivial plugin that makes it easier to work with the money gem.

class Product < ActiveRecord::Base
  acts_as_money
  money :price
end

This assumes that there are 2 columns in the database, cents (integer) and currency (string). These fields can be changed by setting the :cents and :currency options. To use the default currency, you can simple set :currency to false

class Room < ActiveRecord::Base
  acts_as_money
  money :rate, :cents => :rate_in_cents, :currency => :rate_currency
  money :discount, :cents => :discount_in_cents, :currency => false
end

By default, your money field will default to a value of 0. If you require it to default to nil, you may set the :allow_nil option:

class Meal < ActiveRecord::Base
  acts_as_money
  money :bill, :allow_nil => true
end

m = Meal.new
m.bill #returns nil

r = Room.new
r.rate      #returns <Money:0x24fd53a6 @currency="USD", @cents=0>

acts_as_money allows you to pass a String, Fixnum, Float or Money object as a parameter to the setter, and it will call #to_money to convert it to a Money object. This makes it convenient for using money fields in forms.

r = Room.new :rate => "100.00"
r.rate                            # returns <Money:0x249ef9c @currency="USD", @cents=10000>

REQUIREMENTS:

  • activerecord

  • money

INSTALL:

gem install cmdjohnson-acts_as_money

In your Gemfile:

gem "cmdjohnson-acts_as_money", "0.0.1", :require => "acts_as_money"

LICENSE:

(The MIT License)

Copyright © 2013 Commander Johnson

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.