Project

economy

0.0
No commit activity in last 3 years
No release in over 3 years
High performance multicurrency money for rails.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.2
~> 0.21

Runtime

~> 5.1
~> 4.0
 Project Readme

Gem Version Code Climate Build Status Dependency Status

Economy

High performance multicurrency money for rails.

Why

I did this gem to:

  • Keep rates cached in redis for optimal performance and sync between instances.
  • Have an out of the box working rates service.
  • Be able to make sql queries without the need to convert integers into decimals.
  • Share a common currency column for multiple money fields if a need it.
  • Avoid the need to manually format the string representation in views.

Install

Put this line in your Gemfile:

gem 'economy'

Then bundle:

$ bundle

To install Redis you can use homebrew:

$ brew install redis

Configuration

Generate the configuration file:

$ bundle exec rails g economy:install

Set the global settings:

Economy.configure do |config|

  config.rates = :yahoo
  config.default_currency = 'USD'

  config.add_currency(
    iso_code: 'USD',
    iso_number: 840,
    symbol: 'U$S',
    decimals: 2
  )
  config.add_currency(
    iso_code: 'UYU',
    iso_number: 858,
    symbol: '$U',
    decimals: 2
  )

end

NOTE: You may want to personalize the generated config/redis.yml.

Usage

Definitions

Define the money field in your models:

class Product < ActiveRecord::Base
  monetize :price
end

Add the money columns to your tables:

class AddPriceToProducts < ActiveRecord::Migration
  def change
    add_column :products, :price, :decimal, precision: 24, scale: 6
    add_column :products, :currency, :string
  end
end

NOTE: You can use a gobal "currency" column o a prefixed "price_currency" column.

Attributes

If you want to assign values, everything continuos working the same:

product.price = 20.00
product.currency = 'USD'

Arithmetics are intuitive:

product.price * 2
# => U$S 40

product.price / 2
# => U$S 10

product.price + Economy::Money.new(10, 'USD')
# => U$S 30

product.price - Economy::Money.new(10, 'USD')
# => U$S 10

To exchange to another currency:

product.price.exchange_to 'UYU'

The formatting method is to_s, it uses active support, so there is no need to call a helper in your views:

<%= product.price %>

Rates

To update rates:

$ bundle exec rake economy:update_rates

Or the plain method:

Economy.update_rates

NOTE: You probably want to put the rake task into a cronjob.

Contributing

Any issue, pull request, comment of any kind is more than welcome!

I will mainly ensure compatibility to Rails, AWS, PostgreSQL, Redis, Elasticsearch and FreeBSD. 

Credits

This gem is maintained and funded by museways.

License

It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.