acts_as_money

plugin | rails November 13 2006

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 &lt;Money:0x249ef9c @currency="USD", @cents=10000&gt;

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.

posted by brandon | updated June 19th 07:29 PM
comments feed

11 comments

  1. Brandon, can you tell us how you’ve licensed this plugin? I can’t seem to find anything in the code about it… Thanks!!

    Haig Didizian Haig Didizian
    January 09, 2007 at 02:33 PM
  2. Haig,

    MIT. I’ll add it to the code.

    Brandon Keepers Brandon Keepers
    January 09, 2007 at 11:31 PM
  3. 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.

    John Ward John Ward
    March 16, 2007 at 04:40 PM
  4. 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?

    Brandon Brandon
    March 16, 2007 at 05:11 PM
  5. 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

    John Ward John Ward
    March 17, 2007 at 02:49 AM
  6. 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?

    LBo LBo
    May 06, 2007 at 06:19 PM
  7. 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 the composed_of plugin 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])
    end
    

    And then in your views:

    text_field_tag 'mymodel[price][dollars]'
    text_field_tag 'mymodel[price][cents]'
    
    Brandon Brandon
    May 07, 2007 at 10:16 AM
  8. I haven’t tried this myself… does this plugin work with Rails 2.0?

    JM JM
    January 15, 2008 at 02:16 PM
  9. 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.

    Srgt PPD Srgt PPD
    February 20, 2008 at 05:37 AM
  10. Srgt PPD,

    The ”$” is coming from the money gem. If you want the amount without it, you can just call #to_s on the money object.

    Brandon Brandon
    February 20, 2008 at 10:49 PM
  11. 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?

    nathaniel nathaniel
    March 03, 2008 at 04:45 PM

Speak your mind:

(Required)

(Required)


(You may use textile in your comments.)

About

I'm Brandon Keepers, a web application developer that likes beautiful code, valid markup and adherence to standards. As a part of Collective Idea in Holland, Michigan, I practice Agile software development primarily using Ruby on Rails.

-86.103171 42.785037

Contact:

more ยป

Syndicate