acts_as_money
At Collective Idea, we often try to take pieces of code that we use often and extract them into plugins. We’re trying to get better about making them available, so, here we go:
acts_as_money is a fairly trivial plugin that has proved to be very useful. It just makes it a little easier to work with the money gem.
class Product < ActiveRecord::Base
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 option to false and not have a currency field.
class Room < ActiveRecord::Base
money :rate, :cents => :rate_in_cents, :currency => :rate_currency
money :discount, :cents => :discount_in_cents, :currency => false
end
acts_as_money also incorporates a patch to composed_of that I submitted to core that 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>
Installation
script/plugin install git://github.com/collectiveidea/acts_as_money.git
Hope you find this useful. I know I’ve been using it in any app that handles money, and it’s been a time saver. Allowing me to use a composed_of field in forms has been extremely helpful.








11 comments
Brandon, can you tell us how you’ve licensed this plugin? I can’t seem to find anything in the code about it… Thanks!!
January 09, 2007 at 02:33 PM
Haig,
MIT. I’ll add it to the code.
January 09, 2007 at 11:31 PM
I think it would be a good idea to package the acts_as_money without the composed_of patch.
Had you considered that?
This is not a reflection on the quality or otherwise, just that it would appear separate from the money code.
March 16, 2007 at 04:40 PM
John,
When I initially released it, acts_as_money actually didn’t use the aggregations patch. But the code was doing some ugly string eval stuff. It was only a couple lines, but ugly none-the-less. I added the aggregations patch to demonstrate its usefulness (no one cared).
I ended up packaging the patch as a plugin, so I could remove it from acts_as_money, but I’m guessing people would find it more annoying to have to install 2 plugins. Unless it’s getting in the way, I think I’m going to leave it as is. Got any good reasons to remove it?
March 16, 2007 at 05:11 PM
Brandon, Thanks for the reply.
The reasons I was considering were as its been posted as a patch it will eventually end up in core rails.
John
March 17, 2007 at 02:49 AM
Hi Brandon,
My users have to fill in a price with two seperate input fields. One for a full amount and one for cents. Together it makes the whole price.
What would be the best way to take care of the separate formvalues in combination with this plugin?
May 06, 2007 at 06:19 PM
LBo,
There’s not exactly a way to do it with the acts_as_money plugin, but if you look at the source for the plugin, it’s very trivial. Mainly, it makes use of a patch to
composed_of, which I have also packaged as a plugin. In your situation, I would just install thecomposed_ofplugin patch, then do something like the following in your model:composed_of :price, :class_name => 'Money', :mapping => [%w(cents cents), %w(currency currency)] do |hash| Money.new("#{hash[:dollars]}.#{hash[:cents]}", hash[:currency]) endAnd then in your views:
May 07, 2007 at 10:16 AM
I haven’t tried this myself… does this plugin work with Rails 2.0?
January 15, 2008 at 02:16 PM
How do you remove the dollar sign from the format method? I tried to delete it, but nothing happend. Search within the code of the plugin but couldn’t find any more ”$” signs.
February 20, 2008 at 05:37 AM
Srgt PPD,
The ”$” is coming from the money gem. If you want the amount without it, you can just call
#to_son the money object.February 20, 2008 at 10:49 PM
I am having a hell of time with acts_as_money and rails 2.0.2. Everytime I edit a record and click save, whatever price is in my price field gets two extra zeros. I am forced to trim two zeros off of every price field everytime I open a record for editing, even if all I wanted to do was adjust a different field.
I have no idea why this is happening. Has anyone had this sort of issue happen to them?
March 03, 2008 at 04:45 PM
Speak your mind: