Skip to content
This repository has been archived by the owner on Sep 4, 2018. It is now read-only.

Latest commit

 

History

History
89 lines (62 loc) · 1.98 KB

README.textile

File metadata and controls

89 lines (62 loc) · 1.98 KB

Introduction

Rails plugin which simplifies adding money and currency attributes to
ActiveRecord models. Uses money gem by RubyMoney.

Installation

Install the plugin:

  script/plugin install git://github.com/kammerer/has_money.git

Usage

Currency field:


class Foo < ActiveRecord::Base
  has_currency :currency
  validates_currency :currency, :in => ["EUR", "PLN"]
end

foo = Foo.new
foo.currency = "EUR"
foo.currency # -> Money::Currency.new("EUR")

foo.currency = Money::Currency.new("PLN") 
foo.currency # -> Money::Currency.new("PLN")

Uses single database column called currency (string).

Money and currency fields:


class Bar < ActiveRecord::Base
  has_money :price
  validates_money :price, :greater_than => 0, :currency => { :in => ["EUR", "PLN"] }
end

bar = Bar.new
bar.price_currency = "EUR"
bar.price = 5
bar.price # -> Money.new(500, "EUR")

bar.price = Money.new(1000, "PLN")
bar.price # -> Money.new(2.5, "EUR")

bar.price_currency = "PLN"
bar.price # -> Money.new(1000, "PLN") assuming 1 EUR == 4 PLN

Uses two database columns:

  • price (integer)
  • price_currency (string)

Money validation options:

  • :greater_than
  • :greater_than_or_equal
  • :less_than
  • :less_than_or_equal
  • :between

All options except :between expects amount of money in main units (i.e. euros or dollars, not cents) as a value. :between expects
a range.

Multiple money fields with the same currency:


class Baz
  has_currency :currency, :for => [:min_price, :max_price]
  has_money :min_price, :with_currency => :currency
  has_money :max_price, :with_currency => :currency
end

Uses three database columns:

  • min_price (integer)
  • max_price (integer)
  • currency (string)

TODOs

  • tests for ActiveRecord style validations