0.0
No release in over 3 years
Low commit activity in last 3 years
A set of helper methods for working with money-based attributes.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.0.3
~> 4.2.10
~> 10.3.2
~> 0.9.4
~> 3.0.0

Runtime

~> 3.2.19
= 0.3.0
= 6.1.1
 Project Readme

MoneyColumn build status

A set of helper methods for working with money-based attributes.

How it works

Let's say you have the following Car class:

class Car
  include MoneyColumn::StoresMoney

  attr_accessor :price_in_cents

  stores_money :price

  def currency
    "USD"
  end
end

So let's go ahead and store a price_in_cents for the car:

car = Car.new
car.price_in_cents = 5000

Since we want to show this in a view as $ 50.00, we can now do the following:

> car.price.format
=> "$50.00"

> car.price
 => #<Money:0x1016e9fb8 @cents=5000, @bank=#<Money::VariableExchangeBank:0x10143f6f0 @mutex=#<Mutex:0x10143f678>, @rates={}>, @currency="USD">

If the currency instance method is defined on your class, MoneyColumn will use that. Otherwise, it will use the default currency specified in the Money gem.

Under the covers

When you define stores_money :price, MoneyColumn looks for an attribute named price_in_cents to use for its conversion.

If you want to explicity tell MoneyColumn where to look for the cents_attribute, do the following:

class CarWithSpecifiedAttribute
  include MoneyColumn::StoresMoney
  
  attr_accessor :amount_in_cents
  
  stores_money :price, :cents_attribute => "amount_in_cents"
end