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

Rails plugin allowing to define money and currency attributes on ActiveRecord models.

License

Notifications You must be signed in to change notification settings

kammerer/has_money

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Rails plugin allowing to define money and currency attributes on ActiveRecord models.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages